import { prop, mongoose, Ref } from '@typegoose/typegoose';
import { DicWarJson } from '../pubUtils/dictionary/DicWarJson';
import Hero, { HeroType } from '../db/Hero';
import { nowSeconds } from '../pubUtils/timeUtil';
import { DicHero } from '../pubUtils/dictionary/DicHero';
import { ArtifactModelType } from '../db/Artifact';


class Talent {
    @prop({ required: true })
    id: number;                  // 天赋表id
    @prop({ required: true })
    level: number;               // 激活等级
}


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

// 从玩家数据中覆盖warjson的部分字段
export class PvpHeroInfo {
    @prop({ required: true })
    actorId?: number = 0; // 敌人id
    @prop({ required: true })
    skinId?: number = 0; // 时装id，fashions表的heroId字段
    @prop({ required: false })
    actorName?: string = ""; // 敌人名
    @prop({ required: false })
    outIndex?: number = 0; // 程序将信息存入数组顺序
    @prop({ required: false })
    star?: number = 0; // 角色星级
    @prop({ required: false })
    job?: number = 0; // 角色职业
    @prop({ required: false })
    lv?: number = 0; // 角色等级
    @prop({ required: false })
    skill?: string = "0"; // 技能
    @prop({ required: false })
    seid?: string = "&"; // 技能
    @prop({ required: false })
    spine?: string = "0"; // S动画
    @prop({ required: false })
    colorStar?: number = 0; // 觉醒
    @prop({ required: false })
    quality?: number = 0; // 品质
    @prop({ required: false, type: Talent, _id: false })
    talent?: Talent[] = []; // 品质
    @prop({ required: false })
    subHid?: number = 0; // 副将

    @prop({ required: true, _id: false })
    attribute?: string; // 属性
    @prop({ required: true, _id: false, type: HeroArtifact })
    artifact?: HeroArtifact[] = []; // 宝物

    setHeroInfo(hero: HeroType, artifact: ArtifactModelType) {
        this.actorId = hero.hid;
        this.skinId = hero.skinId;
        this.actorName = hero.hName;
        this.star = hero.star;
        this.lv = hero.lv;
        this.job = hero.job;
        this.colorStar = hero.colorStar;
        this.quality = hero.quality;
        let skin = hero.skins?.find(cur => cur.enable);
        if(skin) this.talent = skin.talent;
        this.subHid = hero.subHid;
        if(artifact) this.artifact.push({ artifactId: artifact.artifactId, lv: artifact.lv })
    }

    setRobotInfo(dicHero: DicHero, lv?: number) {
        this.actorId = dicHero.heroId;
        this.skinId = dicHero.heroId;
        this.actorName = dicHero.name;
        this.quality = dicHero.quality;
        this.job = dicHero.jobid;
        this.star = dicHero.initialStar;
        this.colorStar = dicHero.initialColorStar;
        this.lv = lv;
    }

    setAttribute(attribute: string) {
        this.attribute = attribute;
    }

    setOutIndex(order: number) {
        this.outIndex = order;
    }
}

export class GVGHeroInfo extends PvpHeroInfo {
    @prop({ required: false })
    outIndex?: number = 0; // 程序将信息存入数组顺序
    @prop({ required: false })
    dataId?: number = 0; // 对应出兵表dataId

    setDataId(dataId: number, order: number) {
        this.dataId = dataId;
        this.outIndex = order;
    }
}

// 远征敌军
export class Enemies extends PvpHeroInfo {
    @prop({ required: true })
    actorId: number; // 敌人id
    @prop({ required: false })
    skinId: number; // 皮肤id，fashions表的heroId
    @prop({ required: false })
    actorName: string; // 敌人名
    @prop({ required: false })
    dataId: number; // 战场中唯一指向武将的代码
    @prop({ required: false })
    relation: number; // 角色属于我方还是地方
    @prop({ required: false })
    x: number; // 战场x坐标
    @prop({ required: false })
    y: number; // 战场y坐标
    @prop({ required: false })
    dirction: number; // 朝向
    @prop({ required: false })
    var: number; // 变量
    @prop({ required: false })
    hide: number; // 是否隐藏
    @prop({ required: false })
    initial_ai: number; // AI类型
    @prop({ required: false })
    ce: number; // 战力

    // warjson 出兵表
    // heroInfo 覆盖掉出兵表的相应参数
    constructor(warjson: DicWarJson, heroInfo: PvpHeroInfo, ce: number) {
        super();
        this.actorId = heroInfo.actorId != undefined ? heroInfo.actorId : warjson.actorId;
        this.skinId = heroInfo.skinId != undefined ? heroInfo.skinId: warjson.actorId;
        this.actorName = heroInfo.actorName != undefined ? heroInfo.actorName : warjson.actorName;
        this.dataId = warjson.dataId;
        this.relation = warjson.relation;
        this.outIndex = heroInfo.outIndex != undefined ? heroInfo.outIndex : warjson.outIndex;
        this.x = warjson.x;
        this.y = warjson.y;
        this.dirction = warjson.dirction;
        this.var = warjson.var;
        this.lv = heroInfo.lv != undefined ? heroInfo.lv : warjson.lv;
        this.hide = warjson.hide;
        this.initial_ai = warjson.initial_ai;
        this.attribute = heroInfo.attribute;
        this.skill = heroInfo.skill != undefined ? heroInfo.skill : warjson.skill;
        this.seid = heroInfo.seid != undefined ? heroInfo.seid : warjson.seid;
        this.star = heroInfo.star != undefined ? heroInfo.star : warjson.star;
        this.spine = heroInfo.spine != undefined ? heroInfo.spine : warjson.spine;
        this.ce = ce;
    }

}

