import { Zone } from "../../../area/Area";
import i18n from "../../../i18n/i18n";
import Main from "../../../main/Main";
import { ReveData } from "../../../util/CHttp";
import CHttpEvent from "../../../util/CHttpEvent";
import FFCalAttr, { FFAttr } from "../FFCalAttr";

/**
 * 设置数据
 */
export interface SettingData {
    music_ON_OFF_1: boolean,
    music_Slider_1: number,
    music_ON_OFF_2: boolean,
    music_Slider_2: number,
}
/**
 * 账号数据
 */
export interface UserData {
    id: number
    token: string
    zone: Zone
}
/**
 * 角色数据
 */
export interface RoleAttr {
    id: string
    token: string
    name: string
    renameCount: number
    // level:number
    money: number
    gold: number
    zdl: number//战斗力

    icon: number//战队头像
    frame: number//战队框
    title: number//称号

    leader: number//队长

    power: number//体力

    openSkill: number//0,是否开启了技能

    // stageId:number//当前达到的关卡
}
export interface RoleIcon {
    data: Array<number>
    open: Array<number>
}
export interface FoodAttr {
    atk: number
    def: number
    hp: number
    sp: number
    water: number
    fire: number
    light: number
    earth: number
    dark: number
    wind: number
    thunder: number
    date: number
    count: number
}

/**
 * 道具
 */
export interface GoodAttr {
    count: number
}
/**
 * 伙伴
 */
export interface PetAttr {
    id: number,
    equips: Array<EquipAttr>
}
/**
 * 装备
 */
export interface EquipAttr {
    __index: number,
    PI: number
    id: number
    qhMax: number
    star: number
    attr: Array<any>
}
/**
 * 关卡中的获得物品记录
 * good = {
 *  "1009":1,
 *  "1010":1
 * }
 */
export interface StageGoodAttr {
    starCount: number,
    good: any
}
/**
 * 关卡
 */
export interface StageAttr {
    stageIndex: number,
    element: Array<string>;
    extraStage: Array<number>;
    data: Map<number, StageGoodAttr>;
}

/**
 * 神卡
 */
export interface GodCardAttr {
    id: number
    level: number //升星等级
    feature: number //获得的特征数量
    reborn: number //重生次数
}

/**
 * 角色数据
 */
export default class Player implements CHttpEvent {
    /**
     * 游戏系统设置
     */
    public setting: SettingData;
    /**
     * 角色基础数据
     */
    public role: RoleAttr;
    /**
     * 头像/框/称号
     */
    public roleIcon: RoleIcon;
    /**
     * 伙伴数据
     */
    public pet: Map<string, PetAttr>;
    /**
     * 装备
     */
    public equip: Array<EquipAttr>;
    /**
     * 神卡数据
     */
    public godCard: Map<string, GodCardAttr>;
    /**
     * 道具背包
     */
    public pack: Map<string, number>;
    /**
     * 关卡数据
     */
    public stage: StageAttr;
    /**
     * 吃食物增加属性
     */
    public foodAttr: FoodAttr;
    /**
     * 第一次充值赠送的id
     */
    public firstPayGave:Array<number>;
    public isNew: boolean = true;
    constructor(data: any) {
        this.readSetting();
        if (data) {
            this.role = data.role;
            this.foodAttr = data.foodAttr
            if (!this.role.name) {
                this.role.name = i18n.t('冒险者')+this.role.id
            }
            this.roleIcon = data.roleIcon
            this.pet = data.pet;
            this.godCard = data.godCard

            this.flushID();
            this.equip = data.equip;
            this.pack = data.pack;
            this.firstPayGave = data.firstPayGave;
            this.isNew = data.isNew;
            // cc.log('装备数量:',this.equip.length)
            this.flushIndex();
        }
    }
    private settingKey = '__setting';
    public readSetting() {
        let str = cc.sys.localStorage.getItem(this.settingKey)
        if (str) {
            this.setting = JSON.parse(str);
        } else {
            this.setting = {
                music_ON_OFF_1: true,
                music_Slider_1: 0.5,
                music_ON_OFF_2: true,
                music_Slider_2: 0.5,
            }
        }
    }
    public saveSetting() {
        cc.sys.localStorage.setItem(this.settingKey, JSON.stringify(this.setting));
    }

