package forms

import (
	"errors"
	"github.com/spf13/cast"
	"strings"
)

type UserAccountGetReq struct {
	ID int64 `form:"playerid" json:"playerid" binding:"required"`
}

type UserAccountListReq struct {
	ListReq
	ChannelId      string   `json:"channel_id" form:"channel_id"`
	ID             int64    `json:"id" form:"id"`
	Nickname       string   `json:"nickname" form:"nickname"`
	OpenId         string   `json:"openId" form:"openId"`
	Ban            int32    `json:"ban" form:"ban"`
	Createtime     []int64  `json:"createtime" form:"createtime[]"`
	Diamond        []string `json:"diamond" form:"diamond[]"`
	DiamondBetween []float64
	Coin           []string `json:"coin" form:"coin[]"`
	CoinBetween    []float64
	Level          []string `json:"level" form:"level[]"`
	LevelBetween   []int32
	Stamina        []string `json:"stamina" form:"stamina[]"`
	StaminaBetween []int32
	Sorter         []SorterModel `json:"sorter" form:"sorter[]"`
	ServerId       int           `json:"server_id" form:"server_id"`
}

type UserAccountBanReq struct {
	ID     int64  `json:"playerid" form:"playerid"`
	Ban    int32  `json:"ban" form:"ban"`
	Reason string `json:"reason" form:"reason"`
}

type GmSetPaySwitch struct {
	PlayerID int64  `json:"playerid"`
	Switch   int    `json:"switch"` //0:关 1：开
	Reason   string `json:"reason"`
}

type BanChatReq struct {
	PlayerID     int64  `json:"playerid" form:"playerid"`
	Ban          int32  `json:"ban" form:"ban"`
	DeadlineTime int64  `json:"deadline_time" form:"deadline_time"`
	Reason       string `json:"reason" form:"reason"`
}

func (req *UserAccountListReq) Check() error {
	if req.Diamond != nil && len(req.Diamond) != 2 {
		return errors.New("钻石必须选择一个区间或者留空")
	}
	if req.Coin != nil && len(req.Coin) != 2 {
		return errors.New("金币必须选择一个区间或者留空")
	}
	if req.Level != nil && len(req.Level) != 2 {
		return errors.New("等级必须选择一个区间或者留空")
	}
	if req.Createtime != nil && len(req.Createtime) != 2 {
		return errors.New("注册时间必须选择一个区间或者留空")
	}

	if len(req.Coin) == 2 {
		for i := 0; i < len(req.Coin); i++ {
			var (
				suffix string
				coin   float64
			)
			req.Coin[i] = strings.TrimSpace(req.Coin[i])
			suffix = req.Coin[i][len(req.Coin[i])-1:]
			if suffix == "K" || suffix == "k" {
				coin = cast.ToFloat64(req.Coin[i][:len(req.Coin[i])-1])
				coin = coin * 1000
			} else if suffix == "M" || suffix == "m" {
				coin = cast.ToFloat64(req.Coin[i][:len(req.Coin[i])-1])
				coin = coin * 1000 * 1000
			} else {
				coin = cast.ToFloat64(req.Coin[i])
			}
			req.CoinBetween = append(req.CoinBetween, coin)
		}
	}

	if len(req.Diamond) == 2 {
		for i := 0; i < len(req.Diamond); i++ {
			var (
				suffix  string
				diamond float64
			)
			req.Diamond[i] = strings.TrimSpace(req.Diamond[i])
			suffix = req.Diamond[i][len(req.Diamond[i])-1:]
			if suffix == "K" || suffix == "k" {
				diamond = cast.ToFloat64(req.Diamond[i][:len(req.Diamond[i])-1])
				diamond = diamond * 1000
			} else if suffix == "M" || suffix == "m" {
				diamond = cast.ToFloat64(req.Diamond[i][:len(req.Diamond[i])-1])
				diamond = diamond * 1000 * 1000
			} else {
				diamond = cast.ToFloat64(req.Diamond[i])
			}
			req.DiamondBetween = append(req.DiamondBetween, diamond)
		}
	}

	if len(req.Createtime) == 2 {
		if req.Createtime[0] > req.Createtime[1] {
			return errors.New("注册时间选择的区间值不合理")
		}
		req.Createtime[0] = req.Createtime[0] / 1000
		req.Createtime[1] = req.Createtime[1] / 1000
	}

	if len(req.Level) == 2 {
		if req.Level[0] > req.Level[1] {
			return errors.New("等级选择的区间值不合理")
		}

		req.LevelBetween = []int32{cast.ToInt32(req.Level[0]), cast.ToInt32(req.Level[1])}
	}

	if len(req.Stamina) == 2 {
		if req.Stamina[0] > req.Stamina[1] {
			return errors.New("体力选择的区间值不合理")
		}

		req.StaminaBetween = []int32{cast.ToInt32(req.Stamina[0]), cast.ToInt32(req.Stamina[1])}
	}
	return nil
}

