package orderCfg

import (
	"server/gamedata"
	"sort"
	"strconv"
)

const (
	CFG_ORDER_DATA       = "OrderData"
	CFG_ORDER_CHESS_DATA = "OrderChessData"
	CFG_ORDER_NUM_DATA   = "OrderNumData"
	CFG_START_ORDER      = "StartOrder"
	CFG_CONST            = "OrderConst"
	CFG_ORDER_SCENE      = "OrderScene"
	CFG_ORDER_K          = "OrderK"
)

func init() {
	gamedata.InitCfg(CFG_ORDER_DATA)
	gamedata.InitCfg(CFG_ORDER_CHESS_DATA)
	gamedata.InitCfg(CFG_ORDER_NUM_DATA)
	gamedata.InitCfg(CFG_START_ORDER)
	gamedata.InitCfg(CFG_CONST)
	gamedata.InitCfg(CFG_ORDER_SCENE)
	gamedata.InitCfg(CFG_ORDER_K)
}

func GetOrderK(Lv int) int {
	data, err := gamedata.GetDataByIntKey(CFG_ORDER_K, Lv)
	if err != nil {
		return 0
	}
	Energy := gamedata.GetIntValue(data, "Energy")
	K := gamedata.GetFloatValue(data, "K")
	a := float64(Energy)*K/10 + 0.5
	return int(a) * 10
}

func GetOrderFactor(Scene int) int {
	data, err := gamedata.GetDataByIntKey(CFG_ORDER_SCENE, Scene)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Factor")
}

func GetEmitExtraOrder() string {
	data, err := gamedata.GetDataByKey(CFG_CONST, "EmitId_Extra_Order")
	if err != nil {
		return ""
	}
	return gamedata.GetStringValue(data, "Value")
}

func GetLvMin(EnergyMul int) int {
	data, err := gamedata.GetDataByIntKey(CFG_ORDER_DATA, EnergyMul)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "LvMin")
}

func GetLvMax(EnergyMul, N int) int {
	data, err := gamedata.GetData(CFG_ORDER_CHESS_DATA)
	if err != nil {
		return 0
	}
	for _, v := range data {
		dEnergy := gamedata.GetIntValue(v, "EnergyMul")
		Min := gamedata.GetIntValue(v, "MinN")
		Max := gamedata.GetIntValue(v, "MaxN")
		if dEnergy == EnergyMul {
			if N >= Min && N <= Max {
				return gamedata.GetIntValue(v, "MaxLv")
			}
		}
	}
	return 0
}

func GetOrderNum(Level int) int {
	data, err := gamedata.GetData(CFG_ORDER_NUM_DATA)
	if err != nil {
		return 0
	}
	for _, v := range data {
		Min := gamedata.GetIntValue(v, "Min")
		Max := gamedata.GetIntValue(v, "Max")
		if Level >= Min && Level <= Max {
			return gamedata.GetIntValue(v, "Num")
		}
	}
	return 0
}

func GetStartOrderInfo(Id int) (int, int) {
	data, err := gamedata.GetDataByIntKey(CFG_START_ORDER, Id)
	if err != nil {
		return -1, -1
	}
	return gamedata.GetIntValue(data, "group"), gamedata.GetIntValue(data, "step")
}

func GetStartOrderList() []*gamedata.StartOrderData {
	data, err := gamedata.GetData(CFG_START_ORDER)
	if err != nil {
		return []*gamedata.StartOrderData{}
	}
	ret := make([]*gamedata.StartOrderData, 0, len(data))
	for k, v := range data {
		OrderId, _ := strconv.Atoi(k)
		ret = append(ret, &gamedata.StartOrderData{
			Id:        OrderId,
			MergeList: gamedata.GetIntSliceValue(v, "merge_id_list"),
			Appear:    gamedata.GetStringValue(v, "appear"),
			Preview:   gamedata.GetStringValue(v, "preview"),
			Items:     gamedata.GetItemList(v, "reward"),
			Group:     gamedata.GetIntValue(v, "group"),
			Step:      gamedata.GetIntValue(v, "step"),
		})
	}
	sort.Slice(ret, func(i, j int) bool {
		return ret[i].Id < ret[j].Id
	})
	return ret
}

func GetStartOrderByStep(id int) []*gamedata.StartOrderData {
	data, err := gamedata.GetData(CFG_START_ORDER)
	if err != nil {
		return []*gamedata.StartOrderData{}
	}
	ret := make([]*gamedata.StartOrderData, len(data))
	for k, v := range data {
		stepVal := gamedata.GetIntValue(v, "step")
		if int(stepVal) == id {
			OrderId, _ := strconv.Atoi(k)
			ret = append(ret, &gamedata.StartOrderData{
				Id:        OrderId,
				Step:      int(stepVal),
				MergeList: gamedata.GetIntSliceValue(v, "merge_id_list"),
			})
		}
	}
	sort.Slice(ret, func(i, j int) bool {
		return ret[i].Id < ret[j].Id
	})
	return ret
}

func GetStartOrderById(id int) *gamedata.StartOrderData {
	data, err := gamedata.GetData(CFG_START_ORDER)
	if err != nil {
		return nil
	}
	v, ok := data[strconv.Itoa(id)]
	if !ok {
		return nil
	}
	stepVal := gamedata.GetIntValue(v, "step")
	return &gamedata.StartOrderData{
		Id:        id,
		Step:      int(stepVal),
		MergeList: gamedata.GetIntSliceValue(v, "merge_id_list"),
	}
}

func GetMaxStep() int {
	data, err := gamedata.GetData(CFG_START_ORDER)
	if err != nil {
		return 0
	}
	max := 0
	for _, v := range data {
		stepVal := gamedata.GetIntValue(v, "step")
		if int(stepVal) > max {
			max = int(stepVal)
		}
	}
	return max
}

func GetOrderType(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_START_ORDER, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "type")
}
