package gmdata

//宝物
import (
	"encoding/json"
	"fmt"
	"leafstalk/log"
	"os"
	"strconv"
)

// DropedTreasure 拥有的宝物
type DropedTreasure struct {
	RoleID     int64   `json:"roleId"`     // 佩戴角色
	TID        int64   `json:"tId"`        // 宝物id
	LuckyValue float64 `json:"luckyValue"` // 幸运值
}

type Treasure struct {
	Id      int64  `json:"number"`
	Quality int    `json:"quality"`
	Name    string `json:"name"`
	//Icon                string `json:"icon"`
	//CombatEffe          int    `json:"combatEffe"`
	//CombatEffeUp        string `json:"combatEffe_up"`
	//Blood               int    `json:"blood"`
	//BloodLevup          string `json:"blood_Levup"`
	//ElementType         int    `json:"elementType"`
	//ElementDamage       int    `json:"elementDamage"`
	//ElementDamageLevup  string `json:"elementDamage_Levup"`
	//GoldReduction       int    `json:"goldReduction"`
	//GoldReductionLevup  string `json:"goldReduction_Levup"`
	//WoodReduction       int    `json:"woodReduction"`
	//WoodReductionLevup  string `json:"woodReduction_Levup"`
	//WaterReduction      int    `json:"waterReduction"`
	//WaterReductionLevup string `json:"waterReduction_Levup"`
	//FireReduction       int    `json:"fireReduction"`
	//FireReductionLevup  string `json:"fireReduction_Levup"`
	//SoilReduction       int    `json:"soilReduction"`
	//SoilReductionLevup  string `json:"soilReduction_Levup"`
	//NowEntries          string `json:"Now_Entries"`
}

var (
	Treasures []*Treasure
)

// LoadTreasure 加载配置信息
func LoadTreasure(confPath string) {
	loadTreasureFile(confPath+"/"+"Treasure.json", &Treasures)
}

func loadTreasureFile(filePath string, val interface{}) {
	ptrFile, err := os.Open(filePath)
	if err != nil {
		log.Fatalln("读取json文件失败", err)
	}
	defer ptrFile.Close()

	decoder := json.NewDecoder(ptrFile)
	if err = decoder.Decode(&val); err != nil {
		log.Fatalln("loadTreasureFile Decoder failed ", filePath, err.Error())
	}
}

// GetTreasure 获取指定宝物
func GetTreasure(id int64) *Treasure {
	for _, v := range Treasures {
		if v.Id == id {
			return v
		}
	}
	return nil
}

// GetTreasureName 获取指定宝物名称
func GetTreasureName(id int64) string {
	for _, v := range Treasures {
		if v.Id == id {
			return v.Name
		}
	}
	return ""
}

// GetTreasureId 获取宝物类型值ID
func GetTreasureId(tid int64) int64 {
	// 转换数字为字符串
	numStr := strconv.FormatInt(tid, 10)

	// 检查数字是否足够长
	if len(numStr) < 11 {
		return 0
	}

	// 提取第三位和第四位数字
	digitStr := numStr[2:4]

	// 将提取的字符串转换为整数
	digits, err := strconv.Atoi(digitStr)
	if err != nil {
		return 0
	}

	return int64(digits)
}

// GetTreasureLevel 获取宝物等级
func GetTreasureLevel(tid int64) int64 {
	// 取模运算获取最后三位数字
	lastThreeDigits := tid % 1000
	return lastThreeDigits
}

// GetTreasureStar 获取宝物星级
func GetTreasureStar(tid int64) int64 {
	strNum := fmt.Sprintf("%011d", tid)

	if len(strNum) < 11 {
		return -1
	}

	digitStr := strNum[6:8]

	var digit int64
	_, err := fmt.Sscanf(digitStr, "%02d", &digit)
	if err != nil {
		fmt.Println("输入的数字格式不正确")
		return -1
	}

	if digit < 10 || digit == 0 {
		return digit
	}

	return -1
}

// GetTreasureByTID 获取指定宝物名称
func GetTreasureByTID(tid int64) string {
	return GetTreasureName(GetTreasureId(tid))
}
