import i18n from "../../../i18n/i18n";
import { ReveData } from "../../../util/CHttp";
import { FFAttr } from "../FFCalAttr";

/**
 * 主角信息
 */
export interface __RoleData {
    id: number,
    name: string,
    icon: string,
    act: string,

    post: number,
    group: number,

    atk: number,
    def: number,
    hp: number,
    sp: number,

    skill: number,
}
//头像/框/称号
export interface __RoleIcon {
    id: number,
    type: number,
    name: string,
    icon: string,
    about: string,

    atk: number,
    def: number,
    hp: number,
    sp: number,
}
/**
 * 道具
 */
export interface __GoodData {
    id: number,
    name: string,
    type: number,
    pz: number,

    icon: string,
    atk: number,
    def: number,
    hp: number,

    sp: number,
    water: number,
    fire: number,
    light: number,

    earth: number,
    dark: number,
    wind: number,
    thunder: number,

    howGet: number,
    about: string,
    sell: number,
    buy: number,
}
/**
 * 装备
 */
export interface __EquipData {
    id: number,
    name: string,
    about: string,
    level: number,//当前最高强化等级

    icon: string,
    bulletEffect: string,
    pz: number,
    occ: number,

    atk: number,
    def: number,
    hp: number,
    sp: number,

    sd: number,
    type: number,

    suit: number
    skill: number
    qh_exp: number//消耗后获得的强化经验
    qh_max: number//强化突破次数

}
/**
 * 装备强化
 */
export interface __EquipQHData {
    id: number
    type: number
    occ: number
    level: number
    exp: number
    atk: number
    def: number
    hp: number
    sp: number
    money: number
}
/**
 * 装备套装
 */
export interface __EquipSuitData {
    id: number
    name: string
    about: string
    suitId: number
    effect: string
    value1_1: number
    value1_2: number
    value1_3: number

    value2_1: number
    value2_2: number
    value2_3: number
}
/**
 * 技能
 */
export interface __SkillData {
    id: number,
    name: string,
    about: string,
    type: number
    icon: string,
    effect: string,
    time: number,
    value1: number,

    value2: number,
    value3: number
    value4: number
}
/**
 * 装备附加属性
 */
export interface __EquipAttrData {
    id: number,
    name: string,
}
/**
 * 怪物
 */
export interface __MonsterData {
    id: number,
    name: string,
    group: number,
    type: number,
    career: number,

    icon: string,
    act: string,
    level: number,

    atk: number,
    def: number,
    hp: number,
    sp: number,
    skill: number,
}
/**
 * 场景
 */
export interface __SceneData {
    id: number,
    name: string,
    about: string,

    startIndex: number
    list: Array<__StageData>
}

/**
 * 关卡
 */
export interface __StageData {
    id: number,
    zid: number,
    name: string,
    about: string,
    mapId: string,

    goodId1: number,
    goodCount1: number,
    goodId2: number,
    goodCount2: number,
    goodId3: number,
    goodCount3: number,

    taskText: string,
    music: string
}
/**
 * 任务
 */
export interface __TaskData {
    id: number,
    name: string,
    reward: string,
}
/**
 * 神卡
 */
export interface __GodCard {
    id: number
    name: string
    about: string
    type: number
    pz: number
    max_star: number
    goodId: number
    goodCount: number
    water: number
    fire: number
    wind: number
    thunder: number
    earth: number
    job: number

    atk: number
    def: number
    hp: number
    sp: number

    feature: string
    value1: number
    value2: number
    value3: number

    feature_about: string
    feature_max: string

    list: Array<__GodCardLevel>

    godReborn: __GodReborn
}
/**
 * 神卡升级
 */
export interface __GodCardLevel {
    id: number
    godCardId: number
    star: number
    type: number
    attrType: number
    goodId1: number
    goodCount1: number

    goodId2: number
    goodCount2: number

    goodId3: number
    goodCount3: number

    water: number
    fire: number
    wind: number
    thunder: number
    earth: number
    job: number

    atk: number
    def: number
    hp: number
    sp: number

}
/**
 * 满星后重生
 */
export interface __GodReborn {
    id: number
    godCardId: number

    goodId1: number
    goodCount1: number

    goodId2: number
    goodCount2: number

    water: number
    fire: number
    wind: number
    thunder: number
    earth: number
    job: number

    atk: number
    def: number
    hp: number
    sp: number

}
/**
 * 魔盒
 */
