<?php

class AdMissionsActModel extends ActBaseModel implements ActInterface
{

    function formatActData(array &$actConfig)
    {
        $config = $actConfig['adMissionsConfig'];
        unset($actConfig['adMissionsConfig']);
        $formatConfig = [];
        foreach ($config as  $lvItem){
            foreach ($lvItem['stages'] as  $spinsItem){
                $hasDefaultShushu = false;
                foreach ($spinsItem['stages'] as  $ssItem){
                    if(in_array(0, $ssItem['shushu']))$hasDefaultShushu = true;

                    if(empty($ssItem['stages']))$this->response(__LINE__, '层级配置为空', $spinsItem);
                    if(count($ssItem['stages']) != 3)$this->response(__LINE__, "层级数：".count($ssItem['stages'])."  不为 3", $spinsItem);

                    foreach ($ssItem['stages'] as $stage){
                        if(empty($stage['prize']))$this->response(__LINE__, '阶段值为空', $ssItem);
                        if(empty($stage['dst']))$this->response(__LINE__, '奖励为空', $ssItem);
                    }
                    $formatConfig[] = [
                        'lvStart' => $lvItem['lvStart'],
                        'lvEnd' => $lvItem['lvEnd'],

                        'spinsStart' => $spinsItem['spinsStart'],
                        'spinsEnd' => $spinsItem['spinsEnd'],
                        'shushu' => $ssItem['shushu'],
                        'stages' => $ssItem['stages'],
                    ];
                }
                if(!$hasDefaultShushu)$this->response(__LINE__, '未配置默认数数分组', $spinsItem);
            }
        }

        $actConfig['reward_conf'] = $formatConfig;
    }

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

        foreach ($rewardConf as $idx => $item) {
            $condition = $this->getStageConditionLvSpinsShushu($item, $item, $item);
            $stageConf = [
                'stages' => $item['stages'],
            ];
            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)
    {
        $rewardsConfig = [];
        $actConfig['reward_conf'] = json_decode($actConfig['reward_conf'], true);
        foreach ($actConfig['reward_conf'] as $item){
            $lvKey = "{$item['lvStart']}-{$item['lvEnd']}";
            $spinsKey = "{$item['spinsStart']}-{$item['spinsEnd']}";

            $lvIdx = $this->getGroupMappingIdx(CONDITION_TYPE_LV, $lvKey);
            $spinsIdx = $this->getGroupMappingIdx(CONDITION_TYPE_SPINS, $spinsKey, $lvIdx);
            $ssIdx = $this->getGroupMappingIdx(CONDITION_TYPE_SHUSHU, implode(',', $item['shushu']), "{$spinsIdx}_{$lvIdx}");

            if (empty($rewardsConfig[$lvIdx]))
                $rewardsConfig[$lvIdx] = [
                    'stages' => [],
                    'lvStart' => $item['lvStart'],
                    'lvEnd' => $item['lvEnd'],
                ];
            if (empty($rewardsConfig[$lvIdx]['stages'][$spinsIdx]))
                $rewardsConfig[$lvIdx]['stages'][$spinsIdx] = [
                    'stages' => [],
                    'spinsStart' =>  $item['spinsStart'],
                    'spinsEnd' => $item['spinsEnd'],
                ];

            if (empty($rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]))
                $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx] = [
                    'stages' => $item['stages'],
                    'shushu' =>  $item['shushu'],
                ];
        }
        $actConfig['reward_conf'] = json_encode($rewardsConfig);
    }

    function importActData(array &$basicConfig, $sheetArray)
    {
        $sheetIdx = 0;
        $stagesMappingArray = $sheetArray[++$sheetIdx] ?? [];
        $stagesProgressRewardArray = $sheetArray[++$sheetIdx] ?? [];


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

        foreach ($stagesProgressRewardArray as $k => $item) {
            if (empty($k)) continue;
            $line = $k + 1;
            list($idx, $dst) = $item;
            list($lvIdx, $spinsIdx, $ssIdx) = $idxMapping[$idx];

            $rewardsTemp = $this->parseLineRewards( array_slice($item, 2), $line);
            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stages'] = $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stages'] ?? [];

            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stages'][]= [
                'id' => count($rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stages']) + 1,
                'dst' => $dst,
                'prize' => $rewardsTemp,
            ];
        }

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