<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * inst twitter粉丝页
 * Date: 2018/1/31
 * Time: 11:35
 */
class Fans extends MY_Controller
{
    CONST SIGNCONFIGID = 1;
    CONST CHANNELGROUP = 4;

    CONST TWITTERFANS   = 1;
    CONST INSTAGRAMFANS = 2;
    CONST FACEBOOKFANS  = 3;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取类型twitter,inst,facebook
     */
    public function getTypes() {
        return [
            self::TWITTERFANS   => 'twitter',
            self::INSTAGRAMFANS => 'instagram',
            self::FACEBOOKFANS  => 'facebook',
        ];
    }

    public function getLangTypes() {
        return [
            self::TWITTERFANS   => 'TWITTER',
            self::INSTAGRAMFANS => 'INSTAGRAM',
            self::FACEBOOKFANS  => 'FACEBOOK',
        ];
    }

    /**
     * 弹出时机
     */
    public function showTypes() {
         return [
            1 => '体力不足最后一个',
            2 => '金币不足最后一个',
            3 => '关闭帮助后',
            4 => '关闭设置后',
        ];       
    }

    /**
     * 获取inst，twitter粉丝页配置信息
     */
    public function index()
    {
        $info       = oo::commonOprRedis('games')->hGetAll(okeys::fansPage());
        if(!empty($info['open_group'])){
            $info['open_group'] = json_decode($info['open_group'], true);
        }else{
            $info['open_group'] = []; 
        }
        $info['open_type'] = empty($info['open_type']) ? self::TWITTERFANS : $info['open_type'];
        $configJson = oo::commonOprModel('json')->getStaticConfig('RecommendConfig');
        if(empty((array)$configJson['showType'])){
            $configJson['showType'] = [];
        }

        $langs = $this->getLangTypes();
        foreach($langs as $k => $v){
            if(!isset($configJson[$k])){
                $configJson[$k] = [
                    'language' => [
                        'titleKey' => 'RECOMMEND.'.$v.'.TITLE',
                        'contentKey' => 'RECOMMEND.'.$v.'.CONTENT',
                        'buttonKey' => 'RECOMMEND.'.$v.'.BUTTON',
                     ],
                    'imgUrl' => '',
                    'btnBgUrl' => '',
                    'btnIconUrl' => '',
                    'btnAction' => '',
                ];
            }
        }

        $arr        = oo::commonOprModel('config')->getUserGroupConfigByCid(self::SIGNCONFIGID);
        $groups     = array_column($arr, 'name', 'id');
        $data = [
            'info'       => $info,
            'configJson' => $configJson,
            'groups'     => $groups,
            'types'      => $this->getTypes(),
            'showTypes'  => $this->showTypes(),
        ];
        $this->load->view('fans/index', $data);
    }

    /**
     * 编辑inst，twitter粉丝页配置信息
     * @return string
     */
    public function edit(){
        $info = $this->input->post('info');
        $configJson = $this->input->post('configJson');
        if(!empty($configJson)){
            $origConfigJson = oo::commonOprModel('json')->getStaticConfig('RecommendConfig');
            $filename = 'RecommendConfig'.time().'.json';
            $origConfigJson['maxEveryDay'] = empty($configJson['maxEveryDay']) ? 10 : (int)$configJson['maxEveryDay'];
            $origConfigJson['maxTotal'] = empty($configJson['maxTotal']) ? 10 : (int)$configJson['maxTotal'];
            $origConfigJson['showType'] = empty($configJson['showType']) ? [] : (array)$configJson['showType'];
            foreach($this->getTypes() as $k => $v){
                if(!isset($configJson[$k])){
                    continue;
                }
                if(!isset($configJson[$k]['imgUrl'], $configJson[$k]['btnBgUrl'], $configJson[$k]['btnIconUrl'])){
                    continue;
                }
                unset($origConfigJson[$k]['imgUrl']);
                unset($origConfigJson[$k]['btnBgUrl']);
                unset($origConfigJson[$k]['btnIconUrl']);
                $origConfigJson[$k]['dirName'] = $v;

                if(empty($configJson[$k]['btnAction']) || $configJson[$k]['btnAction'] == 1 || empty($configJson[$k]['btnAction2'])){
                    $origConfigJson[$k]['btnAction'] = trim($configJson[$k]['btnAction']);
                }else{
                    $origConfigJson[$k]['btnAction'] = trim($configJson[$k]['btnAction2']);
                }
                $origConfigJson[$k]['language'] = [
                    'titleKey'   => 'RECOMMEND.'.strtoupper($v).'.TITLE',
                    'contentKey' => 'RECOMMEND.'.strtoupper($v).'.CONTENT',
                    'buttonKey'  => 'RECOMMEND.'.strtoupper($v).'.BUTTON',
                ];
            }

            $res  = oo::commonOprModel('json')->SaveJson($origConfigJson, $filename);
            if($res){
                $res = oo::commonOprModel('json')->setStaticConfig('RecommendConfig', $filename);
            }
        }

        if($info['open_channel'] == self::CHANNELGROUP){
            if(!empty($info['open_group'])){
                $info['open_group'] = json_encode($info['open_group']);
            }
        }else{
            $info['open_group'] = '[]';
        }
        oo::commonOprRedis('games')->hMSet(okeys::fansPage(), $info);

        if(!$res){
            return $this->response(1, '操作失败');
        }else{
            return $this->response(0, '操作成功');
        }
    }

}
