<?php

class MultiRoundTimeLimitedCardsModel extends ActBaseModel implements ActInterface
{
    static $islands = [];
    static $islandThemes = [];
    function serializeShowConf(array &$actConfig)
    {

    }

    function formatActData(array &$actConfig)
    {
        $this->checkConflictActs(TIME_LIMITED_CARD, $actConfig['start_time'], $actConfig['end_time']);

        $config = $actConfig['timeLimitedCards'];
        unset($actConfig['timeLimitedCards']);
        if (empty($config['themeId'])) $this->response(-1 * __LINE__, "开放主题不能为空");
        if (empty($config['endPreview'])) $this->response(-1 * __LINE__, "倒计时配置不能为空");
        $config['endPreview'] = $config['endPreview'] * 60;
        $config['repeatCardReward'] = json_decode(urldecode($config['repeatCardReward']), true);
        if (empty($config['repeatCardReward'])) $this->response(-1 * __LINE__, "卡片重复奖励不能为空");
        if (empty($config['cardSetRewardRatio'])) $this->response(-1 * __LINE__, "卡组集齐奖励增长系数不能为空");
        foreach ($config['cardSetRewardRatio'] as $cgid => $ratio){
            if (empty($ratio)) $this->response(-1 * __LINE__, "卡组:{$cgid} 集齐奖励增长系数不能为空");
        }

        if (empty($config['progress'])) $this->response(-1 * __LINE__, "阶段奖励不能为空");
        foreach ($config['progress'] as $k => $item) {
            if (empty($item['target'])) $this->response(-1 * __LINE__, "第{$item['stage']}阶段,阶段值不能为空");
            if (empty($item['progressRewardRatio'])) $this->response(-1 * __LINE__, "第{$item['stage']}阶段,每轮进度奖励增长系数不能为空");
            $prize = json_decode(urldecode($item['rewards']), true);
            if (empty($prize)) $this->response(-1 * __LINE__, "第{$item['stage']}阶段,奖励不能为空");
            $config['progress'][$k]['rewards'] = $prize;
        }

        $actConfig['extraData'] = json_encode($config, 32);
    }

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

    function importActData(array &$basicConfig,  $sheetArray)
    {
        $sheetIdx = 0;
        $basicConfArray = $sheetArray[$sheetIdx++];
        $cardRepeatArray = $sheetArray[$sheetIdx++];
        $cardsetRatioArray = $sheetArray[$sheetIdx++];
        $stagesRewardsArray = $sheetArray[$sheetIdx++];

        $themes = oo::commonOprDb('slavedb')->getAll('select * from ' . otable::cardgroup_limit_theme_config() . ' order by id desc');
        $themes = array_column($themes, null, 'theme_res');
        $themesName = array_keys($themes);
        $conf = [
            'themeId' => 0,
            'endPreview' => 0,
            'progress' => []
        ];
        $openTheme = [];
        // 解析基础配置
        foreach ($basicConfArray as $index => $oneLineInfo) {
            switch (trim($oneLineInfo[0])) {
                case "开放卡组":
                case "开放主题":
                case "开放卡组主题":
                    if (!in_array($oneLineInfo[1], $themesName) || empty($themes[$oneLineInfo[1]]['id'])) $this->response(__LINE__, '限时卡组主题不存在：' . $oneLineInfo[1]);
                    $conf['themeId'] = $themes[$oneLineInfo[1]]['id'];
                    $openTheme = $themes[$oneLineInfo[1]];
                    break;
                case "每轮进度奖励增长系数":
                    $conf['progressRewardRatio'] = intval($oneLineInfo[1]);
                    break;
                case "活动结束预告":
                case "活动结束预告(小时)":
                case "活动结束预告（小时）":
                    $conf['endPreview'] = floatval($oneLineInfo[1]);
                    break;
                default:
                    break;
            }
        }

        // 卡片重复奖励
        $conf['repeatCardReward'] = urlencode(json_encode($this->parseLineRewards($cardRepeatArray[1]), 32));

        // 卡组集齐奖励增长系数
        $cgids = json_decode($openTheme['cgids'],true);
        $cgidRatios = [];
        for($i = 1; $i<count($cardsetRatioArray); $i++){
            list($cgid, $ratio) = $cardsetRatioArray[$i];
            $cgidRatios[$cgid] = $ratio;
        }
        foreach ($cgids as $cgid){
            $conf['cardSetRewardRatio'][$cgid] = $cgidRatios[$cgid] ?? 0;
        }

        // 阶段配置
        foreach ($stagesRewardsArray as $k => $item) {
            if (empty($k)) continue;
            $filterLine = array_filter($item);
            if (empty($filterLine)) continue;
            $line = $k + 1;
            $rewardBasic = array_slice($item, 0, 3);
            $rewardsLine = array_slice($item, 3);
            list($stageIdx, $target, $progressRewardRatio) = $rewardBasic;
            $rewardsTemp = $this->parseLineRewards($rewardsLine, $line);
            $conf['progress'][$stageIdx] = [
                'stage' => $stageIdx,
                'target' => $target,
                'progressRewardRatio' => $progressRewardRatio,
                'rewards' => urlencode(json_encode($rewardsTemp)),
            ];
        }


        $basicConfig['timeLimitedCards'] = $conf;
    }


}