<?php

class MayaActModel extends ActBaseModel implements ActInterface
{

    function formatActData(array &$actConfig)
    {

        $actConfig['extraData'] = json_encode([
            'mayaWheelTotalCnt' => $actConfig['mayaWheelTotalCnt'],
            'mayaWheelSpend' => array_values($actConfig['mayaWheelSpend']),
        ], JSON_NUMERIC_CHECK);
        $actConfig['reward_conf'] = $actConfig['mayaWheelReward'];

        foreach ($actConfig['reward_conf'] as $lvIdx => &$item1) {
            foreach ($item1['stages'] as$spinsIdx =>  &$item2) {
                foreach ($item2['stages'] as $ssIdx => &$itemSs) {
                    foreach ($itemSs['rewards']['diceConf'] as $layerIdx => &$item3) {
                        $hasBonus = false;
                        foreach ($item3['list'] as &$item4) {
                            $item4['bonus'] = boolval($item4['bonus']);
                            $hasBonus = $item4['bonus'] || $hasBonus;
                            if (empty($item4['prize'])) {
                                $this->response(1, '保存失败，转盘奖励为空');
                            }
                        }
                        if(!$hasBonus)$this->response(1, "第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，第{$layerIdx}层奖励未设置大奖", $item3['list']);
                    }
                    foreach ($itemSs['rewards']['progressConf'] as $item5) {
                        if (empty($item5['prize'])) {
                            $this->response(1, '保存失败，进度奖励为空');
                        }
                    }
                }
            }
        }
        unset($actConfig['mayaWheelTotalCnt'], $actConfig['mayaWheelSpend'], $actConfig['mayaWheelReward']);
        $this->saveTokenGoods($actConfig, 'extra_reward');
    }

    function writeActCache(array $actConfig)
    {
        $conditionKeys = okeys::activityConditionKeys($actConfig['aid']);
        $stageRewardKeys = okeys::activityRewardsStageKeys($actConfig['aid']);
//        $stageExtraConfKeys = okeys::activityStageExtraConfigKeys($actConfig['aid']);
        $extraData = json_decode($actConfig['extraData'], true);
        $rewardConf = json_decode($actConfig['reward_conf'], true);
//        $extraReward = json_decode($actConfig['extra_reward'], true);

        foreach ($rewardConf as $idx1 => $item1) {
            foreach ($item1['stages'] as $idx2 => $item2) {
                foreach ($item2['stages'] as $idx3 => $item3) {
                    $idx = "{$idx1}_{$idx2}_{$idx3}";
                    $condition = $this->getStageConditionLvSpinsShushu($item1, $item2, $item3);
                    $diceConfig = [];
                    foreach ($item3['rewards']['diceConf'] as $idx4 => $item4) {
                        $diceConfig[$idx4] = [
                            'totalCnt' => $extraData['mayaWheelTotalCnt'],
                            'spendConfig' => $extraData['mayaWheelSpend'],
                            'rewardConfig' => array_values($item4['list'])
                        ];
                    }
                    $stageConf = ['diceConf' => $diceConfig, 'progressConf' => array_values($item3['rewards']['progressConf'] ?? [])];
                    oo::commonOprRedis('games')->hset($stageRewardKeys, $idx, json_encode($stageConf, 32));
                    oo::commonOprRedis('games')->hset($conditionKeys, $idx, json_encode($condition, 32));
//                    oo::commonOprRedis('games')->hset($stageExtraConfKeys, $idx, json_encode(['tokenGoods' =>$extraReward[$idx] ?? []], 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));
//        oo::commonOprRedis('games')->expireAt($stageExtraConfKeys, oo::redisRandomExpire($expireTime));
        return true;
    }

    function serializeShowConf(array &$actConfig)
    {
        // TODO: Implement serializeShowConf() method.
    }

    function importActData(array &$basicConfig, $sheetArray)
    {
        $sheetIdx = -1;
        $basicArray = $sheetArray[++$sheetIdx] ?? [];
        $stagesMappingArray = $sheetArray[++$sheetIdx] ?? [];
        $stagesRewardsArray = $sheetArray[++$sheetIdx] ?? [];
        $stagesProgressRewardArray = $sheetArray[++$sheetIdx] ?? [];
        $tokenGoodsArray = $sheetArray[++$sheetIdx] ?? [];

        foreach ($basicArray as $k => $item) {
            $line = $k + 1;
            if ($item[0] == '转盘总数') {
                $basicConfig['mayaWheelTotalCnt'] = intval($item[1]);
            }
            if ($item[0] == '代币消耗配置') {
                for ($idx = 1; $idx <= $basicConfig['mayaWheelTotalCnt']; $idx++) {
                    $item[$idx] = intval($item[$idx]);
                    if (empty($item[$idx])) $this->response(-1, $line . '行消耗代币不能为空' . PHP_EOL);
                    $basicConfig['mayaWheelSpend'][$idx - 1] = $item[$idx];
                }
            }
        }
        if (empty($basicConfig['mayaWheelTotalCnt']))
            $this->response(-1,  '转盘总数不能为空' . PHP_EOL);
        if (empty($basicConfig['mayaWheelSpend']))
            $this->response(-1,  '消耗代币配置不能为空' . PHP_EOL);

        list($rewardsConfig, $idxMapping) = $this->parseLvSpinsShushuStagesMapping($stagesMappingArray);


        foreach ($stagesRewardsArray as $k => $item) {
            if (empty($k)) continue;
            $line = $k + 1;
            list($idx, $rewardStage, $rewardIdx, $weight, $lastCnt, $isBonus) = $item;
            list($lvStage, $spinsStage, $ssStage) = $idxMapping[$idx];

            $rewardsTemp = $this->parseLineRewards( array_slice($item, 6), $line);

            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['rewards']['diceConf'][$rewardStage]['list'][$rewardIdx]= [
                'bonus' => $this->parseYesOrNo($isBonus),
                'weight' => $weight,
                'lastCnt' => $lastCnt,
                'prize' => $rewardsTemp,
            ];
        }

        foreach ($stagesProgressRewardArray as $k => $item) {
            if (empty($k)) continue;
            $line = $k + 1;
            list($idx, $id, $stepNum) = $item;
            list($lvStage, $spinsStage, $ssStage) = $idxMapping[$idx];

            $rewardStage = count($rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['rewards']['progressConf']) + 1;

            $rewardsTemp = $this->parseLineRewards( array_slice($item, 3), $line);

            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['rewards']['progressConf'][$rewardStage] = [
                'stage' => $rewardStage,
                'target' => $stepNum,
                'prize' => $rewardsTemp,
            ];
        }

//        $this->formatTokenGoodsSheet($basicConfig, $tokenGoodsArray, $idxMapping);

        $basicConfig['mayaWheelReward'] = $rewardsConfig;
    }
}