<?php

class DreamMissionsActModel extends ActBaseModel implements ActInterface
{

    function formatActData(array &$actConfig)
    {

        if(empty($actConfig['dreamMissionConfig']))$this->response(1, '操作失败，配置为空', $actConfig);

        $taskTypes = array_keys(self::$taskMapping);
        $gtype = ActBaseModel::getGtypeByActType($this->actType);
//        $gtype = oo::commonOprModel('newshop')->getGtypeByActType('dream_missions');
        if($gtype==0){
            $this->response(1, "保存失败，当前活动未配置商品类型");
        }
        foreach ($actConfig['dreamMissionConfig'] as $lvIdx => $itemLv) {
            if (empty($itemLv['stages']))
                $this->response(1, "保存失败，第{$lvIdx}层等级层，配置为空");


            foreach ($itemLv['stages'] as $spinsIdx => $itemSpins) {
                if (empty($itemSpins['stages']))
                    $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，配置为空");

                foreach ($itemSpins['stages'] as $ssIdx => &$itemSs) {

                    if (empty($itemSs['stages']))
                        $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，配置为空");
                    if (empty($itemSs['stages']['tasks']))
                        $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，任务配置为空");
//                    if (empty($itemSs['stages']['stages']))
//                        $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，阶段配置为空");
                    if (empty($itemSs['stages']['sumSpins']))
                        $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，体力总量为空");
                    foreach ($itemSs['stages']['tasks'] as $itemTask) {
                        $prize = @json_encode(urldecode($itemTask['prize'])) ?? [];
                        if(empty($prize)) $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，第{$itemTask['stage']}个任务，奖励为空");
                        if(!in_array($itemTask['taskId'],$taskTypes))
                            $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，第{$itemTask['stage']}个任务，任务类型异常：{$itemTask['taskId']}");
                        if(empty($itemTask['maxNum']))
                            $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，第{$itemTask['stage']}个任务，任务值为空", $itemTask);
//                        if(empty($itemTask['addRate']))
//                            $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，第{$itemTask['stage']}个任务，任务积分为空", $itemTask);
                    }
//                    foreach ($itemSs['stages']['stages'] as $itemStage) {
//                        $prize = @json_encode(urldecode($itemStage['prize'])) ?? [];
//                        if(empty($prize))
//                            $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，第{$itemStage['stage']}阶段，奖励为空");
//                        if(empty($itemStage['rate']))
//                            $this->response(1, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，第{$ssIdx}层数数层，第{$itemStage['stage']}阶段，阶段值为空", $itemStage);
//                    }
                    $actConfig['dreamMissionConfig'][$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['stages']['gtype']= $gtype;
                }
            }
        }
        $actConfig['reward_conf'] = $actConfig['dreamMissionConfig'];
        unset($actConfig['dreamMissionConfig']);
    }

    function writeActCache(array $actConfig)
    {
        $conditionKeys = okeys::activityConditionKeys($actConfig['aid']);
        $stageRewardKeys = okeys::activityRewardsStageKeys($actConfig['aid']);
        $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);
                    oo::commonOprRedis('games')->hset($stageRewardKeys, $idx, json_encode($item3['stages'], 32));
                    oo::commonOprRedis('games')->hset($conditionKeys, $idx, json_encode($condition, 32));
                }
            }
        }
        $expireTime = ActBaseModel::getActCacheExpireTime($actConfig['end_time']);#redis最少一分钟
        oo::commonOprRedis('games')->expireAt($conditionKeys, oo::redisRandomExpire($expireTime));
        oo::commonOprRedis('games')->expireAt($stageRewardKeys, oo::redisRandomExpire($expireTime));
        return true;
    }

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

    function importActData(array &$basicConfig, $sheetArray)
    {
        $sheetIdx = -1;
        $basicArray = $sheetArray[++$sheetIdx] ?? [];
        $stagesMappingSheet = $sheetArray[++$sheetIdx] ?? [];
        $taskSheet = $sheetArray[++$sheetIdx] ?? [];
        //$rewardSheet = $sheetArray[++$sheetIdx] ?? [];
        $sumSpinsSheet = $sheetArray[++$sheetIdx] ?? [];

        list($rewardsConfig, $idxMapping) = $this->parseLvSpinsShushuStagesMapping($stagesMappingSheet);
        $taskNameMapping = array_flip(self::$taskMapping);

        foreach($stagesMappingSheet as $k => $item){

            if($k==0) continue;

            list($lvStage, $spinsStage, $ssStage) = $idxMapping[$item[0]];
            if(empty($item[6])){
                $this->response(-1, "唯一id:{$item[0]},对应的价格不为空",[$item]);
            }
            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['price'] = $item[6];
            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['sku'] = $this->getSkuByPrice($item[6]);
        }

        foreach ($taskSheet as $k => $item) {
            if (empty($k) || empty($item)) continue;
            $line = $k + 1;
            list($idx, $taskName, $maxNum, $addRate) = $item;
            list($lvStage, $spinsStage, $ssStage) = $idxMapping[$idx];
            if (empty($idxMapping[$idx])) $this->response(-1, "第{$line}行,唯一id:{$idx} 无对应关系", $item);
            if (empty($taskNameMapping[$taskName]))$this->response(-1, "第{$line}行,任务类型:{$taskName} 不存在啊");

            $rewardsTemp = $this->parseLineRewards( array_slice($item, 4), $line);
            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['tasks'] = $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['tasks'] ?? [];
            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['tasks'][]= [
                'stage' => count($rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['tasks']) + 1,
                'taskId' => $taskNameMapping[$taskName],
                'maxNum' => $maxNum,
                'addRate' => $addRate,
                'prize' => $rewardsTemp,
            ];
        }

//        foreach ($rewardSheet as $k => $item) {
//            if (empty($k) || empty($item)) continue;
//            $line = $k + 1;
//            list($idx, $rate) = $item;
//            if (empty($idxMapping[$idx])) $this->response(-1, "第{$line}行,唯一id:{$idx} 无对应关系", $item);
//            list($lvStage, $spinsStage, $ssStage) = $idxMapping[$idx];
//
//            $rewardsTemp = $this->parseLineRewards( array_slice($item, 2), $line);
//            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['stages'] = $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['stages'] ?? [];
//            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['stages'][]= [
//                'stage' => count($rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['stages']) + 1,
//                'rate' => $rate,
//                'prize' => $rewardsTemp,
//            ];
//        }
        foreach ($sumSpinsSheet as $k => $item) {
            if (empty($k) || empty($item)) continue;
            list($idx, $sumSpins) = $item;
            list($lvStage, $spinsStage, $ssStage) = $idxMapping[$idx];
            $rewardsConfig[$lvStage]['stages'][$spinsStage]['stages'][$ssStage]['stages']['sumSpins'] = $sumSpins;
        }
        $basicConfig['dreamMissionConfig'] = $rewardsConfig;
    }

    public function getActBasicConfig(): array
    {
        return self::$taskMapping;
    }
}