<?php

class StartlightTreasureActModel extends ActBaseModel implements ActInterface
{
    private $magicNum = 0;
    function getActBasicConfig(): array
    {
        return [];
    }

    function formatActData(array &$actConfig)
    {
        if (empty($actConfig['starlightConfExtra']['collect_base_num']))
            $this->response(__LINE__, "收集物基础值为空");
        $actConfig['reward_conf'] = $actConfig['starlightConfExtra'];
        $config = $actConfig['starlightTreasureConf'];
        unset($actConfig['starlightTreasureConf']);
        unset($actConfig['starlightConfExtra']);
        foreach ($config as  $i => &$item){
            $wrapIdx = $i+1;
            // 代币收集配置
            if (empty($item['tokenCollectConfig']))
                $this->response(__LINE__, "区间:{$wrapIdx} 代币收集配置为空");
            foreach ($item['tokenCollectConfig'] as &$tokenCollect){
                if (empty($tokenCollect['target']))
                    $this->response(__LINE__, "区间:{$wrapIdx} 代币收集  目标值为空");
                if (empty($tokenCollect['value']))
                    $this->response(__LINE__, "区间:{$wrapIdx} 代币收集  代币数为空");
            }

            // 关卡配置
            if (empty($item['starlightConfig']))
                $this->response(__LINE__, "区间:{$wrapIdx} 关卡配置为空");
            foreach ($item['starlightConfig'] as &$subItem){
                $subItem['prize'] = @json_decode(urldecode($subItem['prize']), true) ?? [];
                if (empty($subItem['target']))
                    $this->response(__LINE__, "区间:{$wrapIdx} 关卡配置  星星数为空");
                if (empty($subItem['prize']))
                    $this->response(__LINE__, "区间:{$wrapIdx} 关卡配置  奖励为空");
                if (empty($subItem['patternType']))
                    $this->response(__LINE__, "区间:{$wrapIdx} 关卡配置  魔法阵图案为空");
            }

            // 星星收集配置
            if (empty($item['tokenStageConfig']))
                $this->response(__LINE__, "区间:{$wrapIdx} 星星收集配置为空");
            foreach ($item['tokenStageConfig'] as &$subItem){
                $subItem['prize'] = @json_decode(urldecode($subItem['prize']), true) ?? [];
                if (empty($subItem['target']))
                    $this->response(__LINE__, "区间:{$wrapIdx} 关卡配置  星星数为空");
                if (empty($subItem['prize']))
                    $this->response(__LINE__, "区间:{$wrapIdx} 关卡配置  奖励为空");
            }

            //魔法阵收集配置
            if (empty($item['magicPatternConfig']))
                $this->response(__LINE__, "区间:{$wrapIdx} 魔法阵收集配置为空");
            $item['magicPatternConfig']['prize'] = @json_decode(urldecode($item['magicPatternConfig']['prize']), true) ?? [];
            if (empty($item['magicPatternConfig']['target']))
                $this->response(__LINE__, "区间:{$wrapIdx} 魔法阵收集配置  魔法阵数为空");
            if (empty($item['magicPatternConfig']['prize']))
                $this->response(__LINE__, "区间:{$wrapIdx} 魔法阵收集配置  奖励为空");
        }

        $actConfig['extraData'] = $config;
    }

    function writeActCache(array $actConfig)
    {
        $conditionKeys = okeys::activityConditionKeys($actConfig['aid']);
        $stageRewardKeys = okeys::activityRewardsStageKeys($actConfig['aid']);
        $rewardConf = json_decode($actConfig['extraData'], true);
        foreach ($rewardConf as $idx => $item) {
            $condition = $this->getStageConditionLvSpinsShushu($item, $item, $item);
            $stageConf = [
                'tokenCollectConfig' => $item['tokenCollectConfig'],
                'starlightConfig' => $item['starlightConfig'],
                'tokenStageConfig' => $item['tokenStageConfig'],
                'magicPatternConfig' => $item['magicPatternConfig'],
            ];
            oo::commonOprRedis('games')->hset($stageRewardKeys, $idx, json_encode($stageConf, 32));
            oo::commonOprRedis('games')->hset($conditionKeys, $idx, json_encode($condition, 32));
        }

        $expireTime = max($actConfig['end_time'], time() + 60);#redis最少一分钟
        oo::commonOprRedis('games')->expireAt($conditionKeys, oo::redisRandomExpire($expireTime));
        oo::commonOprRedis('games')->expireAt($stageRewardKeys, oo::redisRandomExpire($expireTime));
        return true;
    }

    function serializeShowConf(array &$actConfig)
    {

    }

