package game

import (
	"fmt"
	"math"
	mergeDataCfg "server/conf/mergeData"
	"server/game/mod/order"
	"server/game/mod/sevenLogin"
	"server/msg"
	"server/pkg/github.com/name5566/leaf/log"
)

func UnitEndlessReward(p *Player) error {
	EndlessMod := p.PlayMod.getEndlessMod()
	if EndlessMod == nil {
		return fmt.Errorf("EndlessMod is nil")
	}
	Item, err := EndlessMod.GetReward()
	if err != nil {
		return err
	}
	if Item == nil {
		return fmt.Errorf("item is nil")
	}
	err = p.HandleItem(Item, msg.ITEM_POP_LABEL_Endless.String())
	if err != nil {
		return err
	}
	return nil
}

func UnitChessShop(p *Player) error {
	ChessMod := p.PlayMod.getChessMod()

	ChargeMod := p.PlayMod.getChargeMod()
	ChargeMod.InitChessShop(ChessMod.GetEmitList())
	return nil
}

func UnitOrder(p *Player) error {
	OrderMod := p.PlayMod.getOrderMod()
	ChessMod := p.PlayMod.getChessMod()
	BaseMod := p.PlayMod.getBaseMod()
	OrderMod.OrderList = make(map[int]order.Order)
	for i := 0; i < 1000; i++ {
		err := OrderMod.CreateNormalOrder(BaseMod.GetLevel(), ChessMod.GetEmitList(), BaseMod.EnergyMul)
		if err != nil {
			return err
		}
	}
	return nil
}

func UnitChess(p *Player) error {
	CardMod := p.PlayMod.getCardMod()
	for i := 0; i < 1000; i++ {
		CardId := CardMod.RandCard(3)
		fmt.Println(CardId)
	}
	return nil
}

func UnitDecorate(p *Player) error {
	PlayerBaseMod := p.GetPlayerBaseMod()
	PlayerBaseMod.Data.Level = 6
	PlayerBaseMod.Data.Exp = 50
	PlayerBaseMod.AddExp(p, 100)
	return nil
}

func UnitSevenMonthReward(p *Player) error {
	i := sevenLogin.F()
	fmt.Print(i)
	return nil
}

func UnitAllCard(p *Player) error {
	CardMod := p.PlayMod.getCardMod()
	Item, err := CardMod.OpenCardPack(5)
	if err != nil {
		return err
	}
	fmt.Print(Item)
	return nil
}

func UnitLimitProgress(p *Player) error {
	LimitedTimeEventMod := p.PlayMod.getLimitedTimeEventMod()
	LimitedTimeEventMod.Progress = 5
	LimitedTimeEventMod.ZeroUpdate(7)
	LimitedTimeEventMod.AddProgress(6)
	return nil
}

func UnitLimitedTimeEvent(p *Player) error {
	LimitedTimeEventMod := p.PlayMod.getLimitedTimeEventMod()

	mergeList := []int{246, 15}
	AddItem := LimitedTimeEventMod.GetChestReward(mergeList)
	fmt.Print(AddItem)
	return nil
}

func UnitCard(p *Player) error {
	CardMod := p.PlayMod.getCardMod()
	for i := 0; i < 1000; i++ {
		CardMod.OpenCardPack(4)
	}
	return nil
}
func UnitOrder1(p *Player, Lv, EnergyMul int) ([]string, [][]float64) {
	// ChessMod := p.PlayMod.getChessMod()
	// Emit := ChessMod.GetEmitList()
	// OrderMod := p.PlayMod.getOrderMod()
	// OrderMod.LastDiff = 1
	// OrderMod.OrderList = make(map[int]order.Order)
	// // ChessMod := p.PlayMod.getChessMod()
	// // EmitList := ChessMod.GetEmitList()
	// // fmt.Println(EmitList)
	// OrderMod.Debug = make(map[int]int)
	// for i := 0; i < 5000; i++ {
	// 	err := OrderMod.CreateNormalOrder(Lv, Emit, EnergyMul)
	// 	log.Debug("OrderMod.CreateNormalOrder %d", i)
	// 	if err != nil {
	// 		return nil, nil
	// 	}
	// }
	// I := make(map[int]int)
	// for k, v := range OrderMod.Debug {
	// 	// fmt.Printf("chessId %d, num %d\n", k, v)
	// 	ChessLv := mergeDataCfg.GetLvById(k)
	// 	I[ChessLv] += v
	// }
	// keys := make([]int, 0, len(I))
	// Sum := 0
	// for k, v := range I {
	// 	Sum += v
	// 	keys = append(keys, k)
	// }
	// sort.Ints(keys)
	// xValue := make([]string, 0)
	// yValue := make([][]float64, 0)
	// line := make([]float64, 0)
	// for _, k := range keys {
	// 	xValue = append(xValue, fmt.Sprintf("Lv %d", k))
	// 	line = append(line, float64(I[k])/float64(Sum)*100)
	// 	fmt.Printf("Lv %d, num %.2f %%\n", k, float64(I[k])/float64(Sum)*100)
	// }
	// yValue = append(yValue, line)
	// return xValue, yValue
	return nil, nil
}

func UnitOrder2(p *Player, Lv, EnergyMul int) float64 {
	ChessMod := p.PlayMod.getChessMod()
	Emit := ChessMod.GetEmitList()
	OrderMod := p.PlayMod.getOrderMod()
	OrderMod.LastDiff = 1
	OrderMod.OrderList = make(map[int]order.Order)
	// ChessMod := p.PlayMod.getChessMod()
	// EmitList := ChessMod.GetEmitList()
	// fmt.Println(EmitList)
	// OrderMod.Debug = make(map[int]int)
	for i := 0; i < 5000; i++ {
		err := OrderMod.CreateNormalOrder(Lv, Emit, EnergyMul)
		log.Debug("OrderMod.CreateNormalOrder %d", i)
		if err != nil {
			return 0
		}
	}
	Energy := 0.0
	for _, v := range OrderMod.OrderList {
		for _, v1 := range v.MergeId {
			Color := mergeDataCfg.GetColorById(v1)
			Lv := mergeDataCfg.GetLvById(v1)
			EmitId := order.GetEmitByColor(Emit, Color)
			AdjustLv := mergeDataCfg.GetAdjust(EmitId, Color)
			Energy += math.Pow(2, float64(Lv-1+AdjustLv))
		}
	}
	return Energy / float64(5000)
}

func UnitPlayroom(p *Player) error {
	PlayroomMod := p.PlayMod.getPlayroomMod()
	PlayroomMod.Physiology[1].Num = 10
	PlayroomMod.Physiology[1].Time = 1738978968
	LimitedTimePlayroomTrigger(p)
	return nil
}
