<?php

class SurpriseCollectModel extends ActBaseModel implements ActInterface
{


    function serializeShowConf(array &$actConfig)
    {

    }

    function formatActData(array &$actConfig)
    {
        if (empty($actConfig['surpriseCollectConfig'])) $this->response(-1, '保存失败，奖励配置为空');
        foreach ($actConfig['surpriseCollectConfig'] as $lvIdx => &$lvItem) {
            $lvStart = intval($lvItem['start']);
            $lvEnd = intval($lvItem['end']);
            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];
                    $shushuItem['lastPrize'] = @json_decode(urldecode($shushuItem['lastPrize']), true);
                    if (empty($shushuItem['list'])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,阶段配置为空");
                    if (empty($shushuItem['lastPrize'])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,最终大奖配置为空");
                    if (count($shushuItem['lastPrize']) > 2) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,最终大奖数量不能超过2个");
                    if (empty($shushuItem['sku'])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,解锁价格为空", $shushuItem);
                    foreach ($shushuItem['list'] as $stageIdx => &$tageItem) {
                        $tageItem['paiedPrize'] = @json_decode(urldecode($tageItem['paiedPrize']), true);
                        $tageItem['freePrize'] = @json_decode(urldecode($tageItem['freePrize']), true);
                        if (empty($tageItem['freePrize'])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,第{$stageIdx}阶段,免费奖励配置为空", $tageItem);
                        if (count($tageItem['freePrize']) > 1) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,第{$stageIdx}阶段,免费奖励数量不能超过1个", $tageItem);
                        if (empty($tageItem['paiedPrize'])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,第{$stageIdx}阶段,付费奖励配置为空", $tageItem);
                        if (count($tageItem['paiedPrize']) > 1) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,第{$stageIdx}阶段,付费奖励数量不能超过1个", $tageItem);
                        if (empty($tageItem['stepnum'])) $this->response(-1, "保存失败，第{$lvIdx}层等级区间,第{$spinIdx}层体力区间,第{$shushuIdx}层数数分组区间,第{$stageIdx}阶段,阶段积分为0", $tageItem);
                    }
                }
            }
        }
        $actConfig['extraData'] = $actConfig['surpriseCollectConfig'];
        unset($actConfig['surpriseCollectConfig']);
    }

    function writeActCache(array $actConfig)
    {
        $this->writeActCacheCommonLvSpinsShushu($actConfig, 'extraData');
    }

    function importActData(array &$basicConfig, $dataArray)
    {

        $sheetIdx = 0;
        $basicConfArray = $dataArray[$sheetIdx++] ?? [];
        $stagesMappingArray = $dataArray[$sheetIdx++] ?? [];
        $stagesRewardsArray = $dataArray[$sheetIdx++] ?? [];
        $lastRewardsArray = $dataArray[$sheetIdx++] ?? [];


        # 解析映射关系
        list($rewardsConfig, $idxMapping) = $this->parseLvSpinsShushuStagesMapping($stagesMappingArray);


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

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

            $stageCnt = count($rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['list']);
            if ($stageCnt + 1 != $stageIdx) $this->response(-1 * __LINE__, "阶段奖励配置，第{$line}行,阶段序号不连续");
            $rewardsTemp = $this->parseLineRewards($rewardsLine, $line);
            if (count($rewardsTemp) != 2) $this->response(-1 * __LINE__, "阶段奖励配置，第{$line}行,奖励个数配置异常");

            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['list'][$stageIdx] = [
                'stage' => $stageIdx,
                'stepnum' => $target,
                'freePrize' => urlencode(json_encode([$rewardsTemp[0]])),
                'paiedPrize' => urlencode(json_encode([$rewardsTemp[1]])),
            ];
        }

        # 解析最终大奖
        foreach ($lastRewardsArray as $k => $item) {
            $filterLine = array_filter($item);
            if (empty($filterLine)) continue;
            $line = $k + 1;
            if ($k == 0) continue;
            $rewardBasic = array_slice($item, 0, 2);
            $rewardsLine = array_slice($item, 2);

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

            $sku = $this->getSkuByPrice($price);
            if (empty($sku)) $this->response(-1 * __LINE__, "最终奖励配置，第{$line}行,价格：{$price} 无对应sku", $item);

            $rewardsTemp = $this->parseLineRewards($rewardsLine, $line);
            if (count($rewardsTemp) > 2) $this->response(-1 * __LINE__, "最终奖励配置，第{$line}行,最终奖励个数不能超过两个");

            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['price'] = $price;
            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['sku'] = $sku;
            $rewardsConfig[$lvIdx]['stages'][$spinsIdx]['stages'][$ssIdx]['lastPrize'] = urlencode(json_encode($rewardsTemp));
        }


        $basicConfig['surpriseCollectConfig'] = $rewardsConfig;

    }

}