<?php

namespace App\Http\Controllers\Wechat\OfficialAccount;

use App\Modules\Channel\Services\ChannelService;

use App\Http\Controllers\Channel\BaseController as ChannelBaseController;
use App\Modules\OfficialAccount\Models\OfficialAccount;
use App\Modules\OfficialAccount\Models\OfficialInteractiveEvent;
use App\Modules\Channel\Models\Channel;
use App\Http\Controllers\Wechat\OfficialAccount\Transformers\OfficialMenuTransformer;
use App\Modules\OfficialAccount\Services\SelfDefineService;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use App\Libs\OSS;

class OfficialMenusController extends ChannelBaseController
{

    /**
     * @apiDefine OfficialAccount 公众号
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/officialMenusList 获取自动配置的菜单
     * @apiGroup OfficialAccount
     * @apiName officialMenusList
     * @apiSuccess {String}  type 菜单类型.
     * @apiSuccess {String}  name 菜单名称.
     * @apiSuccess {String}  url 链接.
     * @apiSuccess {String}  name 名称.
     * @apiSuccess {String}  sub_button 子菜单.
     * @apiSuccessExample {json} Success-Response:·
     *
     *      {
     *          "code": 0,
     *          "msg": "",
     *          "data": [
     *              {
     *                  "type": "view",
     *                  "name": "继续阅读",
     *                  "url": "www.baidu.comcontinue"
     *              },
     *              {
     *                  "type": "view",
     *                  "name": "书城首页",
     *                  "url": "www.baidu.com"
     *              },
     *              {
     *                  "name": "用户中心",
     *                  "sub_button": [
     *                      {
     *                          "type": "view",
     *                          "name": "个人中心",
     *                          "url": "www.baidu.comuser"
     *                      },
     *                      {
     *                          "type": "view",
     *                          "name": "阅读记录",
     *                          "url": "www.baidu.combookshelf"
     *                      },
     *                      {
     *                          "type": "view",
     *                          "name": "我要充值",
     *                          "url": "www.baidu.compaycenter"
     *                      },
     *                      {
     *                          "type": "click",
     *                          "name": "联系客服",
     *                          "key": "contact_customer"
     *                      }
     *                  ]
     *              }
     *          ]
     *      }
     */

    function officialMenusList(Request $request)
    {

        $appid = $request->has('appid') ? $request->input('appid') : '';

        if (empty($appid)) {
            return response()->error("PARAM_EMPTY");
        }

        $officialAccount = OfficialAccount::officialAccountByAppid($appid);

        if (!empty($officialAccount)) {

            $distribution_channel_id = $officialAccount['distribution_channel_id'];
            
            $self_config = ChannelService::check_channel_account_priv($distribution_channel_id,'menu_type');
            $menu_type = isset($self_config->content)?$self_config->content:'menu_default';

            //默认菜单
            $buttons =SelfDefineService::getDefaultMenu($distribution_channel_id);

            //自定义菜单
            if( $selfDefinedButtons = SelfDefineService::getMenuFormated($distribution_channel_id,['status'=>1]) )
            {
                $buttons = $selfDefinedButtons;
            }else{
                if($menu_type == 'user_center'){
                    $buttons =
                        [
                            [
                                "type" => "click",
                                "name" => "最近阅读",
                                "key" => "recent_read",
                            ],
                            [
                                "type" => "click",
                                "name" => "今日签到",
                                "key" => 'daily_sign',
                            ],
                            [
                                "type" => "click",
                                "name" => "用户中心",
                                "key" => 'user_center',
                            ],
                        ];
                }
            }

            return response()->success($buttons);
        }
    }

    function officialMenusListV1(Request $request)
    {

        $appid = $request->has('appid') ? $request->input('appid') : '';

        if (empty($appid)) {
            return response()->error("PARAM_EMPTY");
        }

        $officialAccount = OfficialAccount::officialAccountByAppid($appid);

        if (!empty($officialAccount)) {

            $distribution_channel_id = $officialAccount['distribution_channel_id'];

            $WECHAT_CUSTOM_HOST = env('WECHAT_CUSTOM_HOST');

            $self_config = ChannelService::check_channel_account_priv($distribution_channel_id,'menu_type');
            $menu_type = isset($self_config->content)?$self_config->content:'menu_default';

        
            $encodeDistributionChannelId = encodeDistributionChannelId($distribution_channel_id);
            \Log::info('officialMenusList：distribution_channel_id:'.$distribution_channel_id.' encodeDistributionChannelId:'.$encodeDistributionChannelId.' menu_type:'.$menu_type);

            $base_url = env('PROTOCOL') . '://site' . $encodeDistributionChannelId . '.' . $WECHAT_CUSTOM_HOST . '.com/';
            $help_url = 'https://help.'.$WECHAT_CUSTOM_HOST.'.com?distribution_channel_id='.$encodeDistributionChannelId;

            if($is_yq_move){
            	\Log::info('officialMenusList_is_yunqi_move:'.$distribution_channel_id);

            	$buttons =
            	[
	            	[
		            	"type" => "click",
		            	"name" => "最近阅读",
		            	"key" => "recent_read",
	            	],
            	];

            }else{
            	if($menu_type == 'user_center'){
            		\Log::info('officialMenusList_$menu_type_1:'.$distribution_channel_id);

            		$buttons =
            		[
	            		[
		            		"type" => "click",
		            		"name" => "最近阅读",
		            		"key" => "recent_read",
	            		],
	            		[
		            		"type" => "click",
		            		"name" => "今日签到",
		            		"key" => 'daily_sign',
	            		],
	            		[
		            		"type" => "click",
		            		"name" => "用户中心",
		            		"key" => 'user_center',
	            		],
            		];
            	}else{
            		\Log::info('officialMenusList_$menu_type_default:'.$distribution_channel_id);

            		$buttons =
            		[
	            		[
		            		"type" => "click",
		            		"name" => "最近阅读",
		            		"key" => "recent_read",
	            		],
	            		[
		            		"type" => "click",
		            		"name" => "今日签到",
		            		"key" => 'daily_sign',
	            		],
	            		[
		            		"name" => "用户中心",
		            		"sub_button" =>
		            		[
			            		[
			            		"type" => "view",
			            		"name" => "个人中心",
			            		"url" => $base_url . 'person',
			            		],
			            		[
			            		"type" => "view",
			            		"name" => "书城首页",
			            		"url" => $base_url,
			            		],
			            		[
			            		"type" => "view",
			            		"name" => "优惠充值",
			            		"url" => $base_url . 'pay',
			            		],
			            		[
			            		"type" => "view",
			            		"name" => "帮助中心",
			            		"url" => $help_url,
			            		],
	            			],
            			],
            		];

            		$selfDefinedButtons = SelfDefineService::getMenuFormated($distribution_channel_id);
            		if($selfDefinedButtons){
            		    $buttons = $selfDefinedButtons;
            		    \Log::info('self_defined_buttons:'.$distribution_channel_id.':'.json_encode($selfDefinedButtons));
                    }
            	}
            }


            return response()->success($buttons);
        }
    }


}
