<?php

namespace loyalsoft;

/*
 * 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 CollegeInfo
 *
 * @author cyzhao
 */
class Info_College extends Object_ext {

    /**
     * 当前所在的级别
     */
    public $collegeId = 1;
    /// <summary>
    /// 所处的课程id
    /// </summary>
    public $courseId = 1001;

    /**
     * 将领取课程奖励的保存在这
     * @var type
     */
    #[ArrayType]
    public $comCourseList = array();
    /// <summary>
    /// 任务卡信息
    /// </summary>
    //public List<missCourseItem> compMissCardList = new List<missCourseItem>();
    public $compMissCardDic = null;

    /**
     * 当前课程进度，进入下一个课程清0
     * @var type
     */
    public $progress = 0;

    /**
     * 年级奖励领取记录
     * @var type
     */
    #[ArrayType]
    public $collegeRewardList = array();

    /**
     *
     * @var type
     */
    public $maskCollege = 0;

    /**
     *
     * @var type
     */
    public $ingTaskCardDic = null;

    /**
     * 圣哲学院功能结算时间
     * @var type
     */
    public $funUnluckTs = 0;

    /**
     * 累计积分
     * @var type
     */
    public $score = 0;

    /**
     *
     * @var type
     */
    #[ArrayType]
    public $scoreRewardList = array();

    /**
     * 构造函数
     * @param type $args
     */
    public function __construct($args = null) {
        if ($args == null) {
            $this->compMissCardDic = new \stdClass();
            $this->ingTaskCardDic = new \stdClass();
        } else {
            parent::__construct($args);
        }
    }

    /**
     * 废弃了
     */
    //public function initialize() {
//        $this->compMissCardDic = new \stdClass();
//        $dic = GameConfig::course_getItem($this->courseId);
//        $misslist = explode(',',$dic->misslist);
//        foreach ($misslist as $id) {
//            $item = new ins_missCourseItem();
//            $item->initialize($id);
//            $this->compMissCardDic->$id = $item;
//        }
    //$this->initCompMissCardDic();
    //}

    public function initCompMissCardDic() {
        $this->compMissCardDic = new \stdClass();
        $dic = GameConfig::course_getItem($this->courseId);
        $misslist = explode(',', $dic->misslist);
        foreach ($misslist as $id) {
            $item = new ins_missCourseItem();
            $item->initialize($id);
            $this->compMissCardDic->$id = $item;
        }
    }

    /**
     * 修改任务卡状态
     * @param type $id
     * @param type $state
     */
    public function modifyTaskCardState($id, $state) {
        $ingTaskCardDic = $this->ingTaskCardDic;
        foreach ($ingTaskCardDic as $collegeId => $courseDic) {
            foreach ($courseDic as $courseId => $cardDic) {
                foreach ($cardDic as $cardId => $val) {
                    if ($cardId == $id) {
                        TaskProc::OnFinishSchoolTask();
                        $this->ingTaskCardDic->$collegeId->$courseId->$cardId->type = $state;
                        break 3;
                    }
                }
            }
        }

//        if(!StlUtil::dictHasProperty($this->compMissCardDic, $id)){
//            return;
//        }
//        $this->compMissCardDic->$id->type = $state;
//        $this->progress += 1;
    }

    /**
     * 废弃
     */
    public function updataCourseNew() {
        //检测是否到下一年级
//        $collegeDic = GameConfig::college_getItem($this->collegeId);
//        if(in_array($this->courseId +1,explode(',', $collegeDic->cList))){
//            $this->courseId += 1;
//            $this->initCompMissCardDic();
//            $this->progress = 0;
//        } else {

        $this->collegeRewardList[] = $this->collegeId;
        //TaskProc::OnFinishAllSchoolTaskOfGrade($this->collegeId);

        $this->collegeId += 1;
        $newCollegeDic = GameConfig::college_getItem($this->collegeId);
        if ($newCollegeDic == null) {
            $this->maskCollege = Enum_College::finish;
            $this->collegeId -= 1;
        } elseif ($newCollegeDic != null && $newCollegeDic->cList == null) {
            $this->maskCollege = Enum_College::update;
        } else {
            $cList = explode(',', $newCollegeDic->cList);
            $this->courseId = $cList[0];
            $this->initCompMissCardDic();
        }
//        }
    }

    /**
     * 废弃
     */
    public function updataCourse() {
        $this->comCourseList[] = $this->courseId;
        //检测是否到下一年级
        $collegeDic = GameConfig::college_getItem($this->collegeId);
        if (in_array($this->courseId + 1, explode(',', $collegeDic->cList))) {
            $this->courseId += 1;
            $this->initCompMissCardDic();
            $this->progress = 0;
        }

//        else {
//            $this->collegeId += 1;
//            $newCollegeDic = GameConfig::college_getItem($this->collegeId);
//            $cList = explode(',', $newCollegeDic->cList);
//            $this->courseId = $cList[0];
//            $this->initCompMissCardDic();
//            $this->progress = 0;
//        }
    }

    //废弃
    public function checkCollege() {
        if ($this->maskCollege == Enum_College::update) {
            $dic = GameConfig::college_getItem($this->collegeId);
            if ($dic != null && $dic->cList != null) {
                $this->maskCollege = 0;
                $cList = explode(',', $dic->cList);
                $this->courseId = $cList[0];
                $this->initCompMissCardDic();
            }
        }
    }

//------------------new---------------------

