import { PvpEnemies, PvpOtherHeroes, WarStar } from "../../domain/dbGeneral";
import { GuildType } from "../../db/Guild";
import { RoleType } from "../../db/Role";
import { WAR_TYPE } from "../../consts";
import { gameData } from "../../pubUtils/data";
import { EXTERIOR } from "../../pubUtils/dicParam";
import { HeroType } from "../../db/Hero";
import { PvpDefenseType } from "../../db/PvpDefense";
import { PvpHistoryOppType } from "../../db/PvpHistoryOpp";
import { UserGuildType } from "../../db/UserGuild";

export class PlayerDetailHero {
    actorId: number;
    skinId: number;
    lv: number;
    star: number;
    colorStar: number;
    quality: number;
    score: number = 0;
    isDefense: boolean;

    setPvpHeroInfo?(hero: PvpEnemies|PvpOtherHeroes) {
        this.actorId = hero.actorId;
        this.skinId = hero.skinId;
        this.lv = hero.lv;
        this.star = hero.star;
        this.colorStar = hero.colorStar;
        this.quality = hero.quality;
        this.score = hero.score;
    }

    setByDbHero?(hero: HeroType) {
        if(!hero) return;

        this.actorId = hero.hid;
        this.lv = hero.lv;
        this.star = hero.star;
        this.colorStar = hero.colorStar;
        this.quality = hero.quality;
        this.skinId = hero.skinId;

    }

    setPvpDefense?(pvpDefense: PvpDefenseType) {
        if(!pvpDefense) return;

        let { heroScores, defense } = pvpDefense;
        let heroScore = heroScores.find(cur => cur.hid == this.actorId);
        this.score = heroScore? heroScore.score: 0;

        if(defense && defense.heroes) {
            let h = defense.heroes.find(cur => cur.actorId == this.actorId);
            if(!!h) this.isDefense = true;
        } else {
            this.isDefense = false;
        }
    }

    setIsDefense?(isDefense: boolean) {
        this.isDefense = isDefense;
    }
}

class PlayerDetailBattle {
    main: string[] = [];
    tower: string[] = [];
    pvp: string[] = [];
    ladder: string[] = [];

    constructor(warStar: WarStar[], ladder: number, tower: number) {
        let main = 0;
        for(let { id, warType } of warStar) {
            if(warType == WAR_TYPE.NORMAL) {
                if(id > main) main = id;
            }
        }
        let curDicWar = gameData.war.get(main);
        if(curDicWar) {
            this.main.push(`${curDicWar.chapter}`, `${curDicWar.section}`);
        }
        if(ladder <= 0 || ladder >= 3001) {
            this.ladder.push('未上榜');
        } else {
            this.ladder.push(`第${ladder}名`)
        }
        this.tower.push(tower.toString());

    }
}

export class PlayerDetail {
    roleId: string;
    roleName: string;
    lv: number;

    serverName: string;
    title?: number = 1;
    ipLocation: string = '';
    head?: number = EXTERIOR.EXTERIOR_FACE;
    frame?: number = EXTERIOR.EXTERIOR_FACECASE;
    spine?: number = EXTERIOR.EXTERIOR_APPEARANCE;

    score?: number = 0;
    pLv?: number = 1;
    defCe?: number = 0;

    heroes: PlayerDetailHero[];
    rank?:number = 0;
    
    battle?: PlayerDetailBattle = new PlayerDetailBattle([], 0, 0);

    constructor(roleId: string, heroes: PlayerDetailHero[], rank?: number) {
        this.roleId = roleId;
        this.heroes = heroes;
        this.rank = rank;
    }

