package mailCfg

import (
	"fmt"
	languageCfg "server/conf/language"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/gamedata"
	"server/msg"
	"strings"
)

const (
	CFG_LOGIN_BACK = "LoginBack"
	CFG_MAIL       = "Mail"

	MAIL_RECALL_ID         = 12
	MAIL_CHARGE_SEND_ID    = 13
	MAIL_CHARGE_RECEIVE_ID = 14
)

type TriggerMail struct {
	Id             int
	Title          string
	SubTitle       string
	Content        string
	EnglistTitle   string
	SubTitleEn     string
	EnglistContent string
	Items          []*item.Item
	Type           int
	Trigger        []string
}

func init() {
	gamedata.InitCfg(CFG_LOGIN_BACK)
	gamedata.InitCfg(CFG_MAIL)
}

func GetLoginBack(Id string) (int, int) {
	data, err := gamedata.GetDataByKey(CFG_LOGIN_BACK, Id)
	if err != nil {
		return 0, 0
	}
	return gamedata.GetIntValue(data, "Num1"), gamedata.GetIntValue(data, "Num2")
}

func GetTriggerMail() []*TriggerMail {
	data, err := gamedata.GetData(CFG_MAIL)
	if err != nil {
		return nil
	}
	ret := make([]*TriggerMail, 0)
	for k, v := range data {
		T := gamedata.GetStringValue(v, "Trigger")
		if T == "" {
			continue
		}
		T1 := strings.Split(T, "|")
		if len(T1) < 4 {
			continue
		}
		data := &TriggerMail{
			Id:             GoUtil.Int(k),
			Title:          gamedata.GetStringValue(v, "Title"),
			Content:        gamedata.GetStringValue(v, "Content"),
			EnglistTitle:   gamedata.GetStringValue(v, "EnglistTitle"),
			EnglistContent: gamedata.GetStringValue(v, "EnglistContent"),
			Items:          gamedata.GetItemList(v, "Items"),
			Type:           gamedata.GetIntValue(v, "Type"),
			Trigger:        strings.Split(T, "|"),
		}
		ret = append(ret, data)
	}
	return ret
}

func GetRecallMail(Title, ItemName, TitleEn, ItemNameEn string) (string, string, string, string) {
	data, err := gamedata.GetDataByIntKey(CFG_MAIL, 12)
	if err != nil {
		return "", "", "", ""
	}
	mt := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, gamedata.GetStringValue(data, "Title"))
	mc := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, gamedata.GetStringValue(data, "Content"))
	mt_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, gamedata.GetStringValue(data, "Title"))
	mc_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, gamedata.GetStringValue(data, "Content"))
	return fmt.Sprintf(mt, Title), fmt.Sprintf(mc, Title, ItemName), fmt.Sprintf(mt_en, TitleEn), fmt.Sprintf(mc_en, Title, ItemNameEn)
}

func GetChargeSendMail(PlayerName string) (string, string, string, string) {
	data, err := gamedata.GetDataByIntKey(CFG_MAIL, MAIL_CHARGE_SEND_ID)
	if err != nil {
		return "", "", "", ""
	}
	mt := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, gamedata.GetStringValue(data, "Title"))
	mc := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, gamedata.GetStringValue(data, "Content"))
	mt_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, gamedata.GetStringValue(data, "Title"))
	mc_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, gamedata.GetStringValue(data, "Content"))
	return mt, fmt.Sprintf(mc, PlayerName), mt_en, fmt.Sprintf(mc_en, PlayerName)
}

func GetChargeReceiveMail(PlayerName string, Content string) (string, string, string, string) {
	data, err := gamedata.GetDataByIntKey(CFG_MAIL, MAIL_CHARGE_RECEIVE_ID)
	if err != nil {
		return "", "", "", ""
	}
	mt := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, gamedata.GetStringValue(data, "Title"))
	mc := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, gamedata.GetStringValue(data, "Content"))
	mt_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, gamedata.GetStringValue(data, "Title"))
	mc_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, gamedata.GetStringValue(data, "Content"))
	return fmt.Sprintf(mt, PlayerName), fmt.Sprintf(mc, Content), fmt.Sprintf(mt_en, PlayerName), fmt.Sprintf(mc_en, Content)
}
