import { RoleType } from "../../db/Role";
import { GVGLeagueType } from "../../db/GVGLeague";
import { GVGLeaguePrepareType } from "../../db/GVGLeaguePrepare";
import { Distribute, GVGUserDataType } from "../../db/GVGUserData";
import { GuildType } from "../../db/Guild";
import { LEAGUE_JOB } from "../../consts";
import { GVGLeagueFarmType } from "../../db/GVGLeagueFarm";
import { nowSeconds } from "../../pubUtils/timeUtil";

class LeagueLeaderInfo {
    name: string;        // 盟主名
    serverId: number;  // 盟主所处小区
    serverName: number; // 盟主所处小区名

    constructor(leader: RoleType, serverNames: any) {
        this.name = leader.roleName;
        this.serverId = leader.serverId;
        this.serverName = serverNames[this.serverId];
    }
}

class GVGItem {
    id: number; 
    count: number;
    expireTime: number;      // 过期时间
}

class LeaguePlayerData {
    auth: number = 3;              // 我的权限 1-盟主 2-副盟主 3-平民
    job: number = 0;               // 我的职能，1-贤臣 2-猛将 0-未选择
    canPrepare: boolean = false;   // 是否可以使用整备期模块，组建期过后加入的玩家，当天的整备模块不可参与
    canChooseJob: boolean = true;  // 是否可以选择职能，团内选择职业的人数达到最大人数了，已经不可再选职位了
    items: GVGItem[] = [];         // 持有的道具，包括两种令、战功和种子
    // 奖励相关
    receivedLv: number = 0;              // 奖励领到几级
    tasks: GVGTask[] = [];           // 如果没有达成的任务就不放在这个数组里面
}

class LeagueResource {
    food: number = 0;        // 粮食
    mineral: number = 0;     // 矿物
    wood: number = 0;        // 木堆
}

class GVGTask {
    id: number;        // 任务id
    count: number;
    status: number;    // 状态 0-未领取 1-已领取
}

export class GVGMainData {
    configId: number = 0;           // 赛期
    period: number = 1;             // 当前是什么期 1-组建期 2-备战期 3-激战期
    countdownTime: number = 0;      // 这个周期结束时间，10位时间戳单位秒
    type: number = 1;               // 1-单服 2-跨服
    hasLeague: boolean = false;     // 是否拥有联军
    // 左侧联军信息
    leagueCode: string;              // 联军唯一code
    leader: LeagueLeaderInfo;
    name: string;                    // 联军名
    icon: number;                    // 图标
    lv: number;                      // 联军等级
    notice: string;                  // 公告
    guildCnt: number;                // 军团数量
    memberCnt: number;               // 玩家人数
    teamCnt: number;                 // 里面的玩家的队伍数 TODO
    leagueCe: number;                // 联军内所有人总战力
    // 个人信息
    myInfo: LeaguePlayerData = new LeaguePlayerData();
    // 激战期相关，城池背景
    cities: number[] = [];           // 占领的城池id
    
    // 内政相关
    resources: LeagueResource = new LeagueResource(); // 联盟的物资
    activeTech: number[] = [];            // 已激活了的科技树的id

    sumTime: number = 0;             // 今日贡献结算时间 10位时间戳

    // 是否每天首次登录
    isFirst = false;
    leagueGoods: { itemType: number, id: number, count: number }[];

    constructor(configId: number, period: number, countdownTime: number, type: number) {
        this.configId = configId;
        this.period = period;
        this.countdownTime = countdownTime;
        this.type = type;
    }

    setLeague(league: GVGLeagueType, serverNames: any) {
        if(!league) return;
        this.hasLeague = true;
        this.leagueCode = league.leagueCode;
        let leader = <RoleType>league.leader;
        this.leader = new LeagueLeaderInfo(leader, serverNames);
        this.name = league.name;
        this.icon = league.icon;
        this.notice = league.notice;
        this.guildCnt = league.guildCnt;
        this.memberCnt = league.memberCnt;
    }
    
