// 任务
import { RewardInter } from '../interface';
import { readFileAndParse, parseNumberList, parseGoodStr } from '../util';
import { FILENAME, TASK_FUN_TYPE } from '../../consts';
const _ = require('lodash');

type KeysEnum<T> = { [P in keyof Required<T>]: true };

interface DicTaskBase {
    // id
    readonly id: number;
    // 任务类型
    readonly taskType: number;
    // 类型下面的分组
    group: string;
    // 任务参数
    readonly taskParam: number[];
    // 条件
    readonly condition: number;
}

const DicTaskKeys: KeysEnum<DicTaskBase> = {
    id: true,
    taskType: true,
    group: true,
    taskParam: true,
    condition: true
};

// 主线任务
export interface DicMainTask extends DicTaskBase {
    // 奖励
    readonly taskReward: RewardInter[];
    // 任务阶段
    readonly taskStage: number;
}

const DicMainTaskKeys: KeysEnum<DicMainTask> = {
    id: true,
    taskType: true,
    group: true,
    taskParam: true,
    condition: true,
    taskStage: true,
    taskReward: true
};

// 每日任务
export interface DicDailyTask extends DicTaskBase {
    // 奖励
    readonly taskReward: RewardInter[];
    // 活跃
    readonly point: number;
    // 经验基数
    readonly exp: RewardInter;
}

const DicDailyTaskKeys: KeysEnum<DicDailyTask> = {
    id: true,
    taskType: true,
    group: true,
    taskParam: true,
    condition: true,
    taskReward: true,
    point: true,
    exp: true,
};

// 成就
export interface DicAchievement extends DicTaskBase {
    // 奖励
    readonly taskReward: RewardInter[];
    // 活跃
    readonly point: number;
}

const DicAchievementKeys: KeysEnum<DicAchievement> = {
    id: true,
    taskType: true,
    group: true,
    taskParam: true,
    condition: true,
    taskReward: true,
    point: true
};


// pvp任务
export interface DicPvpDailyTask extends DicTaskBase {
    // 奖励
    readonly taskReward: RewardInter[];
}

const DicPvpDailyTaskKeys: KeysEnum<DicPvpDailyTask> = {
    id: true,
    taskType: true,
    group: true,
    taskParam: true,
    condition: true,
    taskReward: true
};

export type DicTask = DicTaskBase & { type: number };

export const dicMainTask = new Map<number, DicMainTask>(); // 主线任务
export const dicDailyTask = new Map<number, DicDailyTask>(); // 每日任务
export const dicAchievement = new Map<number, DicAchievement>(); // 成就
export const dicPvpDailyTask = new Map<number, DicDailyTask>(); // 成就
export const taskMap = new Map<number, Map<number, DicMainTask | DicDailyTask | DicAchievement | DicDailyTask>>();
taskMap.set(TASK_FUN_TYPE.MAIN, dicMainTask);
taskMap.set(TASK_FUN_TYPE.DAILY, dicDailyTask);
taskMap.set(TASK_FUN_TYPE.ACHIEVEMENT, dicAchievement);
taskMap.set(TASK_FUN_TYPE.PVP, dicPvpDailyTask);

export const dicTaskType = new Map<number, DicTask[]>();
export function loadTask() {
    dicTaskType.clear();
    dicMainTask.clear();
    dicDailyTask.clear();
    dicAchievement.clear();
    dicPvpDailyTask.clear();

    const arrMainTask = readFileAndParse(FILENAME.DIC_MAIN_TASK);

    arrMainTask.forEach(o => {
        o.taskParam = parseNumberList(o.taskParam);
        o.taskReward = parseGoodStr(o.taskReward);
        dicMainTask.set(o.id, _.pick(o, Object.keys(DicMainTaskKeys)));
        pushDicTaskType(o.taskType, TASK_FUN_TYPE.MAIN, o);
    });

    const arrDailyTask = readFileAndParse(FILENAME.DIC_DAILY_TASK);
    arrDailyTask.forEach(o => {
        o.taskParam = parseNumberList(o.taskParam);
        o.taskReward = parseGoodStr(o.taskReward);
        o.exp = parseExp(o.exp);
        dicDailyTask.set(o.id, _.pick(o, Object.keys(DicDailyTaskKeys)));
        pushDicTaskType(o.taskType, TASK_FUN_TYPE.DAILY, o);
    });

    const arrAchievement = readFileAndParse(FILENAME.DIC_ACHIEVEMENT);
    arrAchievement.forEach(o => {
        o.taskParam = parseNumberList(o.taskParam);
        o.taskReward = parseGoodStr(o.taskReward);
        dicAchievement.set(o.id, _.pick(o, Object.keys(DicAchievementKeys)));
        pushDicTaskType(o.taskType, TASK_FUN_TYPE.ACHIEVEMENT, o);
    });

    const arrPvpDailyTask = readFileAndParse(FILENAME.DIC_PVP_DAILY_TASK);
    arrPvpDailyTask.forEach(o => {
        o.taskParam = parseNumberList(o.taskParam);
        o.taskReward = parseGoodStr(o.taskReward);
        o.group = 1;
        dicPvpDailyTask.set(o.id, _.pick(o, Object.keys(DicPvpDailyTaskKeys)));
        pushDicTaskType(o.taskType, TASK_FUN_TYPE.PVP, o);
    });
}

function pushDicTaskType(taskType: number, type: number, o: any) {
    if (!dicTaskType.has(taskType)) {
        dicTaskType.set(taskType, new Array<DicTask>());
    }
    let newObj = _.pick(o, Object.keys(DicTaskKeys));
    newObj.type = type;
    dicTaskType.get(taskType).push(newObj)
}

function parseExp(exp: string) {
    let [ id, count ] = parseNumberList(exp);
    return {id, count}
}