export class PvpEnemies extends Enemies {
    @prop({ required: true })
    star: number;
    @prop({ required: true })
    colorStar: number;
    @prop({ required: true })
    quality: number;
    @prop({ required: true })
    lv: number;
    @prop({ required: true })
    job: number;
    @prop({ required: true })
    score: number;
    @prop({ required: true, type: () => Talent, _id: false })
    talent: Talent[];
    @prop({ required: true })
    subHid: number;
    @prop({ required: true, type: () => HeroArtifact, _id: false })
    artifact: HeroArtifact[] = []; // 宝物


    // score: 这个武将的军功
    constructor(warjson: DicWarJson, heroInfo: PvpHeroInfo, score?: number, ce?: number) {
        super(warjson, heroInfo, ce);
        this.star = heroInfo.star;
        this.colorStar = heroInfo.colorStar;
        this.quality = heroInfo.quality;
        this.lv = heroInfo.lv;
        this.talent = heroInfo.talent;
        this.job = heroInfo.job;
        this.subHid = heroInfo.subHid;
        this.artifact = heroInfo.artifact||[];
        if(score) this.score = score;
    }
}

// 未显示在阵容中的其他武将
export class PvpOtherHeroes extends PvpHeroInfo {
    @prop({ required: true })
    score: number;

    // score: 这个武将的军功
    constructor(score: number) {
        super();
        this.score = score;
    }
}

export class ItemReward {
    @prop({ required: true })
    id: number;
    @prop({ required: true })
    count: number;
}


export class TopHero {
    @prop({ required: true })
    hid: number; // 武将id
    @prop({ required: true })
    ce: number; // 战力
    @prop({ ref: Hero, type: mongoose.Schema.Types.ObjectId })
    hero: Ref<Hero>;
}


export class DungeonHero {
    @prop({ required: true })
    battleId: number; // 关卡id
    @prop({ required: true, type: Number, default: [] })
    heroes: Array<number>; // 武将id
}

export class PayRecord {
    @prop({ required: true })
    id: string;						// 购买项 product id
    @prop({ required: true })
    cnt: number;					// 购买次数
}

export class WarStar {
    @prop({ required: true })
    id: number;						// 关卡 id
    @prop({ required: true })
    warType: number;      // 关卡类型
    @prop({ required: true })
    star: number;					// 星级
    @prop({ required: true, type: Number })
    stars: number[];					// 星级
}

export class Teraph {
    @prop({ required: true, default: 1 })
    id: number;						// 神像的id
    @prop({ required: true, default: 1 })
    grade: number;                  // 等级
    @prop({ required: true, default: 0 })
    hp: number;
    @prop({ required: true, default: 0 })
    atk: number;
    @prop({ required: true, default: 0 })
    def: number;
    @prop({ required: true, default: 0 })
    mdef: number;
    @prop({ required: true, default: 0 })
    agi: number;
    @prop({ required: true, default: 0 })
    luk: number;
}

export class Figure {
    @prop({ required: true, default: 0 })
    id: number;						// 头像id
    @prop({ required: true, default: false })
    enable: boolean = false;				// 是否启用
    @prop({ required: false })
    time?: number;					        // 到期时间
    @prop({ required: false, type: Number })
    unlockedId?: number[];			// 当前已经解锁了的type
    @prop({ required: true, default: false })
    unlocked: boolean = false;			// 是否已解锁
    
    // 埋点用
    count = 1;
    inc = 1;
    reason?: number;

    public get status () {           // 虚拟字段status，当前头像是否已经解锁
        if(!this.unlocked) return this.unlocked; // 未解锁
        if(this.time && this.time < nowSeconds()) return false; // 已解锁过时
        return true;
    }

    constructor(id: number, unlocked: boolean, time?: number, enable = false) {
        this.id = id;
        this.unlocked = unlocked;
        if(time) this.time = time;
        this.enable = enable;
    }

}

/**
 * @description 出价记录
 * @export
 * @class BidRec
 */
export class BidRec {
    @prop({ required: true, default: '' })
    roleId: string; // 出价玩家
    @prop({ required: true, default: 0 })
    price: number; // 出价金额
    @prop({ required: true })
    time: Date; // 出价时间
}

/**
 * @description 拍品记录
 * @export
 * @class LotRec
 */
export class LotRec {
    @prop({ required: true, default: '' })
    code: string; // 竞拍物品唯一标识
    @prop({ required: true, default: 0 })
    gid: number; // 物品 id
    @prop({ required: true, default: 0 })
    count: number; // 物品数量
    @prop({ required: true, default: 0 })
    price: number; // 物品成交价
    @prop({ required: true, default: null })
    time: Date; // 出价时间
    @prop({ required: true, default: 0 })
    max: boolean; // 一口价
}

/**
 * @description 分红记录
 * @export
 * @class DividendRec
 */
export class DividendRec {
    @prop({ required: true, default: '' })
    roleId: string;
    @prop({ required: true, default: 0 })
    baseNum: number; // 职位分红
    @prop({ required: true, default: 0 })
    posNum: number; // 职位分红
    @prop({ required: true, default: 0 })
    weekendNum: number; // 额外分红，周末
    @prop({ required: true, default: 0 })
    total: number; // 总分红
    @prop({ required: true, default: 0 })
    status: number; // 0：未领取，1：已领取
}

/**
 * @description 商店商品
 * @export
 * @class ShopItem
 */
export class ShopItem {
    @prop({ required: true, default: 0 })
    id: number;     // 商品id shopItemId
    @prop({ required: true, default: 0 })
    order: number;  // 显示排序
    @prop({ required: true, default: 0 })
    discount: number; // 折扣
}