    private flushID() {
        for (const id in this.pet) {
            let pet = this.pet[id];
            pet.id = parseInt(id);
        }
    }
    public getPet(id) {
        return this.pet[id]
    }
    private flushIndex() {
        for (let i = 0; i < this.equip.length; i++) {
            let element = this.equip[i];
            element.__index = i;
        }
    }

    public getGoodCount(id) {
        let count = this.pack['' + id]
        if (count == undefined) {
            return 0
        }
        return count
    }

    /**
     * 监听网络
     * @param reveData 
     */
    public httpEvent(reveData: ReveData) {
        // cc.log('Player reve : ',this);
        // cc.log('reve : ',reveData);
        let data = reveData.data;
        //更新基础属性
        if (data._role != undefined) {
            if (data._role.power != undefined) {
                this.role.power = data._role.power;
            }
            if (data._role._money != undefined) {
                this.role.money += data._role._money;
            }
            if (data._role._gold != undefined) {
                this.role.gold += data._role._gold;
            }
            if (data._role.gold != undefined) {
                this.role.gold = data._role.gold;
            }
            if (data._role.renameCount != undefined) {
                this.role.renameCount += data._role.renameCount;
            }
            if(data._firstPayGave){
                this.firstPayGave = data._firstPayGave;
            }
        }
        //更新背包
        if (data._pack != undefined) {
            for (let i = 0; i < data._pack.length; i++) {
                const element = data._pack[i];
                let v = this.pack["" + element.id];
                if (v != undefined) {
                    v += element.count;
                    this.pack["" + element.id] = v;
                } else {
                    this.pack["" + element.id] = element.count;
                }
            }
        }
        //移除装备
        let isFlush = false;
        if (data._equipRemove != undefined) {
            let tmp = []
            for (let i = 0; i < this.equip.length; i++) {
                if (data._equipRemove.indexOf(i) < 0) {
                    const element = this.equip[i];
                    tmp.push(element)
                }
            }
            this.equip = tmp
            isFlush = true;
        }
        //添加装备
        if (data._equip != undefined) {
            for (let i = 0; i < data._equip.length; i++) {
                const element = data._equip[i];
                this.equip.push(element);
            }
            isFlush = true;
        }
        //添加头像/头像框/称号
        if (data._roleIcon != undefined) {
            for (let i = 0; i < data._roleIcon.length; i++) {
                const element = data._roleIcon[i];
                this.roleIcon.data.push(element);
            }
        }


        //  //更新装备
        //  if(data._equipUpdate != undefined){
        //     for (let i = 0; i < data._equipUpdate.length; i++) {
        //         const element = data._equipUpdate[i];
        //         this.equip.push(element);
        //     }
        //     isFlush = true;
        // }
        if (isFlush) {
            this.flushIndex();
        }
        //更新伙伴
        if (data._pet != undefined) {
            for (let id in data._pet) {
                let element: PetAttr = data._pet[id];
                let v = this.pet[id];
                if (v == undefined) {
                    this.pet[id] = element;
                    element.id = parseInt(id);
                } else {
                    v.equips = element.equips;
                }
            }
        }
        //添加神卡
        if (data._godCard != undefined) {
            for (let i = 0; i < data._godCard.length; i++) {
                const element = data._godCard[i];
                this.godCard[element.id] = element;
            }
        }
    }


    public getFight(main: Main): Array<FFAttr> {
        let fds = [];
        for (const id in this.pet) {
            let data = this.pet[id];
            fds.push(FFCalAttr.getAttr(main, data));
        }
        return fds;
    }
    /**
     * 刷新战斗力
     */
    public flushZdl(main) {
        let petMap = this.pet;
        let zdl = 0
        for (const id in petMap) {
            let petAttr = petMap[id];
            let attrData = FFCalAttr.getAttr(main, petAttr)
            zdl += attrData.zdl
        }
        this.role.zdl = zdl
        return zdl
    }

}