<?php

class SurpriseCardActModel extends ActBaseModel implements ActInterface
{

    function serializeShowConf(array &$actConfig)
    {

    }

    function formatActData(array &$actConfig)
    {
        $actConfig['extraData'] = [
            'limitConfig' => intval($actConfig['surpriseCardBasic']['limitConfig']),
            'collectConfig' => array_values($actConfig['surpriseCardBasic']['collectConfig']),
        ];
        if(empty($actConfig['surpriseCard']))$this->response(__LINE__,  '奖励配置为空', $actConfig);

        foreach ($actConfig['surpriseCard'] as $lvIdx => $lvItem){
            if(empty($lvItem['stages']))$this->response(__LINE__,  "第{$lvIdx}层等级层， 体力层配置为空", $lvItem);

            foreach ($lvItem['stages'] as $spinsIdx => $spinsItem){
                if(empty($spinsItem['stages']))$this->response(__LINE__,  "第{$lvIdx}层等级层， 第{$spinsIdx}层体力层， 数数层配置为空", $lvItem);

                foreach ($spinsItem['stages'] as $ssIdx => $ssItem){
                    if(empty($ssItem['stageConfig']))$this->response(__LINE__,  "第{$lvIdx}层等级层， 第{$spinsIdx}层体力层， 第{$ssIdx}层数数层， 阶段配置为空", $lvItem);
                    if(empty($ssItem['endStage']))$this->response(__LINE__,  "第{$lvIdx}层等级层， 第{$spinsIdx}层体力层， 第{$ssIdx}层数数层， 最终阶段配置为空", $lvItem);

                    $cnt = count($ssItem['stageConfig']);
                    $actConfig['surpriseCard'][$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig'][$cnt] = $ssItem['endStage'];
                    $actConfig['surpriseCard'][$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig'][$cnt]['stage'] = $cnt+1;
                    $ssItem['stageConfig'][$cnt] = $ssItem['endStage'];
                    foreach ($ssItem['stageConfig'] as $stageIdx => $stageItem){
                        if(empty($stageItem['cards']))$this->response(__LINE__,  "第{$lvIdx}层等级层， 第{$spinsIdx}层体力层， 第{$ssIdx}层数数层， 第{$stageIdx}阶段， 奖励配置为空", $lvItem);

                        foreach ($stageItem['cards'] as $cardIdx => $cardItem){
                            $prize = @json_decode(urldecode($cardItem['prize']),  true) ?? [];
                            if(empty($prize))
                                $this->response(__LINE__,  "第{$lvIdx}层等级层， 第{$spinsIdx}层体力层， 第{$ssIdx}层数数层， 第{$stageIdx}阶段， 第{$cardIdx}个奖励， 配置为空", $stageItem);
                            $actConfig['surpriseCard'][$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig'][$stageIdx]['cards'][$cardIdx]['prize'] = $prize;
                        }
                    }
                    $actConfig['surpriseCard'][$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig'] = array_values($actConfig['surpriseCard'][$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig']);
                }
            }
        }
        $actConfig['reward_conf'] = $actConfig['surpriseCard'];
        unset($actConfig['surpriseCardBasic']);
        unset($actConfig['surpriseCard']);
    }

    function writeActCache(array $actConfig)
    {
        $conditionKeys = okeys::activityConditionKeys($actConfig['aid']);
        $stageRewardKeys = okeys::activityRewardsStageKeys($actConfig['aid']);
        $extraReward = json_decode($actConfig['reward_conf'], true) ?? [];
        $expireTime = max($actConfig['end_time'], time() + 60);#redis最少一分钟
        foreach ($extraReward as $idx1 => $item1) {
            foreach ($item1['stages'] as $idx2 => $item2) {
                foreach ($item2['stages'] as $idx3 => $item3) {
                    $stageConditions = $this->getStageConditionLvSpinsShushu($item1, $item2, $item3);
                    $stageKey = "{$idx1}_{$idx2}_{$idx3}";
                    unset($item3['shushu']);
                    unset($item3['endStage']);
                    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;
        $surpriseCardBasicSheet = $sheetArray[++$sheetIdx];
        $stageMappingSheet = $sheetArray[++$sheetIdx];
        $stageRewardsSheet = $sheetArray[++$sheetIdx];
        $endRewardsSheet = $sheetArray[++$sheetIdx];
        $basicConfig['surpriseCardBasic'] = $this->parseSurpriseCardBasic($surpriseCardBasicSheet);

        list($rewardConfig, $idxMapping) = $this->parseLvSpinsShushuStagesMapping($stageMappingSheet);

        foreach ($stageRewardsSheet as $k => $item){
            if(empty($k))continue;
            list($idx, $stage, $target, $weight) = $item;
            $lineNum = $k + 1;
            if(empty($idxMapping[$idx]))$this->response(__LINE__, "第{$lineNum}行 唯一ID不存在：{$idx}", [$idxMapping, $idx]);
            list($lvIdx, $spinsIdx, $ssIdx) = $idxMapping[$idx];
            $stage = intval($stage);
            if(empty($stage))$this->response(__LINE__, "第{$lineNum}行 阶段配置异常：{$stage}", $item);
            $prize = $this->parseLineRewards(array_slice($item, 4), $lineNum);
            $stageIdx = $stage - 1;
            if(empty($rewardConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig'][$stageIdx])){
                $rewardConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig'][$stageIdx] = [
                    'stage' => $stage,
                    'target' => $target,
                    'cards' => [],
                ];
            }
            $rewardConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig'][$stageIdx]['cards'][] = [
                'weight' => $weight,
                'prize' => urlencode(json_encode($prize, JSON_NUMERIC_CHECK)),
            ];
        }

        foreach ($endRewardsSheet as $k => $item){
            if(empty($k))continue;
            list($idx, $target, $weight) = $item;
            $lineNum = $k + 1;
            if(empty($idxMapping[$idx]))$this->response(__LINE__, "第{$lineNum}行 唯一ID不存在：{$idx}", [$idxMapping, $idx]);
            list($lvIdx, $spinsIdx, $ssIdx) = $idxMapping[$idx];
            $prize = $this->parseLineRewards(array_slice($item, 3), $lineNum);

            $rewardConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['endStage'] = [
                'stage' => count($rewardConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stageConfig']),
                'target' => $target,
                'cards' => [
                    [
                        'weight' => $weight,
                        'prize' => urlencode(json_encode($prize, JSON_NUMERIC_CHECK)),
                    ]
                ],
            ];
        }
        $basicConfig['surpriseCard'] = $rewardConfig;
    }

    private function parseSurpriseCardBasic($surpriseCardBasicSheet){
        $surpriseCardBasic = [
            'limitConfig' => 0,
            'collectConfig' => [
                'oneHit' => 0,
                'twoHit' => 0,
                'threeHit' => 0,
            ],
        ];
        foreach ($surpriseCardBasicSheet as $item){
            list($prefix, $value) = $item;
            $value = intval($value);
            if($value<0) $this->response(__LINE__, "参数异常:{$value}");
            switch ($prefix){
                case '门槛积分':$surpriseCardBasic['limitConfig'] = $value;break;
                case '一连击':$surpriseCardBasic['collectConfig']['oneHit'] = $value;break;
                case '二连击':$surpriseCardBasic['collectConfig']['twoHit'] = $value;break;
                case '三连击':$surpriseCardBasic['collectConfig']['threeHit'] = $value;break;
            }
        }
        if(empty(array_sum($surpriseCardBasic['collectConfig'])))$this->response(__LINE__, "积分配置不能全为0");
        return $surpriseCardBasic;
    }
}