<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace loyalsoft;

/**
 * Description of Info_Gates
 *
 * @author c'y'zhao
 */
class Info_Gates extends Object_ext {

    /**
     * 当前剧情选择的关卡
     */
    public $CurrentGateId = 0;

    /**
     * 已解锁剧情关卡的最大id
     * @var type
     */
    public $UnlockedGatesMaxId = 0;

    /**
     * 最大通关id
     * @var type
     */
    public $maxPassGateId = 0;

    /**
     * 章节列表
     * @var Ins_GateInfo
     */
    public $GateList = null;

    /**
     * 挑战功能领取记录
     * @var type
     */
    public $GatesChallengeRecord = null;

    /**
     * 进化解锁记录
     * @var type
     */
    #[ArrayType]
    public $evolveUnlockRecord = array();

    /**
     * 巡逻领取奖励的时间
     * @var int
     */
    public $xunluo_StartTs = 0;

    /**
     * 快速巡逻的购买记录
     * @var int
     */
    public $xunluo_quick_buyRecord = 0;

    /**
     * 进化左边解锁最大值得记录
     * @var type
     */
    public $evolveMaxId_left = 0;

    /**
     * 进化右边解锁最大值得记录
     * @var type
     */
    public $evolveMaxId_right = 0;

    /**
     * 扫荡
     * @var type
     */
    public $fightSweepNum = 0;

    /**
     * 挑战关卡信息
     * @var Ins_TowerGateInfo
     */
    public $TowerGate;

    public function initialize() {
        $startId = glc()->FirstGateId;                                          # 初始化第一关的关卡id
        $this->CurrentGateId = $startId;
        $this->UnlockedGatesMaxId = $startId;

        $this->GateList = new \stdClass();
        $Ins_GateInfo = new Ins_GateInfo();
        $Ins_GateInfo->GateId = $this->CurrentGateId;
        $this->GateList->$startId = $Ins_GateInfo;

        $this->GatesChallengeRecord = new \stdClass();
        $this->xunluo_StartTs = now();
    }

    public function __construct($arg = null) {
        if ($arg == null) {
            if ($this->CurrentGateId == 0) {
                $startId = 1;
                $this->CurrentGateId = $startId;
                $this->UnlockedGatesMaxId = $startId;

                $this->GateList = new \stdClass();
                $Ins_GateInfo = new Ins_GateInfo();
                $Ins_GateInfo->GateId = $this->CurrentGateId;
                $this->GateList->$startId = $Ins_GateInfo;
            }

            $this->GatesChallengeRecord = new \stdClass();

            $this->xunluo_StartTs = now();
            $this->TowerGate = new Ins_TowerGateInfo();
            $this->TowerGate->skill_beidong = new \stdClass();
            $this->TowerGate->skill_zhudong = new \stdClass();
            $this->TowerGate->skill_zhudong_lockState = new \stdClass();
            $this->TowerGate->skill_beidong_lockState = new \stdClass();
            $this->TowerGate->CurLayer = 1;                                     // 默认从1层开始
        } else {
            parent::__construct($arg);

            $this->TowerGateInfo()->fixArray();
            if (null != $this->GateList) {
                foreach ($this->GateList as $id => &$gt) {
                    $gt = new Ins_GateInfo($gt);
                    $gt->fixArray();
                }
            }
        }
    }

    /**
     * 挑战关卡数据结构
     * @return Ins_TowerGateInfo
     */
    public function TowerGateInfo() {
        $this->TowerGate = new Ins_TowerGateInfo($this->TowerGate);
        return $this->TowerGate;
    }

    /**
     * 当前关卡信息
     * @return Ins_GateInfo
     */
    public function CurGateInfo() {
        $gateId = $this->CurrentGateId;
        my_Assert(StlUtil::dictHasProperty($this->GateList, $gateId), ErrCode::gate_NoUserGateInfo);
        $gateInfo = ctx()->gates->GateList->$gateId;
        return $gateInfo;
    }

    /**
     * @return int 解锁下一剧情关卡
     */
    public function UnlockNextPlotGate($gateId) {
        $nextId = $gateId;
        foreach (GameConfig::gate() as $k => $v) {
            isEditor() and $v = new \sm_gate();
            if ($v->unlockGateId == $gateId) {
                $nextId = $v->gateId;
            }
        }
//        echoLine("解锁下一关卡 $nextId");
        if (Ins_GateInfo::gateNum($this->UnlockedGatesMaxId) < Ins_GateInfo::gateNum($nextId)# 下一关尚未解锁
                && !StlUtil::dictHasProperty($this->GateList, $nextId)) {
//            $this->CurrentGateId = $nextId;                                   # 不再自动推到下一关, -2024.5.31
            $this->UnlockedGatesMaxId = $nextId;
            $gate = new Ins_GateInfo();
            $gate->GateId = $nextId;
            $this->GateList->$nextId = $gate;
        }

        return $nextId;
    }

    /**
     * @param int $gateId 通关的关卡id
     */
    public function UnlockMainChallengeGate($gateId) {
        $dic = GameConfig::gate();
        $index = 0;                                                   # 挑战关卡解锁逻辑. -gwang 2024年4月15日
        foreach ($dic as $id => $item) {
            if (Ins_GateInfo::GateTypeFromId($id) == Enum_GateType::MainChallengeGate #
                    && $item->challengeGateId == $gateId) {
                $index += 1;
                $gate = new Ins_GateInfo();
                $gate->GateId = $id;
                ctx()->gates->GateList->$id = $gate;
            }
            if ($index >= 3) {                                                  # 最多3个难度
                break;
            }
        }
    }

    /**
     * 最大通关id
     */
    public function maxPassGateNumId() {
        $maxIndex = 0;
        //$maxGateId = 0;
        foreach ($this->GateList as $gateId => $Ins_GateInfo) {
            if ($Ins_GateInfo->pass > 0 && Ins_GateInfo::gateNum($gateId) > $maxIndex) {
                $maxIndex = Ins_GateInfo::gateNum($gateId);
                //$maxGateId = $gateId;
            }
        }
        return $maxIndex;
    }
    
    /**
     * 最大通关id
     */
    public function maxPassGateId() {
        $maxIndex = 0;
        $maxGateId = 0;
        foreach ($this->GateList as $gateId => $Ins_GateInfo) {
            if ($Ins_GateInfo->pass > 0 && Ins_GateInfo::gateNum($gateId) > $maxIndex) {
                $maxIndex = Ins_GateInfo::gateNum($gateId);
                $maxGateId = $gateId;
            }
        }
        return $maxGateId;
    }
}
