import { ChannelUser } from './../domain/ChannelUser';
import { Channel } from 'pinus';
import { getRandValueByMinMax, getRandEelm, decodeIdCntArrayStr } from '../pubUtils/util';
import { TERAPH_RANDOM } from "../consts";
import { DicTeraph } from '../pubUtils/dictionary/DicTeraph';
import { Teraph, RoleModel } from '../db/Role';
import { ROLE_SELECT } from '../consts';
import { SCHOOL } from '../pubUtils/dicParam';
import { gameData } from '../pubUtils/data';
import { SchoolModel } from '../db/School';
import { SclResultInter, SclPosInter } from '../pubUtils/interface';
const TERAPH_STRENGTHEN = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/**
 * 计算强化次数和消耗
 * @param count 强化次数
 * @param attrs 可以强化的属性名字
 * @param teraphInfo 字典表对应的神像信息
 * @param teraph 数据库内单个神像信息
 */
export function checkTeraphMaterialEnough(count: number, attrs:number[], teraphInfo: DicTeraph, teraph: Teraph) {
    let consumes = [];
    let times = 0;
    if (count < 10) {
        for (let i = 0; i < count; i++) {
            if (!attrs.length) {
                break;
            }
            let num = getRandValueByMinMax(TERAPH_RANDOM.MIN, TERAPH_RANDOM.MAX); // 强化时随机增加 2-4 属性
            let arr = getRandEelm(attrs, num); // 随机出的属性id
            let critical = getRandValueByMinMax(0, 100);//属性暴击率
            let criEffect = 1; // 暴击效果
            if (critical <= teraphInfo.criRate) 
                criEffect = teraphInfo.criEffect;
            for (let attrId of arr) {
                let val = teraph.attr.get(attrId); // 已有的强化值
                val += teraphInfo.mainAttrUp.get(attrId) * criEffect;
                let max = teraphInfo.mainAttrMax.get(attrId);
                if(val > max) {
                    val = max;
                    let attrIndex = attrs.indexOf(attrId);
                    attrs.splice(attrIndex, 1);
                }
                teraph.attr.set(attrId, val);
            }
            times++;
        }
    } else if (count == 10){
        let criticalArrTimes = getRandEelm(TERAPH_STRENGTHEN, 2);//10次中随机2次发生暴击
        for (let item of TERAPH_STRENGTHEN) {
            if (!attrs.length) {
                break;
            }
            let criEffect = 1;
            let index = criticalArrTimes.indexOf(item)
            if (index > 0) {
                criEffect = teraphInfo.criEffect;//本次是暴击 item 在 criticalArrTimes 中
            }
            let num = getRandValueByMinMax(TERAPH_RANDOM.MIN, TERAPH_RANDOM.MAX);
            let arr: number[] = getRandEelm(attrs, num);
            for (let attrId of arr) {
                let val = teraph.attr.get(attrId); // 已有的强化值
                val += teraphInfo.mainAttrUp.get(attrId) * criEffect;
                let max = teraphInfo.mainAttrMax.get(attrId);

                if(val > max) {
                    val = max;
                    let attrIndex = attrs.indexOf(attrId);
                    attrs.splice(attrIndex, 1);
                }
                teraph.attr.set(attrId, val);
            }
            times++;
        }
    } 
    //计算消耗
    for (let {id, count} of teraphInfo.upMaterial) {
        consumes.push({ id, count: count * times});
    }
    return { consumes};
}
/**
 * 
 * @param channel 
 * @param user 
 */
export function addUserToChannel(channel: Channel, user: ChannelUser) {
    const users = channel.getMembers();
    const { uid, sid } = user;
    if (users.indexOf(uid) === -1) {
        channel.add(uid, sid);
    }
}

export async function getSimpleRoleInfo(roleId: string) {
    if (!roleId) return null;
    let role = await RoleModel.findByRoleId(roleId, ROLE_SELECT.SHOW_SIMPLE, true);
    return role;
}

export async function getSimpleRoleInfos(roleIds: string[]) {
    if (!roleIds || !roleIds.length) return null;
    let roles = await RoleModel.findRoleByField('roleId', roleIds, ROLE_SELECT.SHOW_SIMPLE, true);
    return roles;
}

/**
 * 百家学宫
 * @param roleId 
 */
export async function getSchoolList(roleId: string) {

    const dicPosition = decodeIdCntArrayStr(SCHOOL.SCHOOL_POSITION, 1); // id=>isOpen

    const userSchoolList = await SchoolModel.findByRoleId(roleId);

    let school = new Array<SclResultInter>();
    gameData.school.forEach((dicSchool) => {
        let position = new Array<SclPosInter>();
        dicPosition.forEach((isOpen, dicId) => {
            let id = parseInt(dicId);

            let userSchool = userSchoolList.find(cur => cur.schoolId == dicSchool.id && cur.positionId == id);
            if (userSchool) {
                position.push({
                    id,
                    hid: userSchool.hid,
                    isOpen: userSchool.isOpen
                });
            } else {
                position.push({
                    id,
                    hid: 0,
                    isOpen: !!isOpen
                });
            }
        });

        school.push({
            id: dicSchool.id,
            position
        });
    });
    return school;
}