<?php

namespace loyalsoft;

/**
 * 任务步骤, 用于任务卡功能(三组合一)
 * @author gwang(wanggangzero@qq.com)
 */
class Ins_TaskStep_Active extends Object_ext {

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

    /**
     * @var type 查询用的id
     */
    public $typeId = 0;

    /**
     * @var int 计数器
     */
    public $cur = 0;

    /**
     * @var 2:进行中 3:完成未领取奖励 4 已经领取奖励
     */
    public $state = 0;

    /**
     * 领取奖励的时间
     * @var type
     */
    public $drawTs = 0;

    /**
     * @return \sm_task_step mo 获取对应的模板数据
     */
    public function mo() {
        $mo = GameConfig::activeTask_getItem($this->typeId);
        my_Assert(null != $mo, ErrCode::err_const_no);
        return $mo;
    }

    /**
     * @return int 计数最大值
     */
    public function max() {
        return $this->mo()->num;
    }

    /**
     * @return string[] 参数数组
     */
    private function paras() {
        return explode(',', $this->mo()->paras);
    }

    /**
     * @return float 当前进度
     */
    public function progress() {
        return $this->cur / $this->max();
    }

    /**
     * 是否完成
     * @return bool
     */
    public function isFinish() {
        return $this->cur >= $this->max();
    }

    /**
     * 构造函数
     * @param type $args
     */
    public function __construct($args) {
        if (isInt($args)) {
            $this->typeId = $args;
        } else {
            parent::__construct($args);
        }
    }

    /**
     * 是否状态型任务
     * @param type $cmd
     */
    function isStatusType() {
        return $this->mo()->cmd == Enum_ActiveTaskCmdType::SetSpecialQualGem || $this->mo()->cmd == Enum_ActiveTaskCmdType::DailyLogin #
               || $this->mo()->cmd == Enum_ActiveTaskCmdType::AnyEquipUpLevel_X || $this->mo()->cmd == Enum_ActiveTaskCmdType::AllEquipUpLevel_X || $this->mo()->cmd == Enum_ActiveTaskCmdType::OnLogin_X;
    }

    function calcStatusCur() {
        $mo = $this->mo();
        $paras = $this->paras();
        $para0 = "";
        $para1 = "";
        $para2 = "";

        if (count($paras) >= 1) {
            $para0 = $paras[0];
        }

        if (count($paras) >= 2) {
            $para1 = $paras[1];
        }

        if (count($paras) >= 3) {
            $para2 = $paras[2];
        }

        switch ($mo->cmd) {
            case Enum_ActiveTaskCmdType::DailyLogin:                            # 每日登录                 
                return 1;        
            case Enum_ActiveTaskCmdType::SetSpecialQualGem:                     # 镶嵌3个普通品质以上宝石 
                $gemEquip = ctx()->store->gemEquip;
                $num = 0;
                foreach ($gemEquip as $pag => $dic) {
                    foreach ($dic as $posId => $equip) {
                        if ($equip != null) {
                            foreach ($equip as $index => $gem) {
                                $ins_gem = new Ins_Gem($gem);
                                if ($ins_gem->mo()->qual >= $para0) {
                                    $num += 1;
                                }
                            }
                        }
                    }
                }

                return $num;
            case Enum_ActiveTaskCmdType::AnyEquipUpLevel_X:                     # 任意1件装备等级达到2     
                $num = 0;
                $equip = ctx()->store->equip;
                foreach ($equip as $index => $item) {
                    $ins_equip = new Ins_Equip($item);
                    if ($ins_equip->level >= $para0) {
                        $num += 1;
                    }
                }

                return $num;
            case Enum_ActiveTaskCmdType::AllEquipUpLevel_X:                     # 全身装备等级达到3级以上 
                $num = 0;
                $equip = ctx()->store->equip;
                foreach ($equip as $index => $item) {
                    $ins_equip = new Ins_Equip($item);
                    if ($ins_equip->level > $para0) {
                        $num += 1;
                    }
                }
                return $num;
            case Enum_ActiveTaskCmdType::OnLogin_X:                             # 第X天登录
                $day = 0;
                if (ctx()->task->day7_startTs > 0) {
                    $startDay = TimeUtil::totalDays(ctx()->task->day7_startTs);
                    $endDay = $startDay + 6;

                    if (TimeUtil::totalDays(now()) <= $endDay) {
                        $day = 0;
                        for ($i = $startDay; $i <= $endDay; $i++) {
                            $day += 1;
                            if (TimeUtil::totalDays(now()) == $i) {
                                break;
                            }
                        }                        
                    }
                }
                return $day;
            default:
                break;
        }
        return 0;
    }

    /**
     * 推进任务进度(采用和petmini相同的推进算法,2020年12月12日11:38:22)
     * @param Ins_TaskEventArgs $taskParam
     */
    public function propel($taskParam) {
//        var_dump($taskParam);
        switch ($taskParam->ope) {
            case Enum_PropelType::set:
                $this->cur = $taskParam->val;
                break;
            case Enum_PropelType::add:
                $this->cur += $taskParam->val;
                break;
            case Enum_PropelType::inc:
                $this->cur += 1;
                break;
            case Enum_PropelType::max:
                if ($taskParam->val > $this->cur) {
                    $this->cur = $taskParam->val;
                }
                break;
            case Enum_PropelType::stat:

                break;
        }
    }

}
