<?php

class CrazyTokenModel extends ActBaseModel implements ActInterface
{
    static $tokenTypes = [

        44 => "拼图碎片" ,// 44,
        45 => "玛雅转盘代币" ,// 45,
        51 => "幸运小丑代币" ,//=> 51,幸运小丑(狐狸)代币
        60 => "超级扭蛋代币" ,//=> 60,
        70 => "塔罗牌代币" ,//=> 70,
        72 => "推币机代币" ,//=> 72,
        74 => "暴走兔子代币" ,//=> 74,
        77 => "星图代币" ,//=> 77,

    ];

    function serializeShowConf(array &$actConfig)
    {

    }

    function formatActData(array &$actConfig)
    {
        if (empty($actConfig['crazyTokenConfig'])) $this->response(1, '保存失败，奖励配置异常');
        if (empty($actConfig['crazyTokenExtra']['icons'])) $this->response(1, '保存失败，代币配置为空');
        $stageRewardValidNum = [3, 5, 7];
        if($actConfig['type'] == CRAZY_TOKEN_2)$stageRewardValidNum = [10];
        foreach ($actConfig['crazyTokenConfig'] as $idx => $item) {
            if (empty($item['stages'])) $this->response(1, "第{$idx}层等级区间未配置体力层");
            foreach ($item['stages'] as $idx1 => $item1) {

                if (empty($item1['stages'])) $this->response(1, "第{$idx}层等级区间，第{$idx1}层体力区间未配置数数层");
                foreach ($item1['stages'] as $idx2 => $item2) {
                    if (empty($item2['layers'])) $this->response(1, "第{$idx}层等级区间，第{$idx1}层体力区间，第{$idx2}层数数区间未配置奖励层级");
                    foreach ($item2['layers'] as $idx3 => $item3) {

                        if (empty($item3['stages'])) $this->response(1, "第{$idx}层等级区间，第{$idx1}层体力区间，第{$idx2}层数数区间, 第{$idx3}奖励层级, 未配置阶段");
                        $stageNum = 0;
                        foreach ($item3['stages'] as $idx4 => $rewardItem) {

                            $tmpPrize = array_values(json_decode(urldecode($rewardItem['prize']), true));
                            if (empty($tmpPrize) ) $this->response(1, "第{$idx}层等级区间，第{$idx1}层体力区间，第{$idx2}层数数区间, 第{$idx3}奖励层级, 第{$idx4}阶段, 未配置奖励", $rewardItem);

                            $actConfig['crazyTokenConfig'][$idx]['stages'][$idx1]['stages'][$idx2]['layers'][$idx3]['stages'][$idx4]['prize'] = $tmpPrize;
                            $stageNum++;
                        }
//                        if (!in_array($stageNum, $stageRewardValidNum)) $this->response(1, "第{$idx}层等级区间，第{$idx1}层体力区间，第{$idx2}层数数区间, 第{$idx3}奖励层级, 阶段奖励数异常：{$stageNum}，应为".implode('、', $stageRewardValidNum));
                        $actConfig['crazyTokenConfig'][$idx]['stages'][$idx1]['stages'][$idx2]['layers'][$idx3]['stageNum'] = $stageNum;
                    }
                }
            }
        }
        $actConfig['reward_conf'] = $actConfig['crazyTokenConfig'];
        $actConfig['extraData'] = $actConfig['crazyTokenExtra'];
        unset( $actConfig['crazyTokenExtra']);
        unset( $actConfig['crazyTokenConfig']);
    }

