<?php

namespace App\Http\Controllers\Channel\Channel;

use Illuminate\Http\Request;
use App\Http\Controllers\Channel\BaseController;
use App\Modules\Channel\Services\ChannelSubscribeSettingService;
use App\Http\Controllers\Channel\Channel\Transformers\ChannelSubscribeTransformer;
class ChannelSubscribeSettingController extends BaseController
{

    /**
     * @apiDefine ChannelSubscribe 渠道强关
     */



    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取强关配置
     * @api {GET} channels/getSubscribeSetting 获取强关配置
     * @apiGroup ChannelSubscribe
     * @apiName getSubscribeSetting
     * @apiParam     {String}  appid  appid
     * @apiSuccess   {Number}  force_subscribe_type  强关类型
     * @apiSuccess   {String}  resource_url  跳转地址
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *               id:123,
     *               force_subscribe_type:2,
     *               resource_url:12222
     *         }
     *     }
     */
    public  function getSubscribeSetting(Request $request){
        $channel_id = $this->getChannelId();
        $appid = $request->input('appid');
        $setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
        if($setting){
            $setting->id = $channel_id;
            return response()->item(new ChannelSubscribeTransformer,$setting);
        }
        return response()->success();
    }



    /**
     * @apiVersion 1.0.0
     * @apiDescription 设置强关类型
     * @api {POST} channels/setSubscribeType 设置强关类型
     * @apiGroup ChannelSubscribe
     * @apiName setSubscribeType
     * @apiParam   {Number}  force_subscribe_type  强关类型
     * @apiParam   {String}  resource_url  跳转地址
     * @apiParam   {String}  appid  appid
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *               id:123,
     *               force_subscribe_type:2,
     *               resource_url:12222
     *         }
     *     }
     */
    public function setSubscribeType(Request $request){
        $force_subscribe_type = $request->input('force_subscribe_type');
        $resource_url = $request->input('resource_url');
        $distribution_channel_id = $this->getChannelId();
        $appid = $request->input('appid');
        if(empty($force_subscribe_type) || empty($appid)){
            return response()->error('PARAM_EMPTY');
        }
        ChannelSubscribeSettingService::updateOrSettingForceSubScribe(compact('force_subscribe_type','resource_url','distribution_channel_id','appid'));
        $setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
        $setting->force_subscribe_type_text = str_replace(['1','2','3'],['强制关注','弱关注','强制链接关注'],$setting->force_subscribe_type);
        $setting->force_subscribe_type = explode(',',$setting->force_subscribe_type);
        return response()->success($setting);
    }

}
