import { EXTERIOR } from "../pubUtils/dicParam";
import { WoodenHorse } from "./battleField/guildActivity";
import { RoleUpdate, RoleType } from "../db/Role";
import { reduceCe } from "../pubUtils/util";
import { GuildUpdateParam } from "../db/Guild";
import { HeroType, } from "../db/Hero";
import { getSeconds } from "../pubUtils/timeUtil";
import { prop } from "@typegoose/typegoose";
import { pick } from "underscore";

// 排行榜返回玩家值
export class RankParam {
    @prop({ required: true })
    roleName: string = "";

    @prop({ required: true })
    lv: number = 0;

    @prop({ required: true })
    vLv?: number = 0;

    @prop({ required: true })
    guildName: string = "";

    @prop({ required: true })
    head: number = EXTERIOR.EXTERIOR_FACE;

    @prop({ required: true })
    frame: number = EXTERIOR.EXTERIOR_FACECASE;

    @prop({ required: true })
    spine: number = EXTERIOR.EXTERIOR_APPEARANCE;

    @prop({ required: true })
    title: number = 1;

    @prop({ required: true })
    ce: number = 0;

    @prop({ required: true })
    updatedAt?: number = 0;

    constructor(role: RoleUpdate|RankParam, fromDb: boolean) {
        if(role.roleName) this.roleName = role.roleName;
        if(role.lv) this.lv = role.lv;
        if(role.vLv) this.vLv = role.vLv;
        if(role.head) this.head = role.head;
        if(role.frame) this.frame = role.frame;
        if(role.spine) this.spine = role.spine;
        if(role.title) this.title = role.title;
        if(role.guildName) this.guildName = role.guildName||"";
        if(role.ce) {
            if(fromDb && !(<RoleUpdate>role).isReducedCe) {
                this.ce = reduceCe(role.ce);
            } else {
                this.ce = role.ce;
            }
        }
        if(role.updatedAt) {
            if(typeof role.updatedAt == 'number') {
                this.updatedAt = role.updatedAt;
            } else {
                this.updatedAt = getSeconds(role.updatedAt);
            }
        } 
    }
}

export class GuildLeader {
    @prop({ required: true })
    roleId: string;

    @prop({ required: true })
    roleName: string;

    @prop({ required: true })
    title: number;

    @prop({ required: true })
    lv: number;

    @prop({ required: true })
    head: number;

    @prop({ required: true })
    frame: number;

    @prop({ required: true })
    spine: number;

    constructor(leader: { roleId: string, roleName: string, title: number, lv: number, head: number, frame: number, spine: number}) {
        this.roleId = leader.roleId;
        this.roleName = leader.roleName;
        this.title = leader.title;
        this.lv = leader.lv;
        this.head = leader.head;
        this.frame = leader.frame;
        this.spine = leader.spine;
    }
}

// 排行榜返回玩家值
export class LineupParam {
    @prop({ required: true })
    hid: number;

    @prop({ required: true })
    skinId: number;

    @prop({ required: true })
    star: number;

    @prop({ required: true })
    colorStar: number;

    @prop({ required: true })
    lv: number;

    @prop({ required: true })
    quality: number;

    @prop({ required: true })
    job: number;

    constructor(hero: HeroType) {
        this.hid = hero.hid;
        this.skinId = hero.skinId;
        this.star = hero.star;
        this.colorStar = hero.colorStar;
        this.lv = hero.lv;
        this.quality = hero.quality;
        this.job = hero.job;
    }
}

export class RoleRankInfo extends RankParam {
    rank: number = 1;
    roleId: string = "";
    num: number = 0;
    time: number = 0;
    hero: LineupParam[];
    lineup: LineupParam[];
    
    setInfo(rank: number, myId: myIdInter, num: number, time: number) {
        this.rank = rank;
        this.roleId = myId.roleId;
        this.num = num;
        this.time = time;
    }

    isMyInfo(myId: myIdInter) {
        return this.roleId == myId.roleId;
    }

    setTopLine(lineup: LineupParam[]){
        this.lineup = lineup;
    }

    setSingleHero(hero: LineupParam[]){
        this.hero = hero;
    }
}

// 公会排行榜返回值
export class GuildRankParam {
    @prop({ required: true })
    icon: number;

    @prop({ required: true })
    name: string;

    @prop({ required: true })
    lv: number;

    @prop({ required: true })
    memberCnt: number;

    @prop({ required: true, _id: false })
    leader: GuildLeader;

    @prop({ required: true })
    guildCe: number;

    constructor(guild: GuildUpdateParam, fromDb: boolean) {
        this.icon = guild.icon;
        this.name = guild.name;
        this.lv = guild.lv;
        let leader = <RoleType>guild.leader;
        let _leader = new GuildLeader(leader);
        this.leader = _leader;
        // console.log('*****', guild.isReducedCe, guild.guildCe, reduceCe(guild.guildCe))
        if(guild.isReducedCe) {
            this.guildCe = guild.guildCe;
        } else {
            this.guildCe = reduceCe(guild.guildCe);
        }

        if(fromDb && !(<GuildUpdateParam>guild).isReducedCe) {
            this.guildCe = reduceCe(guild.guildCe);
        } else {
            this.guildCe = guild.guildCe;
        }


        this.memberCnt = guild.memberCnt;
    }
}

export class GuildRankInfo extends GuildRankParam {
    rank: number;
    code: string;
    num: number;
    time: number;
    active?: number;
    
