import { Vec3 } from "cc";
import { msgac } from "../data/msgac";
import { eventManager } from "../manager/eventManager";
import { localStorageManager } from "../manager/localStorageManager";

export class playerModel {
    private static _instance: playerModel;
    private constructor() { }
    public static get instance(): playerModel {
        if (!this._instance) {
            this._instance = new playerModel();
        }
        return this._instance;
    }

    data: any;

    // 金币飘动终点位置
    coinPosEnd: Vec3;

    init() {
        let data: any = localStorageManager.instance.getGameData();
        this.data = data;

        if (!data.level) {
            data.level = 1;
        }
        if (!data.levelAct) {
            // 实际关卡数据序号
            data.levelAct = 1;
        }
        if (!data.coin) {
            data.coin = 0;
        }
        if (!data.roleId) {
            data.roleId = 1;
        }
        if (!data.skillObj) {
            data.skillObj = {};
        }
        if (!data.levelEspIdArr) {
            data.levelEspIdArr = [];
        }
        if (!data.turnTableAwardIdArr) {
            data.turnTableAwardIdArr = [];
        }
        if (!data.equipWeaponId) {
            data.equipWeaponId = 1011;
        }
        if (!data.equipDressId) {
            data.equipDressId = 2011;
        }
        if (!data.equipIdArr) {
            // 默认有1级武器和1级装备
            data.equipIdArr = [data.equipWeaponId, data.equipDressId];
        }
    }

    getEquipDressId() {
        return this.data.equipDressId;
    }
    setEquipDressId(id: number) {
        this.data.equipDressId = id;
        this.saveData();
    }

    getEquipWeaponId() {
        return this.data.equipWeaponId;
    }
    setEquipWeaponId(id: number) {
        this.data.equipWeaponId = id;
        this.saveData();
    }

    getEquipIdArr() {
        return this.data.equipIdArr;
    }
    addEquipId(id: number) {
        if (this.data.equipIdArr.indexOf(id) != -1) {
            return;
        }
        this.data.equipIdArr.push(id);
        this.saveData();
    }

    // 是否已展示过引导小手 1:已显示过了
    getHandShow() {
        return this.data.handShow;
    }
    setHandShow(hand: number) {
        this.data.handShow = hand;
        this.saveData();
    }

    resetTurnTableAward() {
        this.data.turnTableAwardIdArr = [];
        this.saveData();
    }
    getTurnTableAwardIdArr() {
        return this.data.turnTableAwardIdArr;
    }
    addTurnTableAwardId(id: number) {
        if (this.data.turnTableAwardIdArr.indexOf(id) != -1) {
            return;
        }
        this.data.turnTableAwardIdArr.push(id);
        this.saveData();
    }

    getTurnTableFreeTime() {
        let turnTableFreeTime = this.data.turnTableFreeTime;
        if (typeof (turnTableFreeTime) == "string") {
            return new Date(turnTableFreeTime);
        }
        if (typeof (turnTableFreeTime) == "object") {
            return turnTableFreeTime;
        }

        return null;
    }
    setTurnTableFreeTime(date: object) {
        this.data.turnTableFreeTime = date;
        this.saveData();
    }

    isLevelEspUnlock(id: number) {
        if (this.data.levelEspIdArr.indexOf(id) != -1) {
            return true;
        }
        return false;
    }
    unlockLevelEsp(id: number) {
        if (this.data.levelEspIdArr.indexOf(id) == -1) {
            this.data.levelEspIdArr.push(id);
            this.saveData();
        }
    }

    getSkillById(id: number) {
        let skill = this.data.skillObj[id];
        if (!skill) {
            skill = {};
            skill.id = id;
            skill.num = 0;
        }
        return skill;
    }
    addSkillNumById(id: number, num?: number) {
        if (!num) {
            num = 1;
        }
        let skill = this.data.skillObj[id];
        if (!skill) {
            skill = {};
            skill.id = id;
            skill.num = 0;
            this.data.skillObj[id] = skill;
        }
        skill.num += num;
        this.saveData();
    }
    subSkillNumById(id: number) {
        let skill = this.data.skillObj[id];
        if (!skill) {
            return;
        }
        skill.num--;
        this.saveData();
    }

    getRoleId() {
        return this.data.roleId;
    }
    setRoleId(roleId: number) {
        this.data.roleId = roleId;
        this.saveData();
    }

    getLevel() {
        return this.data.level;
    }
    setLevel(level: number) {
        this.data.level = level;
        this.saveData();
    }

    getLevelAct() {
        return this.data.levelAct;
    }
    setLevelAct(levelAct: number) {
        this.data.levelAct = levelAct;
        this.saveData();
    }

    saveData() {
        localStorageManager.instance.setGameData(this.data);
    }

    getCoin() {
        return this.data.coin;
    }
    addCoin(num: number) {
        this.data.coin += num;
        this.saveData();

        // eventManager.instance.send(msgac.showNotice, localText.textObj.coin + "+" + num);
        eventManager.instance.send(msgac.coinChange);
    }
    subCoin(num: number) {
        this.data.coin -= num;
        this.saveData();

        // eventManager.instance.send(msgac.showNotice, localText.textObj.coin + "-" + num);
        eventManager.instance.send(msgac.coinChange);
    }

}

