/*
 * @Author: YeeChan
 * @Date: 2021-12-30 14:42:48
 * @Description: 体力管理器
 */

import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import GameMgr from "../../FrameWork/Mgr/GameMgr";
import Common5 from "../../Platform/th/Common5";


export default class PhysicalPowerManger {

    /**多少秒恢复一个体力值 */
    public static RecoverPhysicalTime: number = 300;
    /**最大的体力值 */
    public static MaxPhysical: number = 3;

    private static physicalNum: number = 0;//当前的体力值
    private static physicalTime: number = 0;//上一次体力恢复的时间
    private static physicalAllNum: number = 0;//无限体力看得视频数量
    private static physicalAllTime: number = 0;//无限体力 时间点

    //定时器
    private static interval = null;
    /**
     * 初始数据
     * @param data 
     */
    public static initData(data: any) {
        if (data.physicalNum == undefined) {
            this.physicalNum = Common5.fisrtPower;
            this.physicalTime = this.getNowTime();
        } else {
            this.physicalNum = this.verify(data.physicalNum, 0);
            this.physicalTime = this.verify(data.physicalTime, 0);
        }
        this.physicalAllNum = this.verify(data.physicalAllNum, 0);
        this.physicalAllTime = this.verify(data.physicalAllTime, 0);

        if (this.interval) {
            clearInterval(this.interval);
            this.interval = null;
        }
        this.interval = setInterval(() => {
            this.updatePhysicalTime();
        }, 1000)
        this.updatePhysicalTime();
    }

    /**
     * 获取所有数据
     * @returns 
     */
    public static getData() {
        let tab = { physicalNum: this.physicalNum, physicalTime: this.physicalTime, physicalAllNum: this.physicalAllNum, physicalAllTime: this.physicalAllTime };
        return tab;
    }


    /**
     * 检查判断是否可以使用足够体力
     */
    public static isCanUserPhysical(num: number = 1) {
        let isCanUser = false;
        if (this.getPhyVideoAllTime() > 0) {//无限体力
            this.updatePhysicalTime();
            isCanUser = true;
        } else {
            let _cnum = this.getPhysicalNum() - num;
            if (_cnum < 0) {//体力不足够
                isCanUser = false;
            } else {
                if (this.getPhyVideoTime() <= 0) {//计时
                    this.setPhyVideoTime(this.getNowTime());
                }
                this.setPhysicalNum(this.getPhysicalNum() - num);
                GameMgr.getInstance_custom().saveGameData_custom();
                this.updatePhysicalTime();
                isCanUser = true;
            }
        }
        Common5.isNoPower = !isCanUser;
        return isCanUser;
    }


    /**
    * 获取体力的数量
    * @returns 
    */
    public static getPhysicalNum() {
        return this.physicalNum;
    }
    /**
     * 设置体力的数量
     * @returns 
     */
    public static setPhysicalNum(num: number) {
        this.physicalNum = num;
        this.updatePhysicalTime();
    }


    /**
     * 获取无限体力看视频的次数
     * @returns 
     */
    public static getPhyVideoAllNum() {
        return this.physicalAllNum;
    }

    /**
     * 设置无限体力看视频的次数
     * @param num 
     */
    public static setPhyVideoAllNum(num: number) {
        this.physicalAllNum = num;
    }

    /**
     * 无限体力的时间
     * @param num 
     * @returns 
     */
    public static getPhyVideoAllTime() {
        return this.physicalAllTime;
    }

    /**
     * 无限体力的时间
     * @param num 
     */
    public static setPhyVideoAllTime(num: number) {
        this.physicalAllTime = num;
    }

    /**
     * 普通体力恢复的时间
     * @param num 
     * @returns 
     */
    public static getPhyVideoTime() {
        return this.physicalTime;
    }

    /**
     * 普通体力恢复的时间
     * @param num 
     */
    public static setPhyVideoTime(num: number) {
        this.physicalTime = num;
    }

    /**
     * 更新体力数据 需要放入定时器中
     */
    public static updatePhysicalTime() {
        let again = false;
        if (this.getPhyVideoAllTime() > 0) {//无限体力中

            let time = this.getPhyVideoAllTime();
            let ntime = new Date().getTime();
            let cctime = time - ntime;
            if (cctime <= 0) {
                this.setPhyVideoAllTime(0);
                this.setPhysicalNum(this.MaxPhysical)
                this.setPhyVideoTime(0);
                GameMgr.getInstance_custom().saveGameData_custom();

                again = true;
            }
        } else {//普通体力
            if (this.getPhysicalNum() < this.MaxPhysical) {
                let time = this.getPhyVideoTime();
                let ntime = new Date().getTime();
                let maxTime = this.RecoverPhysicalTime * 1000
                if (ntime - time >= maxTime) {
                    time = time + maxTime;
                    this.setPhyVideoTime(time);
                    let num = this.getPhysicalNum() + 1;
                    if (num >= this.MaxPhysical) {
                        num = this.MaxPhysical;
                        this.setPhyVideoTime(0);
                    }
                    this.setPhysicalNum(num)
                    GameMgr.getInstance_custom().saveGameData_custom();
                    again = true;
                }
            } else {
                this.setPhyVideoTime(0);
            }
        }
        EventMgr.emitEvent_custom(ryw_Event.updatePhysicalPower);
        if (again) {
            this.updatePhysicalTime();
        }
    }


    /**
     * 增加一个新的无限体力24小时
     * @param show 
     */
    public static addNewPhysicalAll() {
        //增加时间
        let ctime = this.getNowTime() + (24 * 60 * 60 * 1000);
        if (this.getPhyVideoAllTime() > 0) {
            ctime = this.getPhyVideoAllTime() + (24 * 60 * 60 * 1000);
        }
        this.setPhyVideoAllTime(ctime)
        GameMgr.getInstance_custom().saveGameData_custom();
        this.updatePhysicalTime();
    }

    /**
        * 获取当前的时间戳
        */
    public static getNowTime(): number {
        return (new Date()).getTime();
    }


    /**
     * 数据校验
     * @param data 校验的数据
     * @param defaultValue 默认值
     */
    public static verify(data: any, defaultValue: any) {
        if (data !== undefined) {
            return data;
        }
        return defaultValue;
    }

    /**
     * 格式化时间获取:时分秒 00:00
     * @param {number} 时间秒
     */
    public static formatTime3(time: number): string {

        let str: string = "";

        let m: number = time / 60;
        m = parseInt(m + "");
        let s: number = time - m * 60;
        s = parseInt(s + "");

        if (m > 9) {
            str += m + ":";
        }
        else {
            str += "0" + m + ":";
        }
        if (s > 9) {
            str += s;
        }
        else {
            str += "0" + s;
        }
        return str;
    }




    /**
     * 格式化时间获取:时分秒 00:00:00
     * @param {number} 时间戳差值(ms)
     */
    public static formatTime(time: number): string {
        let str: string = "";
        let h: number = time / 3600;
        h = parseInt(h + "");
        let m: number = (time - h * 3600) / 60;
        m = parseInt(m + "");
        let s: number = time - h * 3600 - m * 60;
        s = parseInt(s + "");
        if (h > 0) {
            str += h + ":";
        }
        if (m > 9) {
            str += m + ":";
        }
        else {
            str += "0" + m + ":";
        }
        if (s > 9) {
            str += s + "";
        }
        else {
            str += "0" + s;
        }
        return str;
    }




}