    function writeActCache(array $actConfig)
    {
        $conditionKeys = okeys::activityConditionKeys($actConfig['aid']);
        $stageRewardKeys = okeys::activityRewardsStageKeys($actConfig['aid']);
        $rewardInfo = json_decode($actConfig['reward_conf'], true) ?? [];
        $expireTime = ActBaseModel::getActCacheExpireTime($actConfig['end_time']);#redis最少一分钟
        foreach ($rewardInfo as $idx1 => $item1) {
            foreach ($item1['stages'] as $idx2 => $item2) {
                foreach ($item2['stages'] as $idx3 => $item3) {
                    $condition = $this->getStageConditionLvSpinsShushu($item1, $item2, $item3);
                    $stageKey = self::getMultiStageKey([$idx1, $idx2, $idx3]);
                    $stageConditions = array_values($condition);
                    unset($item3['shushu']);
                    oo::commonOprRedis('games')->hset($conditionKeys, $stageKey, json_encode($stageConditions, 32));
                    oo::commonOprRedis('games')->hset($stageRewardKeys, $stageKey, json_encode($item3, 32));
                }
            }
        }

        oo::commonOprRedis('games')->expireAt($conditionKeys, oo::redisRandomExpire($expireTime));
        oo::commonOprRedis('games')->expireAt($stageRewardKeys, oo::redisRandomExpire($expireTime));
    }

    function importActData(array &$basicConfig, $sheetArray)
    {
        $sheetIdx = 0;
        $extraConf = $sheetArray[++$sheetIdx] ?? [];
        $stageMappingSheet = $sheetArray[++$sheetIdx] ?? [];
        $rewardsSheet      = $sheetArray[++$sheetIdx] ?? [];
        list($rewardsConfig, $idxMapping) = $this->parseLvSpinsShushuStagesMapping($stageMappingSheet);
        foreach ($rewardsSheet as $k => $item){
            $lineNum = $k + 1;
            $item = array_filter($item);
            if(empty($k) || empty($item))continue;
            list($idx, $layer, $stage, $stepnum) = $item;
            if(empty($idxMapping[$idx]))$this->response(__LINE__, "第{$lineNum}行,唯一ID：{$idx} 不存在");
            list($lvIdx, $spinsIdx, $ssIdx) = $idxMapping[$idx];
            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['layers'] = $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['layers'] ?? [];
            $layerCnt = count($rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['layers']);
            if(!in_array($layer, [$layerCnt, $layerCnt + 1]))$this->response(__LINE__, "第{$lineNum}行,层级不连续");
            $layerIdx = $layer - 1;
            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['layers'][$layerIdx]['stages'] = $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['layers'][$layerIdx]['stages'] ?? [];
            $stageCnt = count($rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['layers'][$layerIdx]['stages']);
            if($stage != $stageCnt + 1)$this->response(__LINE__, "第{$lineNum}行,阶段不连续");
            $prizeTmp = $this->parseLineRewards(array_slice($item, 4), $lineNum);
            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['layers'][$layerIdx]['stages'][] = [
                'stage' => $stage,
                'stepnum' => $stepnum,
                'prize' => urlencode(json_encode($prizeTmp)),
            ];
        }
//        $this->response(__LINE__, "66666", $rewardsConfig);
        $tokenMap = array_flip(self::$tokenTypes);
        $tokens = [];
        $unlimited = 0;
        for($i=0;$i<count($extraConf);$i++){
            $lineNum = $i + 1;
            $text = $extraConf[$i][0];
            if(empty($text))continue;
            switch ($text){
                case "代币展示图标":
                    unset($extraConf[$i][0]);
                    foreach ($extraConf[$i] as $text){
                        if(empty($text))continue;
                        if(empty($tokenMap[$text]))$this->response(1, "第{$lineNum}行，代币：{$text} 不存在");
                        $tokens[] = $tokenMap[$text];
                    }
                    break;
                case '无限升级开关':$unlimited = intval($extraConf[$i][1]);break;
            }
        }

        $basicConfig['crazyTokenExtra'] = [
            'icons' => array_unique($tokens),
            'noCircle' => $unlimited,
        ];
        $basicConfig['crazyTokenConfig'] = $rewardsConfig;
    }

    function getActBasicConfig(): array
    {
        return [
            'tokens' => self::$tokenTypes
        ];
    }
}