<?php

namespace App\Http\Controllers\Channel\OfficialAccount;

use App\Http\Controllers\Channel\BaseController as ChannelBaseController;

use App\Http\Controllers\Channel\OfficialAccount\Transformers\SmartPushMsgTransformer;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use App\Modules\OfficialAccount\Services\MsgService;
use App\Modules\Statistic\Services\SendStatistics;

/**
 * 关注回复
 */
class DistributionSelfDefineContentsController extends ChannelBaseController
{

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

    /**
     * @apiVersion 1.0.0
     * @api {GET} distribution_self_define_contents 获取关注回复
     * @apiGroup OfficialAccount
     * @apiName distribution_self_define_contents
     * @apiParam   {String}  distribution_channel_id 渠道ID.
     */

    function getSubReply(Request $request)
    {

        $distribution_channel_id = $this->getChannelId();

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

        $sub_reply = MsgService::getReply($distribution_channel_id);

        $data = [];
        $list = [];
        $data['mode'] = 1;

        if($sub_reply)
        {
            isset($sub_reply['current']) && $data['mode'] = $sub_reply['current'];
            if(isset($sub_reply[2]))//有设置图文
            {
                $list[] = [
                    'content'=>json_decode($sub_reply[2]),
                    'is_pic'=>1,
                    'status'=>$data['mode'] == 2 ? 1 : 0
                ];
            }

            if(isset($sub_reply[3]))//有设置文本
            {
                $list[] = [
                    'content'=>$sub_reply[3],
                    'is_pic'=>0,
                    'status'=>$data['mode'] == 3 ? 1 : 0
                ];
            }
        }

        $data['list'] = $list;
        return response()->success($data);
    }
    
    /**
     * @apiVersion 1.0.0
     * @api {GET} distribution_self_define_contents 获取关注回复列表
     * @apiGroup OfficialAccount
     * @apiName distribution_self_define_contents
     * @apiParam   {String}  distribution_channel_id 渠道ID.
     */
    
    function getSubReplyList(Request $request)
    {
    
    	$distribution_channel_id = $this->getChannelId();
    
    	if(empty($distribution_channel_id)) {
    		return response()->error("PARAM_EMPTY");
    	}
    
    	$sub_reply = MsgService::getReply($distribution_channel_id);
    	if(!isset($sub_reply['content_mode_2'])){
    		$sub_reply['content_mode_2'] = '';
    	}
    	if(!isset($sub_reply['content_mode_3'])){
    		$sub_reply['content_mode_3'] = '';
    	}
    	if(!isset($sub_reply['current'])){
    		$sub_reply['current'] = '1';
    	}
//     	$pic_list = isset($sub_reply['content_mode_2'])?$sub_reply['content_mode_2']:'';
//     	$pic_list = object_to_array(json_decode($pic_list));
//     	$text_list = isset($sub_reply['content_mode_3'])?$sub_reply['content_mode_3']:'';
//     	$text_list = object_to_array(json_decode($text_list));
//     	$mode = isset($sub_reply['current'])?$sub_reply['current']:'1';
//     	\Log::info('pic_list');\Log::info($pic_list);
//     	\Log::info('text_list');\Log::info($text_list);
//     	$data = [
//     	   'pic_list'=>$pic_list,
//     	   'text_list'=>$text_list,
//     	   'mode'=>$mode,
//     	];
    	return response()->success($sub_reply);
    }
    
    function getCurrentReply(Request $request)
    {
    
    	$distribution_channel_id = $this->getChannelId();
    
    	if(empty($distribution_channel_id)) {
    		return response()->error("PARAM_EMPTY");
    	}
    
    	$sub_reply = MsgService::getCurrentReply($distribution_channel_id);
    	$sub_reply = object_to_array($sub_reply);
    	return response()->success($sub_reply);
    }

    /**
     * @apiVersion 1.0.0
     * @api {GET} distribution_self_define_contents 设置关注回复
     * @apiGroup OfficialAccount
     * @apiName set_distribution_self_define_contents
     * @apiParam   {String}  distribution_channel_id 渠道ID.
     */

    function setSubReply(Request $request)
    {

        $distribution_channel_id = $this->getChannelId();
        $mode = $request->has('mode') ? (int)$request->input('mode') : 1;
        $is_pic = $request->has('is_pic') ? (int)$request->input('is_pic') : 0;
        $start_time = $request->has('start_time') ?$request->input('start_time') : '2018-01-01';
        $end_time = $request->has('end_time') ? $request->input('end_time') : '2030-01-01';
        $content = $request->has('content') ? $request->input('content') : '';
        $key = $request->has('key') ? $request->input('key') : '0';
        $action = $request->has('action') ? $request->input('action') : 'update';
        $status = $request->has('status') ? (int)$request->input('status') : 1;
        $is_pic && $content = json_encode($content);

        if(empty($distribution_channel_id)) {
            return response()->error("PARAM_EMPTY");
        }
        
        \Log::info('setSubReply');\Log::info($content);
        if(strpos($content,'w.url.cn') > -1){
        	\Log::info('setSubReply_url_false');
        	return response()->error("SHORT_URL_NOT_ALLOW_ERROR");
        }

        $sub_reply = MsgService::setReply(compact('distribution_channel_id','is_pic','content','mode','start_time','end_time','key','action','status'));
        return response()->success();
    }


}