package mail

import (
	"fmt"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/msg"
)

type MailMod struct {
	List        map[int]*MailInfo // 邮件列表
	AutoId      int               // 自增id
	ServerMail  []int             // 后台邮件
	TriggerMail []int             // 触发邮件
}

const (
	MAIL_STATUS_IDLE   = 0
	MAIL_STATUS_READ   = 1
	MAIL_STATUS_REWARD = 2
	MAIL_STATUS_DEL    = 3

	MAIL_TYPE_NORMAL   = 1
	MAIL_TYPE_FESTIVAL = 2
	MAIL_TYPE_GIFT     = 3
)

type MailInfo struct {
	Title      string // 邮件标题
	SubTitle   string // 邮件副标题
	Content    string // 邮件内容
	TitleEn    string // 邮件标题英文
	SubTitleEn string // 邮件副标题英文
	ContentEn  string // 邮件内容英文
	// 葡萄牙语 巴西
	TitlePtBr    string
	SubTitlePtBr string
	ContentPtBr  string
	// 西班牙语 拉丁美洲
	TitleEsLatam    string
	SubTitleEsLatam string
	ContentEsLatam  string

	Items  []*item.Item // 邮件道具
	Type   int          //邮件类型
	Send   int64        // 发送时间
	Del    int64        // 删除时间
	Status int
}

func (m *MailMod) InitData() {
	if m.List == nil {
		m.List = make(map[int]*MailInfo)
	}
}

type MailStruct struct {
	Title    string
	SubTitle string
	Content  string
	// 英文
	TitleEn    string
	SubTitleEn string
	ContentEn  string
	// 葡萄牙语 巴西
	TitlePtBr    string
	SubTitlePtBr string
	ContentPtBr  string

	// 西班牙语 拉丁美洲
	TitleEsLatam    string
	SubTitleEsLatam string
	ContentEsLatam  string

	Items []*item.Item
	Type  int
}

func (m *MailMod) SendMail(mail *MailStruct) int {
	if mail == nil {
		return 0
	}
	m.AutoId++
	m.List[m.AutoId] = &MailInfo{
		Title:           mail.Title,
		SubTitle:        mail.SubTitle,
		Content:         mail.Content,
		TitleEn:         mail.TitleEn,
		SubTitleEn:      mail.SubTitleEn,
		ContentEn:       mail.ContentEn,
		TitlePtBr:       mail.TitlePtBr,
		SubTitlePtBr:    mail.SubTitlePtBr,
		ContentPtBr:     mail.ContentPtBr,
		TitleEsLatam:    mail.TitleEsLatam,
		SubTitleEsLatam: mail.SubTitleEsLatam,
		ContentEsLatam:  mail.ContentEsLatam,
		Items:           mail.Items,
		Send:            GoUtil.Now(),
		Type:            mail.Type,
	}
	return m.AutoId
}

// 阅读邮件
func (m *MailMod) Read(id int) error {
	v, ok := m.List[id]
	if !ok {
		return fmt.Errorf("mail id %d not exist", id)
	}
	if v.Status == MAIL_STATUS_IDLE {
		v.Status = MAIL_STATUS_READ
	}
	return nil
}

// 领取邮件奖励
func (m *MailMod) Reward(id int) ([]*item.Item, error) {
	v, ok := m.List[id]
	if !ok {
		return nil, fmt.Errorf("mail id %d not exist", id)
	}
	if v.Status != MAIL_STATUS_IDLE {
		return nil, fmt.Errorf("mail not readed")
	}
	v.Status = MAIL_STATUS_REWARD
	return v.Items, nil
}

// 删除邮件
func (m *MailMod) Del(id int) error {
	v, ok := m.List[id]
	if !ok {
		return fmt.Errorf("mail id %d not exist", id)
	}
	if v.Status == MAIL_STATUS_DEL {
		return fmt.Errorf("mail have deleted")
	}
	v.Status = MAIL_STATUS_DEL
	v.Del = GoUtil.Now()
	return nil
}

func (m *MailMod) BackData() *msg.ResMailList {
	res := &msg.ResMailList{
		MailList: make(map[int32]*msg.MailInfo, len(m.List)),
	}
	for k, v := range m.List {
		if v.Status == MAIL_STATUS_DEL {
			continue
		}
		res.MailList[int32(k)] = mailInfoToMsg(k, v)
	}
	return res
}

func (m *MailMod) NotifyMail(Id int) *msg.MailNotify {
	mailInfo, ok := m.List[Id]
	if !ok {
		return nil
	}
	return &msg.MailNotify{
		Info: mailInfoToMsg(Id, mailInfo),
	}
}

func mailInfoToMsg(id int, v *MailInfo) *msg.MailInfo {
	return &msg.MailInfo{
		Id:           int32(id),
		Title:        v.Title,
		SubTitle:     v.SubTitle,
		Content:      v.Content,
		TitleEn:      v.TitleEn,
		SubTitleEn:   v.SubTitleEn,
		ContentEn:    v.ContentEn,
		TitlePtBr:    v.TitlePtBr,
		SubTitlePtBr: v.SubTitlePtBr,
		ContentPtBr:  v.ContentPtBr,
		TitleEsLa:    v.TitleEsLatam,
		SubTitleEsLa: v.SubTitleEsLatam,
		ContentEsLa:  v.ContentEsLatam,
		Type:         int32(v.Type),
		Items:        item.ItemToMsg(v.Items),
		Status:       int32(v.Status),
		Time:         int32(v.Send),
	}
}

func (m *MailMod) GetTriggerMail() []int {
	return m.TriggerMail
}

func (m *MailMod) AddTriggerMail(i int) {
	m.TriggerMail = append(m.TriggerMail, i)
}