    setLeagueParpare(leaguePrepare: GVGLeaguePrepareType) {
        if(!leaguePrepare) return; 
        this.lv = leaguePrepare.lv;
        this.resources = leaguePrepare.resources;
        this.activeTech = leaguePrepare.activeTech||[];
    }

    setPlayerInfo(userData: GVGUserDataType, auth: number, canPrepare: boolean, canChooseJob: boolean, items: GVGItem[], tasks: GVGTask[]) {
        this.myInfo.auth = auth;
        this.myInfo.canPrepare = canPrepare;
        this.myInfo.canChooseJob = canChooseJob;
        this.myInfo.items = items;
        if(userData) {
            this.myInfo.job = userData?.job||0;
            this.myInfo.receivedLv = userData?.receivedLv||0;
        }
        this.myInfo.tasks = tasks;
    }

    setFistLogin(item: { itemType: number, id: number, count: number }[]) {
        this.isFirst = true;
        this.leagueGoods = item;
    }

    setLeagueCe(leagueCe: number) {
        this.leagueCe = leagueCe;
    }

    setCities(cities: number[]) {
        this.cities = cities;
    }
}

export class LeagueListInfo {
    leagueCode: string;   // 联军唯一id
    name: string;        // 联军名
    icon: number;        // icon
    memberCnt: number;   // 玩家人数
    guildCnt: number;    // 组成军团名
    leaderName: string;  // 大将军的姓名
    leaderServerName: string; // 大将军所处小区名
    leagueCe: number;     // 联军战力
    hasApply: boolean = false; // 是否申请过了

    constructor(league: GVGLeagueType, serverNames: any) {
        if(!league) return;
        this.leagueCode = league.leagueCode;
        this.name = league.name;
        this.icon = league.icon;
        this.memberCnt = league.memberCnt;
        this.guildCnt = league.guildCnt;
        this.leaderName = (<RoleType>league.leader).roleName;
        this.leaderServerName = serverNames[(<RoleType>league.leader).serverId];
    }

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

    setLeagueCe(leagueCe: number) {
        this.leagueCe = leagueCe;
    }
}

export class LeagueSimpleInfo extends LeagueListInfo {
    notice: string;  // 公共
    cities: number[] = [];  // 占领的城池
    
    constructor(league: GVGLeagueType, serverNames: any) {
        super(league, serverNames);
        if(!league) return;
        this.notice = league.notice;
    }

    setCities(cities: number[]) {
        this.cities = cities;
    }
}

export class LeagueGuildInfo {
    guildCode: string;   // 军团唯一id
    icon: number;
    name: string;        // 军团名
    serverName: string;  // 区服名
    lv: number;          // 军团等级
    memberCnt: number;   // 人数
    leaderName: string;  // 大将军的姓名
    guildCe: number;     // 军团战力
    hasInvited: boolean; // 是否邀请过了
    active: number;

    constructor(guild: GuildType, serverNames: any) {
        if(!guild) return;
        this.guildCode = guild.code;
        this.icon = guild.icon;
        this.name = guild.name;
        this.serverName = serverNames[guild.serverId];
        this.lv = guild.lv;
        this.memberCnt = guild.memberCnt;
        let leader = <RoleType>guild.leader;
        this.leaderName = leader.roleName;
        this.guildCe = guild.guildCe;
    }

    setHasInvited(hasInvited: boolean) {
        this.hasInvited = hasInvited;
    }

    setActive(active: number) {
        this.active = active;
    }
}

export class LeagueMemberListInfo {
    roleId: string;     // 玩家id
    roleName: string;   // 玩家名
    lv: number;         // 玩家等级
    head: number;       // 玩家头像
    frame: number;      // 玩家相框
    title: number;      // 爵位
    guildName: string;  // 所处军团名
    serverName: string; // 所处小区名
    job: number = 0;        // 官职 1-贤臣 2-猛将
    auth: number = 0;       // 官职
    active: number = 0;     // 活跃
    ce: number;         // 玩家战力
    isOnline: boolean;  // 是否在线
    quitTime: number;   // 离线时间

