import { RoleType } from "../../db/Role";
import { FriendShipType } from "../../db/FriendShip";
import * as friendUtil from '../../pubUtils/friendUtil'
import { FRIEND_RELATION_TYPE } from "../../consts";
import { Connect, EPlace, HeroType, Stone, Talent } from "../../db/Hero";
import { JewelSe, JewelType } from "../../db/Jewel";
import { ArtifactModelType } from "../../db/Artifact";

export class FriendParams {
    roleId: string;
    roleName: string;
    lv: number;
    head: number;
    frame: number;
    spine: number;
    ce: number;
    title: number;
    serverId: number;
    serverName: string;
    type: number = FRIEND_RELATION_TYPE.NORMAL;
    guildName: string = '';
    ipLocation: string = '';

    constructor(role: RoleType) {
        this.roleId = role.roleId;
        this.roleName = role.roleName;
        this.lv = role.lv;
        this.head = role.head;
        this.frame = role.frame;
        this.spine = role.spine;
        this.ce = role.ce;
        this.title = role.title;
        this.guildName = role.guildName||'';
        this.ipLocation = role.fixedIpLocation||role.ipLocation||'未知';
    }

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

    setType(type: number) {
        this.type = type;
    }
}

export class FriendRecommendParams extends FriendParams {
    friendCnt: number = 0;
    recFrdApplyCnt: number = 0;

    constructor(role: RoleType) {
        super(role);
        if(role.friendCnt) this.friendCnt = role.friendCnt;
        if(role.recFrdApplyCnt) this.recFrdApplyCnt = role.recFrdApplyCnt;
    } 
    
}

export class FriendApplyParams extends FriendParams {
    applyCode: string;
    friendCnt: number = 0;
    
    constructor(applyCode: string, role: RoleType) {
        super(role);
        this.applyCode = applyCode;
        if(role.friendCnt) this.friendCnt = role.friendCnt;
    } 
}

export class FriendListParam extends FriendParams {
    guildName: string = "";
    isOnline: boolean = false;
    quitTime: number = 0;
    friendValue: number = 0;
    friendLv: number = 1;
    canReceive: boolean = false; // 初始没人送肯定不能领取
    canSend: boolean = true; // 初始可以赠送

    constructor(role: RoleType, myRoleId: string, friendship: FriendShipType) {
        super(role);
        if(role.guildName) this.guildName = role.guildName;
        this.quitTime = role.quitTime;

        this.friendLv = friendship.friendLv;
        this.friendValue = friendship.friendValue;

        friendUtil.setCanReceiveAndCanSend.bind(this, myRoleId, friendship)();
    }

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

export class FriendValueListParam {
    roleId: string;
    friendValue: number = 0;
    friendLv: number = 1;
    canReceive: boolean = false;
    canSend: boolean = true;

    constructor(roleId: string, myRoleId: string, friendship: FriendShipType) {
        this.roleId = roleId;

        this.friendLv = friendship.friendLv;
        this.friendValue = friendship.friendValue;

        friendUtil.setCanReceiveAndCanSend.bind(this, myRoleId, friendship)();
    }
}

export class BlackListParam extends FriendParams {
    guildName: string = "";
    isOnline: boolean = false;
    quitTime: number = 0;

    constructor(role: RoleType) {
        super(role);
        if(role.guildName) this.guildName = role.guildName;
        this.quitTime = role.quitTime;
    }

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

export class HeroDetailJewelRandSeParam {
    id: number;
    seid: number;
    rand: number;

    constructor(randSe: JewelSe) {
        this.id = randSe.id;
        this.seid = randSe.seid;
        this.rand = randSe.rand;
    }
}

export class HeroDetailJewelParam {
    seqId: number;
    id: number;
    randSe: HeroDetailJewelRandSeParam[] = [];
    rareSe: HeroDetailJewelRandSeParam[] = [];

    constructor(jewel: JewelType) {
        this.seqId = jewel.seqId;
        this.id = jewel.id;
        for(let randSe of jewel.randSe) {
            this.randSe.push(new HeroDetailJewelRandSeParam(randSe));
        }
        for(let rareSe of jewel.rareSe) {
            this.rareSe.push(new HeroDetailJewelRandSeParam(rareSe));
        }
    }
}

export class HeroDetailEplaceParam {
    id: number;
    equipId: number;
    lv: number;
    quality: number;
    star: number;
    stones: Stone[];
    jewelId: number;
    jewel: HeroDetailJewelParam = null;

    constructor(equip: EPlace) {
        this.id = equip.id;
        this.equipId = equip.equipId;
        this.lv = equip.lv;
        this.quality = equip.quality;
        this.star = equip.star;
        this.stones = equip.stones;
        this.jewelId = equip.jewel;
    }

    setJewel(jewel: JewelType) {
        this.jewel = new HeroDetailJewelParam(jewel);
    }
}

export class HeroDetailParam {
    roleId: string;
    hid: number;
    ce: number;
    lv: number;
    star: number;
    colorStar: number;
    quality: number;
    job: number;
    skinId: number;
    ePlace: HeroDetailEplaceParam[] = [];
    attributes: {
        hp: number;
        atk: number;
        def: number;
        mdef: number;
    }
    talent: Talent[] = [];
    connections: Connect[] = [];
    role: {
        title: number;
        scroll: number;
        teraph: number;
        school: number;
    };
    subHid: number = 0;
    artifacts: {artifactId: number, lv: number }[] = []

    constructor(hero: HeroType) {
        this.roleId = hero.roleId;
        this.hid = hero.hid;
        this.ce = hero.ce;
        this.lv = hero.lv;
        this.star = hero.star;
        this.colorStar = hero.colorStar;
        this.quality = hero.quality;
        this.job = hero.job;
        this.skinId = hero.skinId;
        for(let equip of hero.ePlace) {
            this.ePlace.push(new HeroDetailEplaceParam(equip));
        }
        for(let skin of hero.skins) {
            if(skin.enable) {
                this.talent = skin.talent||[];
            }
        }
        this.connections = hero.connections;
        this.subHid = hero.subHid;
    }

    setAttributes(attributes: {hp: number, atk: number, def: number, mdef: number}) {
        this.attributes = attributes;
    }

    setJewels(jewels: Map<number, JewelType>) {
        for(let eplaceParam of this.ePlace) {
            if(jewels.has(eplaceParam.jewelId)) {
                eplaceParam.setJewel(jewels.get(eplaceParam.jewelId));
            }
        }
    }

    setRole(title: number, scroll: number, teraph: number, school: number) {
        this.role = { title, scroll, teraph, school };
    }

    setArtifact(artifact: ArtifactModelType) {
        this.artifacts.push({ artifactId: artifact.artifactId, lv: artifact.lv });
    }
}