<?php

class ClubFarmActModel extends ActBaseModel implements ActInterface
{

    const fieldLen = 3;

    function formatActData(array &$actConfig)
    {
        foreach ($actConfig['clubFarmConf']['intervals'] as $idx => &$item) {
            $line = $idx + 1;
            foreach ($item['farmland_config'] as $idx2 => $item2) {
                foreach ($item2['fields'] as $idx3 => $item3) {
                    if (empty($item3['prizes'])) {
                        $this->response(-1 * __LINE__, "第{$line}套配置中, 农场编号:{$idx2}, 菜地编号:{$idx3} 奖励配置为空");
                    }
                }
            }
            if (empty($item['user_rank'])) {
                $item['user_rank'] = [];
            }

            if (empty($item['big_reward']['prizes'])) {
                $this->response(-1 * __LINE__, "第{$line}套配置中, 大奖配置, 奖励配置为空");
            }

            $item['big_reward']['club_target'] = count($item['farmland_config']) * self::fieldLen;
        }

        $actConfig['extraData'] = json_encode($actConfig['clubFarmConf'], JSON_NUMERIC_CHECK);

        unset($actConfig['clubFarmConf']);
    }

    function writeActCache(array $actConfig)
    {
    }

    function serializeShowConf(array &$actConfig)
    {
    }

    function importActData(array &$basicConfig, $sheetArray)
    {
        if (count($sheetArray) < 7) {
            $this->response(-1 * __LINE__, "工作表数量不匹配");
        }

        $sheet2 = $sheetArray[1] ?? [];
        $sheet3 = $sheetArray[2] ?? [];
        $sheet4 = $sheetArray[3] ?? [];
        $sheet5 = $sheetArray[4] ?? [];
        $sheet6 = $sheetArray[5] ?? [];
        $sheet7 = $sheetArray[6] ?? [];

        $collectConf = $this->parseCollectSheet($sheet2);
        $clubRankConf = $this->parseClubRankSheet($sheet3);
        $stageConf = $this->parseStageSheet($sheet4);
        $rewardConf = $this->parseRewardSheet($sheet5);
        $userRankConf = $this->parseUserRankSheet($sheet6);
        $bigRewardConf = $this->parseBigRewardSheet($sheet7);

        $basicConfig['clubFarmConf'] = $this->packageConf($collectConf, $clubRankConf, $stageConf, $rewardConf, $userRankConf, $bigRewardConf);
    }

    private function parseCollectSheet(array $sheet): array
    {
        $baseConfig = ["preTimes" => 0, "score_config" => []];
        foreach ($sheet as $item) {
            $label = trim($item[0]);
            $value = intval($item[1]);
            switch ($label) {
                case '普通偷':
                    $baseConfig['score_config']['steal_break'] = $value;
                    break;
                case '完美偷':
                    $baseConfig['score_config']['steal_success'] = $value;
                    break;
                case '打成功':
                    $baseConfig['score_config']['attack_success'] = $value;
                    break;
                case '打到盾':
                    $baseConfig['score_config']['attack_break'] = $value;
                    break;
                case '提前预告时长':
                    $baseConfig['preTimes'] = $value;
                    break;
            }
        }

        return $baseConfig;
    }

    private function parseClubRankSheet(array $sheet): array
    {
        $clubRankPrize = [];
        for ($i = 1; $i < count($sheet); $i++) {
            $line = $i + 1;
            $clubRankPrize[] = [
                "prizes" => $this->parseLineRewards(array_slice($sheet[$i], 1), $line),
            ];
        }

        return $clubRankPrize;
    }

    private function parseStageSheet(array $sheet): array
    {
        $layer = [];
        for ($i = 1; $i < count($sheet); $i++) {
            list($id, $start, $end, $limit) = $sheet[$i];

            $layer[] = ["id" => $id, "start" => $start, "end" => $end, "min_scores" => $limit];
        }

        return $layer;
    }

    private function parseRewardSheet(array $sheet): array
    {
        $rewardArrMap = [];

        $fieldsArr = [];
        for ($i = 1; $i < count($sheet); $i++) {
            $line = $i + 1;

            $oneStage = ["target" => 0];

            list($id, $seq, $oneStage['target']) = $sheet[$i];
            $oneStage['prizes'] = $this->parseLineRewards(array_slice($sheet[$i], 3), $line);

            $fieldsArr[] = $oneStage;
            if ($seq % self::fieldLen == 0) {
                $rewardArrMap[$id][] = ["fields" => $fieldsArr];
                $fieldsArr = [];
            }
        }

        return $rewardArrMap;
    }

    private function parseUserRankSheet(array $sheet): array
    {
        $rewardArrMap = [];
        for ($i = 1; $i < count($sheet); $i++) {
            $line = $i + 1;

            $oneStage = [];

            list($id, $rank) = $sheet[$i];
            $oneStage['prizes'] = $this->parseLineRewards(array_slice($sheet[$i], 2), $line);

            $rewardArrMap[$id][] = $oneStage;
        }

        return $rewardArrMap;
    }

    private function parseBigRewardSheet(array $sheet): array
    {
        $rewardArrMap = [];
        for ($i = 1; $i < count($sheet); $i++) {
            $line = $i + 1;

            $oneStage = ["club_target" => 0];

            list($id) = $sheet[$i];
            $oneStage['prizes'] = $this->parseLineRewards(array_slice($sheet[$i], 1), $line);

            $rewardArrMap[$id] = $oneStage;
        }

        return $rewardArrMap;
    }

    private function packageConf(array $collectConf, array $clubRankConf, array $stageConf, array $rewardConf, array $userRankConf, array $bigRewardConf): array
    {
        $clubFarmConf = $collectConf;
        $clubFarmConf['club_rank'] = $clubRankConf;
        foreach ($stageConf as $item) {
            $id = $item['id'];
            unset($item['id']);

            $one = $item;
            $one['farmland_config'] = $rewardConf[$id];
            if (empty($one['farmland_config'])) {
                $this->response(-1 * __LINE__, "唯一ID:{$id}, 田地奖励配置不存在");
            }
            $one['user_rank'] = $userRankConf[$id] ?? [];

            $one['big_reward'] = $bigRewardConf[$id];
            if (empty($one['big_reward'])) {
                $this->response(-1 * __LINE__, "唯一ID:{$id}, 大奖奖励配置不存在");
            }

            $clubFarmConf['intervals'][] = $one;
        }

        return $clubFarmConf;
    }

}