    setRobot(pvpHistoryOpp: PvpHistoryOppType) {
        if(pvpHistoryOpp.roleName) this.roleName = pvpHistoryOpp.roleName;
        if(pvpHistoryOpp.lv) this.lv = pvpHistoryOpp.lv;
        if(pvpHistoryOpp.title) this.title = pvpHistoryOpp.title;
        if(pvpHistoryOpp.head) this.head = pvpHistoryOpp.head;
        if(pvpHistoryOpp.frame) this.frame = pvpHistoryOpp.frame;
        if(pvpHistoryOpp.spine) this.spine = pvpHistoryOpp.spine;
        if(pvpHistoryOpp.score) this.score = pvpHistoryOpp.score;
        if(pvpHistoryOpp.pLv) this.pLv = pvpHistoryOpp.pLv;
        if(pvpHistoryOpp.defCe) this.defCe = pvpHistoryOpp.defCe;
    }

    setPlayer(role: RoleType, ) {
        if(role.roleName) this.roleName = role.roleName;
        if(role.lv) this.lv = role.lv;
        if(role.title) this.title = role.title;
        if(role.head) this.head = role.head;
        if(role.frame) this.frame = role.frame;
        if(role.spine) this.spine = role.spine;
        this.ipLocation = role.fixedIpLocation||role.ipLocation||'未知';
    }

    setServerName(serverName: string) {
        this.serverName = serverName;
    }

    setCe(ce: number) {
        this.defCe = ce;
    }

    setPvpDefense(pvpDefense: PvpDefenseType) {
        if(pvpDefense.score) this.score = pvpDefense.score;
        if(pvpDefense.defense && pvpDefense.lineupCe) {
            let defCe = 0;
            for(let { actorId } of pvpDefense.defense.heroes) {
                let lineupCe = pvpDefense.lineupCe.find(cur => cur.hid == actorId);
                if(lineupCe) defCe += lineupCe.ce;
            }
            this.defCe = defCe;
            this.pLv = pvpDefense.defense.pLv;
        }
    }

    setWarStar?(warStar: WarStar[], pvp: number, tower: number) {
        this.battle = new PlayerDetailBattle(warStar, pvp, tower);
    }
}

// 军团返回
export class GuildMainInfo {
    code: string;              // 军团唯一编号
    icon: number;              // 头像
    name: string;              // 军团名
    lv: number;                // 等级
    memberCnt: number;         // 人数
    isAuto: boolean;           // 是否是自动加入
    ceLimit: number;           // 战力条件
    
    constructor(guild: GuildType) {
        this.code = guild.code;
        this.icon = guild.icon;
        this.name = guild.name;
        this.lv = guild.lv;
        this.memberCnt = guild.memberCnt;
        this.ceLimit = guild.ceLimit;
        this.isAuto = guild.isAuto;
    }
}

// getGuildList里的返回
export class GuildListInfo extends GuildMainInfo {
    leader: string;            // 团长名
    hasApply: boolean = false;         // 是否有申请过
    
    constructor(guild: GuildType) {
        super(guild);
        let leader = <RoleType>guild.leader;
        if(leader) this.leader = leader.roleName;
    }

    setApply(hasApply: boolean) {
        this.hasApply = hasApply;
    }
}

export class GuildMemberParam {
    roleId: string;
    roleName: string;
    ce: number;
    head: number = EXTERIOR.EXTERIOR_FACE;
    frame: number = EXTERIOR.EXTERIOR_FACECASE;
    spine: number = EXTERIOR.EXTERIOR_APPEARANCE;
    lv: number;
    title: number = 1;
    job: number;
    quitTime: number;
    isOnline: boolean;

    auth?: number;
    activeWeekly?: number;
    activeUpdateTime?: number;

    constructor(userGuild: UserGuildType, role: RoleType) {
        this.roleId = role.roleId;
        this.roleName = role.roleName;
        this.ce = role.ce;
        this.head = role.head;
        this.frame = role.frame;
        this.spine = role.spine;
        this.lv = role.lv;
        this.title = role.title;
        this.quitTime = role.quitTime;

        this.job = userGuild.job;
        this.auth = userGuild.auth;
        this.activeWeekly = userGuild.activeWeekly;
        this.activeUpdateTime = userGuild.activeUpdateTime;
    }

    setOnline(isOnline: boolean) {
        this.isOnline = isOnline;
    }
}