package gui.tw.diaoyu;

import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil;
import handling.world.MapleParty;
import provider.MapleDataTool;
import scripting.PortalPlayerInteraction;
import scripting.PortalScript;
import server.MapleItemInformationProvider;
import server.life.MapleLifeFactory;
import server.maps.MapleMap;
import server.maps.MapleMapFactory;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class DiaoyuConfig {



    //包含高级鱼
    List<DiaoyuItem> items = new ArrayList<>();

    //不包含高级鱼
    List<DiaoyuItem> items2 = new ArrayList<>();

    List<DiaoyuItem> baodi = new ArrayList<>();
    List<DiaoyuItem> baodi2 = new ArrayList<>();

    List<DiaoYuMap> mapIds=new ArrayList<>();

    public List<DiaoYuMap> getMapIds() {
        return mapIds;
    }

    public void setMapIds(List<DiaoYuMap> mapIds) {
        this.mapIds.clear();
        for (DiaoYuMap mapId : mapIds) {
            mapId.setName(MapleDataTool.getString("mapName", MapleMapFactory.nameData.getChildByPath(getMapStringName(mapId.getMapId())), ""));
            this.mapIds.add(mapId);
        }
    }

    List<BL> bls=new ArrayList<>();

    List<DiaoyuItem>  All= new ArrayList<>();


    int count = 0;

    int count2 = 0;


    int dykg = 1;
    int ryyz = 1;

    int dytx = 1;

    int kjbzdc = 1;

    int bldj = 1;

    int dyyg = 5340001;

    int dyyz = 3011000;

    int dyjg = 1000;

    int ptye = 2300000;

    int gjye = 2300001;
    //公告
    String notice = "";


    public List<BL> getBls() {
        return bls;
    }

    public void setBls(List<BL> bls) {
        this.bls = bls;
    }

    public void add(DiaoyuItem item) {
        All.add(item);
        if (item.getItemId()>0) {
            item.setItemName(MapleItemInformationProvider.getInstance().getName(item.getItemId()));
        }else if (item.getItemId() == 0){
            item.setItemName("金币");
        }else if (item.getItemId() ==-1){
            item.setItemName("经验");
        }else if (item.getItemId() == -2){
            item.setItemName("抵用券");
        }else if (item.getItemId() == -3){
            item.setItemName("点券");
        }


        if (item.getChance()>0){
            item.setChance2(item.getChance());
            this.items.add(item);
            count +=item.getChance();
            //根据几率进行排序
            items = items.stream().sorted(Comparator.comparingInt(DiaoyuItem::getChance)).collect(Collectors.toList());
            if (item.getIsAdvanced()!=1){
                items2.add(item);
                count2 +=item.getChance();
            }
            items2 = items2.stream().sorted(Comparator.comparingInt(DiaoyuItem::getChance)).collect(Collectors.toList());
        }else {
            item.setChance2(1);
            this.baodi.add(item);
            if (item.getIsAdvanced()!=1) {
                this.baodi2.add(item);
            }
        }
    }

    public List<DiaoyuItem> getAll() {
        return new ArrayList<>(this.All);
    }

    public void setAll(List<DiaoyuItem> all) {
        items.clear();
        items2.clear();
        baodi.clear();
        baodi2.clear();
        All.clear();
        for (DiaoyuItem diaoyuItem : all) {
            add(diaoyuItem);
        }
    }


    public DiaoyuItem getItem(boolean gj) {
        int c = RandomUtil.randomInt(1, 1000000);
        if (gj){
            if (c<count){
                return qzsj(items);
            }else {
                return qzsj(baodi);
            }
        }else {
            if (c<count2){
                return qzsj(items2);
            }else {
                return qzsj(baodi2);
            }
        }
    }

    public DiaoyuItem qzsj(List<DiaoyuItem> items) {
        List<WeightRandom.WeightObj<DiaoyuItem>> weightList = new ArrayList<>();
        for (DiaoyuItem item : items) {
            weightList.add(new WeightRandom.WeightObj<>(item, item.getChance2()));
        }

        WeightRandom<DiaoyuItem> weightRandom = RandomUtil.weightRandom(weightList);
        DiaoyuItem selectedItem = weightRandom.next();
        return selectedItem;
    }


    public boolean  isDyMap(int mapId) {
        return mapIds.stream().anyMatch(mapId1 -> mapId1.getMapId() == mapId);
    }

    public int getDykg() {
        return dykg;
    }

    public void setDykg(int dykg) {
        this.dykg = dykg;
    }

    public int getRyyz() {
        return ryyz;
    }

    public void setRyyz(int ryyz) {
        this.ryyz = ryyz;
    }

    public int getDytx() {
        return dytx;
    }

    public void setDytx(int dytx) {
        this.dytx = dytx;
    }

    public int getKjbzdc() {
        return kjbzdc;
    }

    public void setKjbzdc(int kjbzdc) {
        this.kjbzdc = kjbzdc;
    }

    public int getBldj() {
        return bldj;
    }

    public void setBldj(int bldj) {
        this.bldj = bldj;
    }

    public int getDyyg() {
        return dyyg;
    }

    public void setDyyg(int dyyg) {
        this.dyyg = dyyg;
    }

    public int getDyyz() {
        return dyyz;
    }

    public void setDyyz(int dyyz) {
        this.dyyz = dyyz;
    }

    public int getDyjg() {
        return dyjg;
    }

    public void setDyjg(int dyjg) {
        this.dyjg = dyjg;
    }

    public int getPtye() {
        return ptye;
    }

    public void setPtye(int ptye) {
        this.ptye = ptye;
    }

    public String getNotice() {
        return notice;
    }

    public void setNotice(String notice) {
        this.notice = notice;
    }

    public int getGjye() {
        return gjye;
    }

    public void setGjye(int gjye) {



        this.gjye = gjye;
    }


    private static String getMapStringName(final int mapid) {
        final StringBuilder builder = new StringBuilder();
        if (mapid < 100000000) {
            builder.append("maple");
        } else if ((mapid >= 100000000 && mapid < 200000000) || mapid / 100000 == 5540) {
            builder.append("victoria");
        } else if (mapid >= 200000000 && mapid < 300000000) {
            builder.append("ossyria");
        } else if (mapid >= 300000000 && mapid < 400000000) {
            builder.append("elin");
        } else if (mapid >= 500000000 && mapid < 510000000) {
            builder.append("thai");
        } else if (mapid >= 540000000 && mapid < 600000000) {
            builder.append("SG");
        } else if (mapid >= 600000000 && mapid < 620000000) {
            builder.append("MasteriaGL");
        } else if ((mapid >= 670000000 && mapid < 677000000) || (mapid >= 678000000 && mapid < 682000000)) {
            builder.append("global");
        } else if (mapid >= 677000000 && mapid < 678000000) {
            builder.append("Episode1GL");
        } else if (mapid >= 682000000 && mapid < 683000000) {
            builder.append("HalloweenGL");
        } else if (mapid >= 683000000 && mapid < 684000000) {
            builder.append("event");
        } else if (mapid >= 684000000 && mapid < 685000000) {
            builder.append("event_5th");
        } else if (mapid >= 700000000 && mapid < 700000300) {
            builder.append("wedding");
        } else if (mapid >= 800000000 && mapid < 900000000) {
            builder.append("jp");
        } else if (mapid >= 700000000 && mapid < 782000002) {
            builder.append("chinese");
        } else {
            builder.append("etc");
        }
        builder.append("/");
        builder.append(mapid);
        return builder.toString();
    }

}