    function importActData(array &$basicConfig, $sheetArray)
    {
        $imgNames = oo::commonOprModel('config')->getActiveImgNameConfig()[$this->actType];
        $this->magicNum = intval($imgNames[$basicConfig['cid']]['extraConf']['magicNum']['val']);
        if (empty($basicConfig['imgName']))
            $this->response(__LINE__, "活动资源为空");
        if (empty($this->magicNum))
            $this->response(__LINE__, "当前活动资源：{$basicConfig['imgName']} 未配置魔法阵数量");
        $sheetIdx = 0;

        $rewardsConfig = $this->parseLvSpinsShushuStagesMappingV1($sheetArray[++$sheetIdx]);

        $tokenCollectMapping = $this->parseTokenCollectMapping($sheetArray[++$sheetIdx]);

        $levelsMapping = $this->parseLevelsMapping($sheetArray[++$sheetIdx]);

        $tokenStagesMapping = $this->parseTokenStagesMapping($sheetArray[++$sheetIdx]);

        $magicPatternMapping = $this->parseMagicPatternMapping($sheetArray[++$sheetIdx]);


        foreach ($rewardsConfig as $id => $item){
            if(empty($tokenCollectMapping[$item['id']]))
                $this->response(__LINE__, "分层：{$item['id']} 无代币收集配置", $tokenCollectMapping);

            if(empty($levelsMapping[$item['id']]))
                $this->response(__LINE__, "分层：{$item['id']} 无关卡配置", $levelsMapping);

            if(empty($tokenStagesMapping[$item['id']]))
                $this->response(__LINE__, "分层：{$item['id']} 无星星收集配置", $tokenStagesMapping);

            if(empty($magicPatternMapping[$item['id']]))
                $this->response(__LINE__, "分层：{$item['id']} 无魔法阵收集配置", $magicPatternMapping);

            $rewardsConfig[$id]['tokenCollectConfig'] = $tokenCollectMapping[$item['id']];
            $rewardsConfig[$id]['starlightConfig'] = $levelsMapping[$item['id']];
            $rewardsConfig[$id]['tokenStageConfig'] = $tokenStagesMapping[$item['id']];
            $rewardsConfig[$id]['magicPatternConfig'] = $magicPatternMapping[$item['id']];
        }

        foreach ($sheetArray[0] as $item){
            if ($item[0] == '收集物基础值'){
                $basicConfig['starlightConfExtra'] = [
                    'collect_base_num' => intval($item[1])
                ];
            }
        }
        $basicConfig['starlightTreasureConf'] = $rewardsConfig;
    }

    private function parseTokenCollectMapping($sheet): array
    {
        $mapping = [];
        for ($i=1; $i<count($sheet); $i++){
            $item = $sheet[$i];
            list($idx, $target, $value) = $item;
            $mapping[$idx][] = [
                'target' => intval($target),
                'value' => intval($value),
            ];
        }
        return $mapping;
    }
    private function parseTokenStagesMapping($sheet): array
    {
        $mapping = [];
        for ($i=1; $i<count($sheet); $i++){
            $item = $sheet[$i];
            list($idx, $target) = $item;
            $prize = $this->parseLineRewards(array_slice($item, 2), __LINE__);
            $mapping[$idx][] = [
                'target' => intval($target),
                'prize' => urlencode(json_encode($prize)),
            ];
        }
        return $mapping;
    }
    private function parseLevelsMapping($sheet): array
    {
        $mapping = [];
        for ($i=1; $i<count($sheet); $i++){
            $item = $sheet[$i];
            list($idx, $target, $patternType) = $item;
            $patternType = intval($patternType);
            if ($patternType > $this->magicNum)
                $this->response(__LINE__, "魔法阵图案序号：{$patternType} 大于星图数 {$this->magicNum}");
            if ($patternType < 1)
                $this->response(__LINE__, "魔法阵图案序号：{$patternType} 配置异常");

            $prize = $this->parseLineRewards(array_slice($item, 3), __LINE__);
            $mapping[$idx][] = [
                'level' => count($mapping[$idx]) + 1,
                'target' => intval($target),
                'patternType' => $patternType - 1,
                'prize' => urlencode(json_encode($prize)),
            ];
        }
        return $mapping;
    }

    private function parseMagicPatternMapping($sheet): array
    {
        $mapping = [];
        for ($i=1; $i<count($sheet); $i++){
            $item = $sheet[$i];
            $idx = $item[0];
            $prize = $this->parseLineRewards(array_slice($item, 1), __LINE__);
            $mapping[$idx] = [
                'target' => $this->magicNum,
                'prize' => urlencode(json_encode($prize)),
            ];
        }
        return $mapping;
    }
}