<?php
// 卡牌之王-大奖版， 传说版
class KingOfCardActModel extends ActBaseModel implements ActInterface
{
    function formatActData(array &$actConfig)
    {

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

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

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

                foreach ($itemSpins['stages'] as $ssIdx => &$itemSs) {
                    if (empty($itemSs['progress']))$this->response(__LINE__, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，，第{$ssIdx}层体力层，阶段配置为空");
                    foreach ($itemSs['progress'] as $progressIdx => &$progress){
                        if (empty($progress['target']))$this->response(__LINE__, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，，第{$ssIdx}层体力层，第{$progressIdx}阶段, 阶段值为空");
                        if (empty($progress['prize']))$this->response(__LINE__, "保存失败，第{$lvIdx}层等级层，第{$spinsIdx}层体力层，，第{$ssIdx}层体力层，第{$progressIdx}阶段, 奖励为空");
                    }
//                    $itemSs['progress'] = array_values($itemSs['progress']);
                }
            }
        }

        $actConfig['reward_conf'] = $actConfig['kingOfCardsConfig'];
        unset($actConfig['kingOfCardsConfig']);
    }

    function writeActCache(array $actConfig)
    {

        $conditionKeys = okeys::activityConditionKeys($actConfig['aid']);
        $stageRewardKeys = okeys::activityRewardsStageKeys($actConfig['aid']);
        $rewardConf = json_decode($actConfig['reward_conf'], 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['progress'], 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] ?? [];   // 分层关系配置
        $rewardSheet = $sheetArray[++$sheetIdx] ?? [];      //普通配置

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

        $this->parseProgressSheet($rewardsConfig, $idxMapping, $rewardSheet);

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


    // 进度
    public function parseProgressSheet(&$rewardsConfig, $idxMapping, $sheetData){
        $sheetCnt = count($sheetData);
        $collectIdxes = [0,1,2,3];
        for($i = 1;$i < $sheetCnt;$i++){
            list($idx, $target) = $sheetData[$i];
            $lineNum = $i+1;
            if (empty($idxMapping[$idx])) $this->response(__LINE__, "第{$lineNum}行,唯一id:{$idx} 无对应关系", $sheetData[$i]);
            list($lvIdx, $spinsIdx, $ssIdx) = $idxMapping[$idx];
            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['progress'] = $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['progress'] ?? [];

            $prizes = $this->parseLineRewards( array_slice($sheetData[$i], 2), $lineNum);

            $stage = 1 + count($rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['progress']);
            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['progress'][$stage] = [
                "stage" => $stage,
                "target" => $target,
                'prize' => $prizes,
            ];
        }
    }


}