export interface __MagicBox {
    id: number
    type: number
    icon: string
    name: string
    about: string
    consumeType: number
    goodId: number
    goodCount: number
    rewardId: number
    list: Array<__MagicBoxReward>
    maxPR: number
}
/**
 * 魔盒奖励
 */
export interface __MagicBoxReward {
    id: number
    magicBoxId: number
    type: number
    value1: number
    value2: number
    value3: number

    PR: number
}
export interface __PayData {
    id: number,//商品编号
    type: number,//类型
    iosId: string,//苹果商品id
    googleId: string,//谷歌商店唯一id
    name: string,//商品名字
    rmb: string,//RMB
    usd: string,//美刀
    gold: number//兑换的钻石
    firstGive: number//首次充值额外奖励钻石
}
export interface __BuyCoinData {
    id: number,//商品编号
    name: string,//商品名字
    maxCount: number,//限购次数
    gold: number  //花费钻石
    money: number // 获得
    gave: number // 赠送
}

export interface __BuyPowerData {
    id: number,//商品编号
    maxCount: number,//限购次数
    gold: number  //花费钻石
    power: number // 获得
}

export interface __BuyPandoraData {
    id: number,//商品编号
    name: string,    //名字
    adCount: number,//广告次数
    flushTime: number  //刷新时间
    goodId: number // 花费紫水晶ID
}

export interface __FirstPayData {
    id: number,//商品编号
    name: string,    //名字
    reward: string,    // 奖励
}

export interface __FirstPayRewardData {
    icon: string,
    count: number,
    frame: number,
}

export interface __WishData {
    id: number,         //商品编号
    name: string,       //名字
    payId: string,      //充值id
    maxCount: number    //每日限购次数
    reward: string,     //奖励
}

/**
 * 从服务器获取的奖励
 *  type:0,
                    icon:'icon/good/3001',
                    count:data._role._money,
                    id:0,
                    pz:0,
                    star:0
 */
export interface RewardData {
    type: number,//0:money,1:gole,2:good
    icon: string,//图标
    count: number,//数量
    id: number,//道具对应的id
    pz: 0,
    star: 0
    PR: 0
    name: ''
}
/**
 * 系统数据管理
 */
export default class SManage {
    /**
     * 角色数据
     */
    private roleDataMap: Map<number, __RoleData> = new Map();
    /**
     * 头像/框/称号
     */
    private roleIcons: Array<__RoleIcon> = [];
    /**
     * 道具
     */
    private goodMap: Map<number, __GoodData> = new Map();
    /**
     * 装备
     */
    private equipMap: Map<number, __EquipData> = new Map();
    /**
     * 装备强化
     * key : int 装备类型
     * vaule: list 强化数据列表
     */
    private equipQHMap: Map<string, Array<__EquipQHData>> = new Map();
    /**
     * 附属性
     */
    private attrMap: Map<number, __EquipAttrData> = new Map();
    /**
     * 技能
     */
    private skillMap: Map<number, __SkillData> = new Map();
    /**
     * 怪物
     */
    private monsterMap: Map<number, __MonsterData> = new Map();

    /**
     * 关卡数据
     */
    private sceneLists: Array<__SceneData> = null;
    private stageLists: Array<__StageData> = null;

