<?php

class LuckyScratchActModel extends ActBaseModel implements ActInterface
{

    function formatActData(array &$actConfig)
    {
        if (empty($actConfig['luckyScratchRewardConfig'])) $this->response(-1, '保存失败，奖励配置为空');

        $noEmptyFields = [
            'grid_num' => '总格数',
            'obtain_num' => '每次购买获得印章数',
            'rewards' => '奖励',
        ];
        foreach ($actConfig['luckyScratchRewardConfig'] as $lvIdx => &$lvItem) {
            if (empty($lvItem['stages'])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间配置为空");

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

                foreach ($spinItem['stages'] as $shushuIdx => &$shushuItem) {
                    if (empty($shushuItem['shushu'])) $shushuItem['shushu'] = [0];
                    if (empty($shushuItem['collect'])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数区间，配置为空");

                    foreach ($shushuItem['collect'] as $cIdx => &$cItem) {
                        $cItem['rewards'] = @json_decode(urldecode($cItem['rewards']), true) ?? [];

                        foreach ($noEmptyFields as  $field => $text){
                            if (empty($cItem[$field])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数区间 {$text}为空");
                        }
                    }
                }
            }
        }
        $actConfig['extraData'] = $actConfig['luckyScratchRewardConfig'];
        unset($actConfig['luckyScratchRewardConfig']);
    }

    function writeActCache(array $actConfig)
    {
        $conditionKeys = okeys::activityConditionKeys($actConfig['aid']);
        $stageRewardKeys = okeys::activityRewardsStageKeys($actConfig['aid']);
        $stageConfig = json_decode($actConfig['extraData'], true) ?? [];
        $expireTime = max($actConfig['end_time'], time() + 60);#redis最少一分钟
        foreach ($stageConfig as $idx1 => $item1) {
            $condition = [
                CONDITION_TYPE_LV => [
                    'shushu' => [],
                    'start' => $item1['start'],
                    'end' => $item1['end'],
                    'type' => CONDITION_TYPE_LV,
                ],
            ];
            foreach ($item1['stages'] as $idx2 => $item2) {
                $condition[CONDITION_TYPE_SPINS] = [
                    'shushu' => [],
                    'start' => $item2['start'],
                    'end' => $item2['end'],
                    'type' => CONDITION_TYPE_SPINS,
                ];

                foreach ($item2['stages'] as $idx3 => $item3) {
                    $condition[CONDITION_TYPE_SHUSHU] = [
                        'shushu' => $item3['shushu'],
                        'start' => 0,
                        'end' => 0,
                        'type' => CONDITION_TYPE_SHUSHU,
                    ];

                    $stageKey = "{$idx1}_{$idx2}_{$idx3}";
                    $stageConditions = array_values($condition);
                    unset($item3['shushu']);
                    oo::commonOprRedis('games')->hset($conditionKeys, $stageKey, json_encode($stageConditions, 32));
                    oo::commonOprRedis('games')->hset($stageRewardKeys, $stageKey, json_encode($item3['collect'], 32));
                }
            }
        }
        oo::commonOprRedis('games')->expireAt($conditionKeys, oo::redisRandomExpire($expireTime));
        oo::commonOprRedis('games')->expireAt($stageRewardKeys, oo::redisRandomExpire($expireTime));
    }

    function serializeShowConf(array &$actConfig)
    {
    }

    function importActData(array &$basicConfig, $sheetArray)
    {
        $sheetIdx = 0;
        $stagesMappingArray = $sheetArray[++$sheetIdx] ?? [];
        $rewardsArray = $sheetArray[++$sheetIdx] ?? [];
        list($rewardsConfig, $idxMapping) = $this->parseLvSpinsShushuStagesMapping($stagesMappingArray);


        # 解析奖励
        foreach ($rewardsArray as $k => $item) {
            $filterLine = array_filter($item);
            if (empty($filterLine)) continue;
            $line = $k + 1;
            if ($k == 0) continue;
            $rewardsLine = array_slice($item, 4);

            list($idx, $gridNum, $freeNum, $payNum) = $item;
            @list($lvIdx, $spinsIdx, $ssIdx) = $idxMapping[$idx];
            if (empty($rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx])) $this->response(-1 * __LINE__, "阶段奖励配置，第{$line}行,分组映射id:{$idx}不存在");


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

            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['collect'][0] = [
                'grid_num' => intval($gridNum),
                'begin_num' => intval($freeNum),
                'obtain_num' => intval($payNum),
                'rewards' => urlencode(json_encode($rewardsTemp)),
            ];
        }

        $basicConfig['luckyScratchRewardConfig'] = $rewardsConfig;

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

}