    constructor(role: RoleType, serverNames: any) {
        if(!role) return;
        this.roleId = role.roleId;
        this.roleName = role.roleName;
        this.lv = role.lv;
        this.head = role.head;
        this.frame = role.frame;
        this.title = role.title;
        this.guildName = role.guildName;
        this.serverName = serverNames[role.serverId];
        this.ce = role.ce;
        this.quitTime = role.quitTime;
        this.isOnline = role.quitTime == role.loginTime;
    }

    setByUserData(data: GVGUserDataType) {
        if(!data) return;
        this.active = data.active;
        this.job = data.job;
    }

    setAuth(auth: number) {
        this.auth = auth;
    }
}

export class LeagueMemberDistributeInfo extends LeagueMemberListInfo{
    food: number = 0;
    mineral: number = 0;
    wood: number = 0;
    score: number = 0;

    setByUserData(data: GVGUserDataType) {
        if(!data) return;
        this.active = data.active;
        this.job = data.job;
        this.food = data.distribute?.food||0;
        this.mineral = data.distribute?.mineral||0;
        this.wood = data.distribute?.wood||0;
        this.score = data.distribute?.score||0
    }
}

export class LeagueDistributeInfo {
    job: number;        // 1-内政 2-外政
    sumDistribute: number;   // 总贡献（宝箱显示）
    receivedBox: number[];   // 已领取的宝箱id
    members: LeagueMemberDistributeInfo[];

    constructor(job: number, sumDistribute: number, receivedBox: number[]) {
        this.job = job;
        this.sumDistribute = sumDistribute||0;
        this.receivedBox = receivedBox||[];
    }

    setMembers(members: LeagueMemberDistributeInfo[]) {
        this.members = members;
    }
}

export class LeagueFarmListInfo {
    farmId: number;
    count: number;     // 已种植数量
    canHarvest: boolean;

    constructor(farmId: number, count: number, canHarvest: boolean) {
        this.farmId = farmId;
        this.count = count;
        this.canHarvest = canHarvest;
    }
}

export class LeagueField {
    fieldId: number; // 田的编号
    seedType: number; // 已种植了的种子类型 0-未种 1-小麦 2-玉米 3-水稻
    addType: number;    // 特殊加成类型 0-普通田 1-小麦加成 2-玉米 3-水稻
    harvestTime: number; // 收获时间， 10位时间戳
    unlockTime: number; // 锁定时间，会按照内政令的数量给玩家预锁定一批填，超过时间限制就不锁给他了也会分给其他人了

    constructor(leagueFarm: GVGLeagueFarmType) {
        if(!leagueFarm) return;
        this.fieldId = leagueFarm.fieldId;
        this.seedType = leagueFarm.seedType;
        this.addType = leagueFarm.addType;
        this.harvestTime = leagueFarm.harvestTime;
        this.unlockTime = leagueFarm.unlockTime;
    }
}

export class LeagueFarmMember {
    roleId: string; // 种田玩家
    roleName: string;
    guildName: string;
    serverName: string;
    count: number;      // 种植地块
    output: number;    // 预计产量
    canHelp: boolean;       // 是否可以帮收
    harvestTime: number; // 收成时间，10位时间戳

    constructor(role: RoleType, serverNames: any) {
        this.roleId = role.roleId;
        this.roleName = role.roleName;
        this.guildName = role.guildName;
        this.serverName = serverNames[role.serverId];
    }

    setByFields(fields: GVGLeagueFarmType[]) {
        this.count = fields.length;
        this.harvestTime = Math.min(...fields.map(cur => cur.harvestTime))||0;
        this.canHelp = !!fields.find(cur => cur.harvestTime < nowSeconds());
        this.output = fields.reduce((pre, cur) => (pre + cur.output||0), 0);
    }
    
    setByMines(mines: GVGLeagueFarmType[]) {
        this.count = mines.length;
        this.output = mines.reduce((pre, cur) => (pre + cur.output||0), 0);
    }
}

export interface LeagueGood {
    itemType: number,
    id: number,
    count: number
}