    /**
     * 神卡
     */
    public godCards: Array<__GodCard> = null;
    /**
     * 魔盒
     */
    public magicBoxs: Array<__MagicBox> = null;
    /**
     * 充值数据
     */
    public payDatas: Array<__PayData> = null;
    /**
     * 任务列表
     */
    private taskLists: Array<__TaskData> = null;
    constructor(data) {
        this.initRoleData(data.roleData);
        this.roleIcons = data.roleIcon;
        this.initGood(data.good);
        this.initEquip(data.equip);
        this.initEquipQH(data.equipQH);
        this.initEquipAttr(data.eAttr);
        this.initSkillData(data.skill);
        this.initMonster(data.monster);
        this.sceneLists = data.scene;
        this.stageLists = data.stage;
        this.initScene();

        this.initGodCard(data.godCard)
        this.initGodCardLevel(data.godCardLevel)
        this.initGodReborn(data.godReborn)

        this.magicBoxs = data.magicBox
        this.initMaigcBoxReward(data.magicBoxReward)

        this.payDatas = data.payData

        this.taskLists = data.task;
    }
    private initMaigcBoxReward(list: Array<__MagicBoxReward>) {
        for (let i = 0; i < this.magicBoxs.length; i++) {
            const element = this.magicBoxs[i];
            for (let j = 0; j < list.length; j++) {
                const tmp = list[j];
                if (tmp.magicBoxId == element.rewardId) {
                    if (element.list == undefined) {
                        element.list = []
                    }
                    element.list.push(tmp)
                }
            }
        }
    }
    private initGodReborn(list: Array<__GodReborn>) {
        list.forEach(element => {
            let godCard = this.getGodCardById(element.godCardId)
            godCard.godReborn = element
        });
    }
    private initGodCardLevel(list: Array<__GodCardLevel>) {
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            let godCard = this.getGodCardById(element.godCardId)
            if (!godCard.list) {
                godCard.list = []
            }
            godCard.list.push(element)
        }
    }

    private initGodCard(list: Array<__GodCard>) {
        this.godCards = list
    }


    private initSkillData(list: Array<__SkillData>) {
        list.forEach(element => {
            this.skillMap.set(element.id, element);
        });
    }
    public getRoleIconById(id) {
        for (let i = 0; i < this.roleIcons.length; i++) {
            const element = this.roleIcons[i];
            if (element.id == id) {
                return element;
            }
        }
    }

    private initRoleData(roleDataLists: Array<__RoleData>) {
        roleDataLists.forEach(element => {
            this.roleDataMap.set(element.id, element);
        });
    }
    private initGood(lists: Array<__GoodData>) {
        lists.forEach(element => {
            this.goodMap.set(element.id, element);
        });
    }
    private initEquipAttr(lists: Array<__EquipAttrData>) {
        lists.forEach(element => {
            this.attrMap.set(element.id, element);
        });
    }
    private initEquip(lists: Array<__EquipData>) {
        lists.forEach(element => {
            this.equipMap.set(element.id, element);
        });
    }
    private initEquipQH(lists: Array<__EquipQHData>) {
        lists.forEach(element => {
            let key = element.occ + '@' + element.type
            let list = this.equipQHMap.get(key)
            if (list) {
                list.push(element)
            } else {
                list = []
                list.push(element)
                this.equipQHMap.set(key, list)
            }
        });
    }
    private initMonster(monsters: Array<__MonsterData>) {
        monsters.forEach(element => {
            this.monsterMap.set(element.id, element);
        });
    }
    private initScene() {
        for (let i = 0; i < this.stageLists.length; i++) {
            const element = this.stageLists[i];
            let scene = this.getSceneById(element.zid);
            if (scene.list == null) {
                scene.list = [];
                scene.startIndex = i;
            }
            scene.list.push(element);
        }
    }

    public getGodCardById(id): __GodCard {
        for (let i = 0; i < this.godCards.length; i++) {
            const element = this.godCards[i];
            if (element.id == id) {
                return element;
            }
        }
    }

    public getSceneById(id) {
        for (let i = 0; i < this.sceneLists.length; i++) {
            const element = this.sceneLists[i];
            if (element.id == id) {
                return element;
            }
        }
    }
    public getSceneByStage(stage: __StageData) {
        if (stage == null) {
            return this.sceneLists[this.sceneLists.length - 1];
        }
        return this.getSceneById(stage.zid);
    }
    public getScene() {
        return this.sceneLists;
    }
    public getStage() {
        return this.stageLists;
    }
    /**
     * 获取关卡
     * @param index 
     */
    public getStageByIndex(index: number) {
        if (index >= this.stageLists.length) {
            return null;
        }
        return this.stageLists[index];
    }
    /**
     * 获取怪物数据
     * @param id 
     */
    public getMonsterData(id: number): FFAttr {
        let monsterData = this.monsterMap.get(id);
        let fd1 = {
            id: id,
            type: monsterData.type,
            post: monsterData.career,
            path: "prefab/monster/" + monsterData.act,
            atk: monsterData.atk,
            def: monsterData.def,
            hp: monsterData.hp,
            sp: monsterData.sp,

            water: 0,
            fire: 0,
            wind: 0,
            thunder: 0,
            earth: 0,

            sd: 500,
            weaponSkill: 0,
            suitId: 0,
            suitCount: 0,
            attrs: [],
            zdl: 1,
            skin: null,
            weapon: null,
            bullet: null
        };
        return fd1;
    }
    public getGoodById(id: string) {
        return this.goodMap.get(parseInt(id));
    }
    public getGoodById1(id: number) {
        return this.goodMap.get(id);
    }
    public getEquipById(id): __EquipData {
        return this.equipMap.get(id);
    }
    public getEquipAttrById(id) {
        return this.attrMap.get(id);
    }
    public getSkillById(id: number) {
        return this.skillMap.get(id);
    }
    public getRoleById(id: number) {
        return this.roleDataMap.get(id);
    }
    public getRoleIcon() {
        return this.roleIcons
    }
    /**
     * 获取某个类型的强化数据
     * @param type 
     */
    public getEquipQHByType(equipData: __EquipData): Array<__EquipQHData> {
        return this.equipQHMap.get(equipData.occ + '@' + equipData.type)
    }
    /**
     * 从接收的数据中分离出奖励数据
     * @param reve 
     */
    public getRewards(reveData: ReveData): Array<RewardData> {
        let list = [];
        let data = reveData.data;
        if (data._role != undefined) {
            if (data._role._money != undefined) {
                if (data._role._money > 0) {
                    let rewardData = {
                        type: 0,
                        icon: 'icon/good/3001',
                        count: data._role._money,
                        id: 0,
                        pz: 4,
                        star: 0,
                        name: i18n.t('金币')
                    }
                    list.push(rewardData);
                }

            }
            if (data._role._gold != undefined) {
                if (data._role._gold > 0) {
                    let rewardData = {
                        type: 1,
                        icon: 'icon/good/3002',
                        count: data._role._gold,
                        id: 0,
                        pz: 5,
                        star: 0,
                        name: i18n.t('钻石')
                    }
                    list.push(rewardData);
                }
            }
            if (data._role._power != undefined) {
                if (data._role._power > 0) {
                    let rewardData = {
                        type: 1,
                        icon: 'icon/good/3003',
                        count: data._role._power,
                        id: 0,
                        pz: 5,
                        star: 0,
                        name: i18n.t('体力')
                    }
                    list.push(rewardData);
                }
            }
        }
        //道具
        if (data._pack != undefined) {
            for (let i = 0; i < data._pack.length; i++) {
                const element = data._pack[i];
                if (element.count > 0) {
                    let good = this.getGoodById1(element.id);
                    let rewardData = {
                        type: 2,
                        icon: 'icon/good/' + good.icon,
                        count: element.count,
                        id: element.id,
                        pz: good.pz,
                        star: 0,
                        name: i18n.t(good.name)
                    }
                    list.push(rewardData);
                }
            }
        }
        //装备
        if (data._equip != undefined) {
            for (let i = 0; i < data._equip.length; i++) {
                const element = data._equip[i];
                let equip = this.getEquipById(element.id);
                let rewardData = {
                    type: 3,
                    icon: 'icon/equip/' + equip.icon,
                    count: 0,
                    id: element.id,
                    pz: equip.pz,
                    PI: element.PI,
                    star: element.star,
                    _data: equip,
                    name: i18n.t(equip.name)
                }
                list.push(rewardData);
            }
        }
        if (data._godCard != undefined) {
            for (let i = 0; i < data._godCard.length; i++) {
                const element = data._godCard[i];
                let rewardData = {
                    type: 4,
                    data: element,
                    name: i18n.t('神卡')
                }
                list.push(rewardData);
            }
        }

        return list;
    }

    /**
     * 从接收的数据中分离出奖励数据
     * @param reve 
     */
    public getRewardViews(rewards: Array<__MagicBoxReward>): Array<RewardData> {
        let list = [];
        for (let i = 0; i < rewards.length; i++) {
            const element = rewards[i];
            if (element.type == 1) {
                let rewardData = {
                    type: 0,
                    icon: 'icon/good/3001',
                    count: element.value1,
                    id: 0,
                    pz: 3,
                    star: 0,
                    PR: element.PR
                }
                list.push(rewardData);
            } else if (element.type == 2) {
                let rewardData = {
                    type: 1,
                    icon: 'icon/good/3002',
                    count: element.value1,
                    id: 0,
                    pz: 5,
                    star: 0,
                    PR: element.PR
                }
                list.push(rewardData);
            } else if (element.type == 3) {
                let good = this.getGoodById1(element.value1);
                let rewardData = {
                    type: 2,
                    icon: 'icon/good/' + good.icon,
                    count: element.value2,
                    id: element.id,
                    pz: good.pz,
                    star: 0,
                    PR: element.PR
                }
                list.push(rewardData);
            } else if (element.type == 4) {
                let equip = this.getEquipById(element.value1);
                let rewardData = {
                    type: 3,
                    icon: 'icon/equip/' + equip.icon,
                    count: 0,
                    id: element.id,
                    pz: equip.pz,
                    PI: element.value3,
                    star: 0,
                    _data: equip,
                    PR: element.PR
                }
                list.push(rewardData);
            }
        }
        return list;
    }
}