type UserAccountListRes struct {
	ListRes
}

type UserAccountSearchReq struct {
	ListReq
	ID     int64  `json:"id" form:"id"`
	OpenId string `json:"openId" form:"openId"`
	AccId  int64  `json:"accId" form:"accId"`
}

func (req *UserAccountSearchReq) Check() error {
	if req.AccId > 0 {
		req.ID = 0
		req.OpenId = ""
	}

	if req.OpenId != "" {
		req.ID = 0
	}

	req.OpenId = strings.ReplaceAll(req.OpenId, "@wx", "")
	return nil
}

type UserAccountSearchRes struct {
	ListRes
}

type ChatReportListReq struct {
	ListReq
	ServerID        int32         `json:"server_id" form:"server_id"`
	ClanServerID    int32         `json:"clan_server_id" form:"clan_server_id"`
	ClanChatChannel int32         `json:"clan_chat_channel" form:"clan_chat_channel"`
	PlayerID        int64         `json:"player_id" form:"player_id"`
	ClanPlayerID    int64         `json:"clan_player_id" form:"clan_player_id"`
	CreateTime      []int64       `json:"create_time" form:"create_time[]"`
	ClanTime        []int64       `json:"clan_time" form:"clan_time[]"`
	Sorter          []SorterModel `json:"sorter" form:"sorter[]"`
	IsExport        int           `json:"is_export" form:"is_export"`
}

type ChatLogListReq struct {
	ServerID    int32 `json:"server_id" form:"server_id"`
	ChatChannel int32 `json:"chat_channel" form:"chat_channel"`
	BeforeID    int64 `json:"before_id" form:"before_id"`
	CurrentID   int64 `json:"current_id" form:"current_id"`
	LaterID     int64 `json:"later_id" form:"later_id"`
}

type DownloadChatLogReq struct {
	Path string `json:"path" form:"path"`
}

type ChatMsgListReq struct {
	ListReq
	ServerID    int32   `json:"server_id" form:"server_id"`
	ChatChannel []int32 `json:"chat_channel" form:"chat_channel[]"`
	PlayerID    int64   `json:"player_id" form:"player_id"`
	CreateTime  []int64 `json:"create_time" form:"create_time[]"`
	IsExport    int     `json:"is_export" form:"is_export"`
}

type ChatMsgListRet struct {
	ID          int64  `json:"id"`
	PlayerID    int64  `json:"player_id"`    // 玩家id
	NickName    string `json:"nick_name"`    // 玩家昵称
	HeadID      int32  `json:"head_id"`      // 头像 -1 代表使用 AvatarURL 微信授权头像
	FrameID     int32  `json:"frame_id"`     // 头像框id
	HeadImg     string `json:"head_img"`     // 头像
	CreateTime  int64  `json:"create_time"`  // 产生时间(毫秒)
	ChatChannel int32  `json:"chat_channel"` // 频道类型
	ChannelName string `json:"channel_name"` // 频道名称
	ChatID      string `json:"chat_id"`      // 聊天Id
	Content     string `json:"content"`      // 聊天内容 [emoji:表情Id]
	SeverID     int32  `json:"sever_id"`     // 服务器Id
	ServerName  string `json:"server_name"`  // 服务器名称
}

type ChatLogContent struct {
	Key  string `json:"key"`
	Size int64  `json:"size"`
}

type UpdateNicknameReq struct {
	UserId   int64  `json:"userId" form:"userId"`
	Nickname string `json:"nickname" form:"nickname"`
}

func (req *UpdateNicknameReq) Check() error {
	if req.UserId <= 0 {
		return errors.New("玩家ID不能为空")
	}

	if req.Nickname == "" {
		return errors.New("玩家昵称不能为空")
	}
	return nil
}

type CompatibilityTestVerifyReq struct {
	ProduceUserId int64 `json:"produceUserId" form:"produceUserId"`
	Service       int64 `json:"service" form:"service"`
}

func (req *CompatibilityTestVerifyReq) Check() error {
	if req.Service <= 0 {
		return errors.New("请先选择服务器")
	}

	if req.ProduceUserId <= 0 {
		return errors.New("正式服玩家ID不能为空")
	}

	return nil
}

type CompatibilityTestMigrateReq struct {
	ProduceUserId int64 `json:"produceUserId" form:"produceUserId"`
	UserId        int64 `json:"userId" form:"userId"`
	Service       int64 `json:"service" form:"service"`
}

func (req *CompatibilityTestMigrateReq) Check() error {
	if req.Service <= 0 {
		return errors.New("请先选择服务器")
	}
	if req.ProduceUserId <= 0 {
		return errors.New("正式服玩家ID不能为空")
	}
	if req.UserId <= 0 {
		return errors.New("玩家ID不能为空")
	}
	return nil
}