    setInfo(rank: number, myId: myIdInter, num: number, time: number) {
        this.rank = rank;
        this.code = myId.guildCode;
        this.num = num;
        this.time = time;
    }

    setInfo2(rank: number, myId: myIdInter, num: number, active: number, time: number) {
        this.rank = rank;
        this.code = myId.guildCode;
        this.num = num;
        this.time = time;
        this.active = active;
    }

    isMyInfo(myId: myIdInter) {
        return this.code == myId.guildCode;
    }
}



export class RoleAndGuildRankInfo {
    rank: number;
    num: number;
    userInfo?: RankParam & { roleId: string };
    lineup?: LineupParam[];
    guildInfo?: GuildRankParam & { code: string };
    
    constructor(rank: number, num: number) {
        this.rank = rank;
        this.num = num;
    }

    setUserInfo(role: RoleRankInfo) {
        this.userInfo = pick(role, ['roleId', 'roleName', 'head', 'frame', 'spine', 'lv', 'title', 'guildName', 'ce']);
        this.lineup = role.lineup;
    }

    setGuildInfo(guild: GuildRankInfo) {
        this.guildInfo = pick(guild, ['code', 'icon', 'name', 'leader', 'guildCe', 'lv', 'memberCnt', 'active']);
    }

    isMyInfo(myId: myIdInter) {
        if(this.userInfo && myId.roleId) {
            if(this.userInfo.roleId == myId.roleId) return true;
        }
        if(this.guildInfo && myId.guildCode) {
            if(this.guildInfo.code == myId.guildCode) return true;
        }
        return false;
    }
}

export class SimpleGuildRankParam {
    rank: number = 0;
    code: string = "";
    name: string = "";
    num: number = 0;
    
    constructor(rank: number, code: string, name: string, num: number) {
        this.rank = rank;
        this.code = code;
        this.name = name;
        this.num = num;
    }
}

export class SimpleGuildRankWithTimeParam extends SimpleGuildRankParam {
    time: number = 0;
    durability: number = 0;
    
    constructor(rank: number, code: string, name: string, obj: WoodenHorse|{distance: number, time: number, startTime: number, durability: number}|false) {
        super(rank, code, name, obj?obj.distance:0);
        // console.log(JSON.stringify(obj));
        if(obj) {
            this.num = obj.distance;
            this.time = Math.floor((obj.time - obj.startTime)/1000);
            this.durability = obj.durability;
        }
    }
}

export class SimpleRoleRankParam {
    rank: number;
    roleId: string;
    roleName: string;
    num: number;

    constructor(rank: number, roleId: string, roleName: string, num: number) {
        this.rank = rank;
        this.roleId = roleId;
        this.roleName = roleName;
        this.num = num;
    }
}

export class KeyName {
    key: string;
    serverId?: number;
    guildCode?: string;
    cityId?: number;
    hid?: number;
    seasonNum?: number;

    constructor(key: string, param: KeyNameParam) {
        this.key = key;
        if(param.serverId) this.serverId = param.serverId;
        if(param.guildCode) this.guildCode = param.guildCode;
        if(param.cityId) this.cityId = param.cityId;
        if(param.hid) this.hid = param.hid;
        if(param.seasonNum) this.seasonNum = param.seasonNum;
    }

    public getName() {
        let res = this.key;
        if(this.serverId) res += `:${this.serverId}`;
        if(this.guildCode) res += `:${this.guildCode}`;
        if(this.cityId) res += `:${this.cityId}`;
        if(this.hid) res += `:${this.hid}`;
        if(this.seasonNum) res += `:${this.seasonNum}`;
        return res;
    }

    public getNameWithPlus(...plus: string[]) {
        let res = this.getName();
        for(let p of plus) {
            res += `:${p}`;
        }
        return res;
    }

    public getTimeName() {
        return this.getNameWithPlus('time');
    }

    public getUnionName() {
        return this.getNameWithPlus('union');
    }
}

export type KeyNameParam = Partial<KeyName>;

export class ValueConfig {
    isMain: boolean;   // 最主要的排序，getRank的num字段，isInc的时候只有这个会用zinc
    name: string
    len: number;  // 长度
    weight?: number;  // 按顺序排之后，其他参数的len加和
    reverse: boolean = false; // 例如时间戳，需要倒转的那种
    isTimestamp: boolean = false; // 是否是时间戳

    constructor(isMain: boolean, name: string, len: number, reverse = false, isTimestamp = false) {
        this.isMain = isMain;
        this.name = name;
        this.len = len;
        this.reverse = reverse;
        this.isTimestamp = isTimestamp;
    }

    setWeight(weight: number) {
        this.weight = weight;
    }
}

export interface myIdInter {
    roleId?: string;
    guildCode?: string;
    hid?: number;
}

// 排行榜总览左边的数据
export class GeneralRankParam {
    type: number;  // 排行榜id
    userInfo?: RankParam;
    num: number;
    received: number[];

    constructor(type: number, rankInfo: RoleRankInfo|GuildRankInfo, general: number, received?: number[]) {
        this.type = type;
        if(general == 1) {
            if(rankInfo instanceof RoleRankInfo) {
                this.userInfo = pick(rankInfo, ['roleId', 'roleName', 'head', 'frame', 'spine', 'guildName', 'ce', 'title', 'lv']);
            }
        }
        this.num = rankInfo.num;
        this.received = received;
    }
}