    /**
     * 根据天 初始化
     * @return type
     */
    public function initTaskCard() {
        if ($this->funUnluckTs == 0) {
            return;
        }

        $day = TimeUtil::totalDays($this->funUnluckTs);
        $curDay = TimeUtil::totalDays(time());
        $num = $curDay - $day;

        $collegeMo = GameConfig::college();
        $college = (array) $collegeMo;
        ksort($college);

        $index = 0;
        foreach ($college as $collegeId => $collegeMo) {
            $courseArr = explode(',', $collegeMo->cList);

            if (!StlUtil::dictHasProperty($this->ingTaskCardDic, $collegeId)) {
                $this->ingTaskCardDic->$collegeId = new \stdClass();
            }
            $collegeDic = $this->ingTaskCardDic->$collegeId;
            foreach ($courseArr as $courseId) {
                if (!StlUtil::dictHasProperty($collegeDic, $courseId)) {
                    $mo = GameConfig::course_getItem($courseId);
                    $arr = explode(',', $mo->misslist);
                    $dic = new \stdClass();
                    foreach ($arr as $cardId) {
                        $item = new ins_missCourseItem();
                        $item->initialize($cardId);

                        $cid = StoreProc::PutTaskCardInStore($cardId);                      // 存入背包
                        NormalEventProc::OnTaskCard_Actived($cid, null);                        # 带入事件

                        $item->indexId = $cid;
                        $taskCard = new Ins_TaskCard(ctx()->store->taskcards->$cid); # 任务卡对象
                        $taskCard->state = Enum_TaskCardStateType::ing;

                        TaskProc::OnGetSchoolTask();
                        foreach ($taskCard->curSteps as &$tsp) {                                # 初期里面只有一个任务
                            $tsp = new Ins_TaskStep($tsp);
                            if ($tsp->isStatusType()) {                                         # 如果是道具收集类的, 扣除所收集道具
                                $tsp->autoCalcStatusCur();
                            }
                        }

                        if ($taskCard->IsFinish()) {                                            # 检查是否任务卡的所有步骤都已完成
                            $taskCard->state = Enum_TaskCardStateType::finish;
                            $item->type = Enum_TaskCardStateType::finish;
                            TaskProc::OnFinishSchoolTask();
                            NormalEventProc::OnTaskCard_Finish($taskCard->uid, null);           # 广播卡完成事件
                        }
                        $dic->$cardId = $item;
                        //统计
                        StatisticsProc::TargetStatistics(Enum_TargetStatistics::collegeCouseId_UnlockUserNum, $cardId);
                        ctx()->store->taskcards->$cid = $taskCard;
                    }
                    //$this->ingTaskCardDic->$courseId = $dic;
                    $collegeDic->$courseId = $dic;
                }
                $this->ingTaskCardDic->$collegeId = $collegeDic;
                if ($index >= $num) {
                    break 2;
                }
                $index += 1;
            }
        }
        ctx()->college->ingTaskCardDic = $this->ingTaskCardDic;

        //--------------------
        //$courseDic = GameConfig::course();
//        $index = 0;
//        foreach ($courseDic as $courseId => $mo) {
//            if(!StlUtil::dictHasProperty($this->ingTaskCardDic, $courseId)){
//                $arr = explode(',',$mo->misslist);
//                $dic = new \stdClass();
//                foreach ($arr as $cardId) {
//                    $item = new ins_missCourseItem();
//                    $item->initialize($cardId);
//
//                    $cid = StoreProc::PutTaskCardInStore($cardId);                      // 存入背包
//                    NormalEventProc::OnTaskCard_Actived($cid, null);                        # 带入事件
//
//                    $item->indexId = $cid;
//                    $taskCard = new Ins_TaskCard(ctx()->store->taskcards->$cid);# 任务卡对象
//                    $taskCard->state = Enum_TaskCardStateType::ing;
//
//                    TaskProc::OnGetSchoolTask();
//                    foreach ($taskCard->curSteps as &$tsp) {                                # 初期里面只有一个任务
//                        $tsp = new Ins_TaskStep($tsp);
//                        if ($tsp->isStatusType()) {                                         # 如果是道具收集类的, 扣除所收集道具
//                            $tsp->autoCalcStatusCur();
//                        }
//                    }
//
//                    if ($taskCard->IsFinish()) {                                            # 检查是否任务卡的所有步骤都已完成
//                        $taskCard->state = Enum_TaskCardStateType::finish;
//                        $item->type = Enum_TaskCardStateType::finish;
//                        TaskProc::OnFinishSchoolTask();
//                        NormalEventProc::OnTaskCard_Finish($taskCard->uid, null);           # 广播卡完成事件
//                    }
//                    $dic->$cardId = $item;
//                    ctx()->store->taskcards->$cid = $taskCard;
//                }
//                $this->ingTaskCardDic->$courseId = $dic;
//            }
//
//            if($index >= $num){
//                break;
//            }
//            $index += 1;
//        }
    }

    /**
     * 圣哲学院功能结算时间
     */
    public function setFunUnluckTs() {
        if ($this->funUnluckTs != 0) {
            return;
        }

        $this->funUnluckTs = time();
        $this->initTaskCard();
    }

    /**
     * 记录已经领取奖励的课程
     * @param type $id
     */
//    public function addReceiveCourseList($id) {
//        $this->comCourseList[] = $id;
//    }

    /**
     * 累计积分
     * @param type $val
     */
    public function addScore($val) {
        $this->score += $val;
    }

}
