// Package forms 原gm
package forms

import (
	"errors"
	"gadmin/internal/gorm/model"
	"math/rand"
	"time"
)

type Resp struct {
	Code int32       `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type CommonReq struct {
	UserId int64 `form:"id"`
}

type UserQuery struct {
	Query string `form:"query"`
}

type UserInfo struct {
	UserId      int64 `json:"user_id"`
	Exp         int64 `json:"exp"`
	TalentPoint int64 `json:"talent_point"`
	Level       int64 `json:"level"`
	Stamina     int64 `json:"stamina"`
	Coin        int64 `json:"coin"`
	Diamond     int64 `json:"diamond"`
}

// UpdateRoleReq 更新经验、体力
type UpdateRoleReq struct {
	UserId      int64 `json:"user_id"`
	Exp         int64 `json:"exp"`
	Stamina     int64 `json:"stamina"`
	Energy      int64 `json:"energy"`
	TalentPoint int64 `json:"talent_point"`
}

type UpgradeRoleReq struct {
	UserId    int64 `json:"user_id"`
	RoleId    int64 `json:"role_id"`
	NewRoleId int64 `json:"new_role_id"`
}

type GetRoleLevelReq struct {
	RoleName  string `json:"role_name" `
	UserId    int64  `json:"user_id"`
	RoleId    int64  `json:"role_id"`
	NewRoleId int64  `json:"new_role_id"`
}

type AddCoinReq struct {
	UserId  int64 `json:"user_id"`
	Coin    int64 `json:"coin"`
	Diamond int64 `json:"diamond"`
	GoodId  int   `json:"good_id"` // 1 coin 2 diamond
}

type AddMaterialReq struct {
	UserId     int64   `json:"user_id"`
	Id         int64   `json:"id"` // 装备，或道具
	Count      int64   `json:"count"`
	SuitName   string  `json:"name"`
	Equipments []int64 `json:"equipments"`
}

type DeleteUserAccountReq struct {
	AccId int64 `json:"accId"`
}

type DeleteUserPlayerReq struct {
	UserId int64 `json:"playerid"`
}

type OpenAccountReq struct {
	UserId int64 `json:"userId"`
}

type DropedRole struct {
	ID int `json:"id"`
	//HP   int  `json:"hp"`
	Wear bool `json:"wear"`
}

type UserItem struct {
	UID            int64        `json:"userId"`
	Coin           int          `json:"coin"`
	Diamond        int          `json:"diamond"`
	Level          int          `json:"userLevel"`
	Exp            int64        `json:"userExp"`
	TimeStamp      int64        `json:"timeStamp"`
	Stamina        int          `json:"stamina"`
	LastLogoutTick int64        `json:"lastLogoutTick"`
	FirstWeapon    bool         `json:"firstGame"`
	Guide          int          `json:"guide"`
	Roles          []DropedRole `json:"roles"`
	//Buff           *model.DivinationBuff   `json:"buff"`
}

var i int64 = 0

func NewUserItem() *UserItem {
	i += 1
	return &UserItem{
		UID:            i,
		Coin:           100,
		Diamond:        rand.Int() % 100000000,
		Level:          8,
		Exp:            1000,
		TimeStamp:      time.Now().Unix() - rand.Int63()%10000,
		Stamina:        30,
		LastLogoutTick: 0,
		FirstWeapon:    false,
		Guide:          2,
		Roles:          []DropedRole{{10001001, false}, {30001001, false}},
	}
}

// send notice
type AddNoticeReq struct {
	Type     string `json:"type"`
	Content  string `json:"content"`
	StartAt  int64  `json:"start_at"`
	EndAt    int64  `json:"end_at"`
	Interval int64  `json:"interval"`
	MsgId    string `json:"key"`
}

type NoticeListReq struct {
}

type DeleteNoticeReq struct {
	Id string `form:"id" json:"id"`
}

type NoticeItem struct {
	MsgId    string `json:"key"`
	Type     string `json:"type"`
	Content  string `json:"content"`
	CreateAt int64  `json:"create_at"`
	UpdateAt int64  `json:"update_at"`
	StartAt  int64  `json:"start_at"`
	EndAt    int64  `json:"end_at"`
	Interval int64  `json:"interval"`
}

// session
type LoginReq struct {
	UserName string `json:"username"`
	Password string `json:"password"`
}

type SetConfPathReq struct {
	Path string `json:"path"`
}

type UpdatePlayerElRankReq struct {
	PlayerId     int `json:"playId"`
	RoomCount    int `json:"roomCount"`
	MaxRoomCount int `json:"maxRoomCount"`
	ServerId     int `json:"server_id"`
}

type UpdateSwitcherReq struct {
	ServerId    int `json:"serverId"`
	IosPay      int `json:"iosPay"`
	PlayerLevel int `json:"playerLevel"` // 支付开放等级
	// DoubleAd    int `json:"doubleAd"`    // 0关闭 5打开

	AndroidPay       int `json:"andrPay"`       //0:禁止支付 1:米大师支付 2：客服支付 3：混合支付
	AndroidOpenLevel int `json:"andrOpenLevel"` // 客服支付达到多少级开放

	DoubleAd    int `json:"doubleAd"`    // 双倍广告次数 默认呢5次
	AdOpenLevel int `json:"adOpenLevel"` // 广告开放等级 默认5级
}

type EmailListReq struct {
	Page    int `json:"page" form:"page"`
	PerPage int `json:"pageSize" form:"pageSize"`
}

type EmailItemReq struct {
	Id      int64  `json:"id"`
	Title   string `json:"title"`   // 标题
	Content string `json:"content"` // 内容
	Ext     []struct {
		Id    string `json:"id"`
		Count string `json:"count"`
	} `json:"ext"` // 奖励材料
	Sender       string `json:"sender"`
	Recievers    string `json:"recievers"`
	SendWay      int    `json:"sendWay"` // 1 立即发送 0 待发送
	SendAt       string `json:"sendAt"`  // 准备发送的时间
	Expired      int    `json:"expired"` // 过期天数
	Opt          string `json:"opt"`     // draft|send|delete
	OperatorId   int64  `json:"operatorId"`
	RecieverType int64  `json:"recieverType"`
}

type EmailItem struct {
	Id      int64  `json:"id"`
	Title   string `json:"title"`   // 标题
	Content string `json:"content"` // 内容
	Type    int    `json:"type"`
	Ext     []struct {
		Id    string `json:"id"`
		Count string `json:"count"`
	} `json:"ext"` // 奖励材料
	Sender       string `json:"sender"`
	Recievers    string `json:"recievers"`
	SendWay      int    `json:"sendWay"` // 1 立即发送 0 待发送
	SendAt       int64  `json:"sendAt"`  // 准备发送的时间
	SentAt       int64  `json:"sentAt"`  // 发送时间
	Expired      int    `json:"expired"` // 过期天数
	Opt          string `json:"opt"`     // draft|send|delete
	Status       string `json:"status"`  // 已发送|待发送
	Deleted      bool   `json:"deleted"`
	OperatorId   int64  `json:"operatorId"`
	RecieverType int64  `json:"recieverType"`
}

type Interface interface {
	// Len is the number of elements in the collection.
	Len() int
	// Less reports whether the element with
	// index i should sort before the element with index j.
	Less(i, j int) bool
	// Swap swaps the elements with indexes i and j.
	Swap(i, j int)
}

type EmailList []*EmailItem

func (s EmailList) Len() int      { return len(s) }
func (s EmailList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s EmailList) Less(i, j int) bool {
	if s[i].Status == "待发送" {
		return true
	} else if s[j].Status == "待发送" {
		return false
	} else {
		return s[i].Id > s[j].Id
	}
}

func (item *EmailItem) ID() int64 {
	return item.Id
}

func (item *EmailItem) SetID(val int64) {
	item.Id = val
}

type EmailListResp struct {
	Code       int       `json:"code"`
	Data       EmailList `json:"data"`
	Pagination Paginate  `json:"pagination"`
}

type Paginate struct {
	Page    int `json:"page"`
	PerPage int `json:"pageSize"`
	Total   int `json:"total"`
}

type QueryPayOrderReq struct {
	PlayerID int    `form:"playerId"`
	Type     int    `form:"type"` // 1 安卓，2 IOS
	Begin    string `form:"begin"`
	End      string `form:"end"`
	Page     int    `form:"page"`
	PerPage  int    `form:"pageSize"`
}

type PayOrder struct {
	ID         int    `json:"id"`
	PlayerID   int    `json:"playerID"`
	Amount     int    `json:"amount"`
	CreateTime string `json:"createAt"`
	Status     int    `json:"status"`
	Platform   string `json:"platform"`
}

type PayOrderResp struct {
	Code       int        `json:"code"`
	Data       []PayOrder `json:"data"`
	Pagination Paginate   `json:"pagination"`
}

type GuidesItem struct {
	Name string `json:"name"`
	Val  int    `json:"index"`
	Pass bool   `json:"pass"`
}

type ToolsRestartReq struct {
	Version string `json:"version"`
}

// ToolsRestartLogReq 重启记录
type ToolsRestartLogReq struct {
	Page  int `json:"page"`
	Limit int `json:"pageSize"`
}

type ToolsRestartLogRes struct {
	List       []*model.ToolsRestartLog `json:"list"`
	Page       int                      `json:"page"`
	Limit      int                      `json:"limit"`
	TotalCount int64                    `json:"total_count"`
}

// ToolsRestartViewReq 重启行数据
type ToolsRestartViewReq struct {
	ID int64 `form:"id" json:"id"`
}

type LevelProgressReq struct {
	PlayerId int64 `form:"player_id" json:"player_id"`
}

type GetPVPRankingListReq struct {
	ServerId int `json:"server_id" form:"server_id"`
}

type GetCurioRankingListReq struct {
	ServerId int `json:"server_id" form:"server_id"`
}

type GetIdiomRankingListReq struct {
	ServerId int `json:"server_id" form:"server_id"`
}

type GetBossRankingListReq struct {
	ServerId int `json:"server_id" form:"server_id"`
}

type ClearPVPRankReq struct {
	UserID   int64 `json:"user_id"`
	ServerId int   `json:"server_id" form:"server_id"`
}

type ClearCurioRankReq struct {
	UserID   int64 `json:"user_id"`
	ServerId int   `json:"server_id" form:"server_id"`
}

type ClearIdiomRankReq struct {
	UserID   int64 `json:"user_id"`
	ServerId int   `json:"server_id" form:"server_id"`
}

type ClearBossRankReq struct {
	UserID   int64 `json:"user_id"`
	ServerId int   `json:"server_id" form:"server_id"`
}

type AlarmGameDataListReq struct {
	ListReq
	ServerID  int     `json:"server_id" form:"server_id"`
	UserID    int64   `json:"player_id" form:"player_id"`
	CreatedAt []int64 `json:"created_at" form:"created_at[]"`
}

func (req *AlarmGameDataListReq) Check() error {
	if req.ServerID == 0 {
		return errors.New("服务器ID不能为空")
	}
	if len(req.CreatedAt) == 2 {
		req.CreatedAt[0] = req.CreatedAt[0] / 1000
		req.CreatedAt[1] = req.CreatedAt[1] / 1000
	}
	return nil
}

type AlarmGameDataInfo struct {
	ID        int64     `json:"id"`
	PlayerID  int64     `json:"playerId"`
	Attrs     string    `json:"attrs"`
	ServerID  int32     `json:"serverId"`
	Msg       string    `json:"msg"`
	Time      int64     `json:"time"`
	Module    string    `json:"module"`
	CreatedAt time.Time `json:"createdAt"`
	Ban       int       `json:"ban"`
}
type AlarmGameDataListResp struct {
	ListRes
}

type AlarmAttrReq struct {
	ListReq
	ChapterID int     `json:"chapter_id" form:"chapter_id"`
	RoomID    int     `json:"room_id" form:"room_id"`
	Level     int     `json:"level" form:"level"`
	UserID    int64   `json:"player_id" form:"player_id"`
	Time      []int64 `json:"time" form:"time[]"`
}

func (req *AlarmAttrReq) Check() error {
	if req.UserID <= 0 {
		return errors.New("玩家ID不能为空")
	}
	if len(req.Time) == 2 {
		req.Time[0] = req.Time[0] / 1000
		req.Time[1] = req.Time[1] / 1000
	}
	return nil
}

type AlarmAttrResp struct {
	ListRes
}

type AlarmCheatingReq struct {
	ListReq
	PlayerId   int64   `json:"player_id" form:"player_id"`
	ServerId   int64   `json:"server_id" form:"server_id"`
	OriginTime []int64 `json:"origin_time" form:"origin_time"`
	YearMonth  int64   `json:"year_month" form:"year_month"`
	Ratio      float64 `json:"ratio" form:"ratio"`
}

func (req *AlarmCheatingReq) Check() error {
	if len(req.OriginTime) == 2 {
		//数据库中是纳秒 参数是毫秒
		req.OriginTime[0] = req.OriginTime[0] * 1000 * 1000
		req.OriginTime[1] = req.OriginTime[1] * 1000 * 1000
	}
	return nil
}

type AlarmCheatingInfo struct {
	ID         int64   `json:"id"`
	OriginId   int64   `json:"origin_id"`
	OriginTime string  `json:"origin_time"`
	YearMonth  string  `json:"year_month"`
	PlayerId   int64   `json:"player_id"`
	ServerId   int     `json:"server_id"`
	Dps        float64 `json:"dps"`
	Rate       float64 `json:"rate"`
	Valuation  float64 `json:"valuation"`
	Damage     float64 `json:"damage"`
	Ratio      float64 `json:"ratio"`
	RoleInfo   string  `json:"role_info"`
	AlarmData  string  `json:"alarm_data"`
	Ban        int     `json:"ban"`
}

type AlarmCheatingResp struct {
	ListRes
}
type AddGiftReq struct {
	PlayerId int64 `json:"player_id"`
	GiftId   int64 `json:"gift_id"` // 装备，或道具
}

type GmSendGift struct {
	MsgId    string `json:"msgId"`
	PlayerID int64  `json:"userId"`
	Gift     int64  `json:"gift"`
}

type ResponseGmSendGift struct {
	ErrCode int    `json:"errCode"`
	MsgId   string `json:"msgId"`
}

// GmChatForbid 用户禁言的nats消息结构
type GmChatForbid struct {
	MsgId      string `json:"msgId"`
	PlayerID   int64  `json:"userId"`
	ForbidTime int64  `json:"forbidTime"`
}

type ResponseGmChatForbid struct {
	ErrCode int    `json:"errCode"`
	MsgId   string `json:"msgId"`
}
