package dto

import (
	statuscode "go-admin/common/status_code"
	"go-admin/pkg/emailhelper"
	"go-admin/pkg/utility"
	"time"

	"github.com/shopspring/decimal"

	"go-admin/app/admin/models"
	"go-admin/common/dto"
	common "go-admin/common/models"
)

type LineUserGetPageReq struct {
	dto.Pagination `search:"-"`
	LineUserOrder
}

type LineUserOrder struct {
	Id             string `form:"idOrder"  search:"type:order;column:id;table:line_user"`
	GroupId        int    `form:"groupIdOrder"  search:"type:order;column:group_id;table:line_user"`
	Username       string `form:"usernameOrder"  search:"type:order;column:username;table:line_user"`
	Nickname       string `form:"nicknameOrder"  search:"type:order;column:nickname;table:line_user"`
	Password       string `form:"passwordOrder"  search:"type:order;column:password;table:line_user"`
	Salt           string `form:"saltOrder"  search:"type:order;column:salt;table:line_user"`
	Email          string `form:"emailOrder"  search:"type:order;column:email;table:line_user"`
	Mobile         string `form:"mobileOrder"  search:"type:order;column:mobile;table:line_user"`
	Avatar         string `form:"avatarOrder"  search:"type:order;column:avatar;table:line_user"`
	Level          int    `form:"levelOrder"  search:"type:order;column:level;table:line_user"`
	Gender         int    `form:"genderOrder"  search:"type:order;column:gender;table:line_user"`
	Bio            string `form:"bioOrder"  search:"type:order;column:bio;table:line_user"`
	Money          string `form:"moneyOrder"  search:"type:order;column:money;table:line_user"`
	Score          int    `form:"scoreOrder"  search:"type:order;column:score;table:line_user"`
	Successions    int    `form:"successionsOrder"  search:"type:order;column:successions;table:line_user"`
	MaxSuccessions int    `form:"maxSuccessionsOrder"  search:"type:order;column:max_successions;table:line_user"`
	Loginip        string `form:"loginipOrder"  search:"type:order;column:loginip;table:line_user"`
	Loginfailure   int    `form:"loginfailureOrder"  search:"type:order;column:loginfailure;table:line_user"`
	Joinip         string `form:"joinipOrder"  search:"type:order;column:joinip;table:line_user"`
	Jointime       int    `form:"jointimeOrder"  search:"type:order;column:jointime;table:line_user"`
	Token          string `form:"tokenOrder"  search:"type:order;column:token;table:line_user"`
	Status         string `form:"statusOrder"  search:"type:order;column:status;table:line_user"`
	Verification   string `form:"verificationOrder"  search:"type:order;column:verification;table:line_user"`
	LoginTime      string `form:"loginTimeOrder"  search:"type:order;column:login_time;table:line_user"`
	CreatedAt      string `form:"createdAtOrder"  search:"type:order;column:created_at;table:line_user"`
	UpdatedAt      string `form:"updatedAtOrder"  search:"type:order;column:updated_at;table:line_user"`
}

func (m *LineUserGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type LineUserInsertReq struct {
	Id             int             `json:"-" comment:"ID"` // ID
	GroupId        int             `json:"groupId" comment:"组别ID"`
	Username       string          `json:"username" comment:"用户名"`
	Nickname       string          `json:"nickname" comment:"昵称"`
	Password       string          `json:"password" comment:"密码"`
	Salt           string          `json:"salt" comment:"密码盐"`
	Email          string          `json:"email" comment:"电子邮箱"`
	Mobile         string          `json:"mobile" comment:"手机号"`
	Area           string          `json:"area" comment:"手机号归属地"`
	Avatar         string          `json:"avatar" comment:"头像"`
	Level          int             `json:"level" comment:"等级"`
	Gender         int             `json:"gender" comment:"性别"`
	Bio            string          `json:"bio" comment:"格言"`
	Money          decimal.Decimal `json:"money" comment:"保证金"`
	Score          int             `json:"score" comment:"积分"`
	Successions    int             `json:"successions" comment:"连续登录天数"`
	MaxSuccessions int             `json:"maxSuccessions" comment:"最大连续登录天数"`
	Loginip        string          `json:"loginip" comment:"登录IP"`
	Loginfailure   int             `json:"loginfailure" comment:"失败次数"`
	Joinip         string          `json:"joinip" comment:"加入IP"`
	Jointime       int             `json:"jointime" comment:"加入时间"`
	RecommendNum   int             `json:"recommend_num" comment:"推荐人数"`
	Token          string          `json:"token" comment:"Token"`
	Status         string          `json:"status" comment:"状态"`
	Verification   string          `json:"verification" comment:"验证"`
	LoginTime      time.Time       `json:"loginTime" comment:"登录时间"`
	common.ControlBy
}

func (s *LineUserInsertReq) Generate(model *models.LineUser) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.GroupId = s.GroupId
	model.Username = s.Username
	model.Nickname = s.Nickname
	model.Password = s.Password
	model.Salt = s.Salt
	model.Email = s.Email
	model.Mobile = s.Mobile
	model.Area = s.Area
	model.Avatar = s.Avatar
	model.Level = s.Level
	model.Gender = s.Gender
	model.Bio = s.Bio
	model.Money = s.Money
	model.Score = s.Score
	model.Successions = s.Successions
	model.MaxSuccessions = s.MaxSuccessions
	model.Loginip = s.Loginip
	model.Loginfailure = s.Loginfailure
	model.Joinip = s.Joinip
	model.Jointime = s.Jointime
	model.RecommendNum = s.RecommendNum
	model.Token = s.Token
	model.Status = s.Status
	model.Verification = s.Verification
	model.LoginTime = s.LoginTime
}

func (s *LineUserInsertReq) GetId() interface{} {
	return s.Id
}

type LineUserUpdateReq struct {
	Id             int             `uri:"id" comment:"ID"` // ID
	GroupId        int             `json:"groupId" comment:"组别ID"`
	Pid            int             `json:"pid" comment:"推荐人ID"`
	Username       string          `json:"username" comment:"用户名"`
	Nickname       string          `json:"nickname" comment:"昵称"`
	Password       string          `json:"password" comment:"密码"`
	Salt           string          `json:"salt" comment:"密码盐"`
	Email          string          `json:"email" comment:"电子邮箱"`
	Mobile         string          `json:"mobile" comment:"手机号"`
	Area           string          `json:"area" comment:"手机号归属地"`
	Avatar         string          `json:"avatar" comment:"头像"`
	Level          int             `json:"level" comment:"等级"`
	Gender         int             `json:"gender" comment:"性别"`
	Bio            string          `json:"bio" comment:"格言"`
	Money          decimal.Decimal `json:"money" comment:"余额"`
	Score          int             `json:"score" comment:"积分"`
	InviteCode     string          `json:"invite_code" comment:"邀请码"`
	Successions    int             `json:"successions" comment:"连续登录天数"`
	MaxSuccessions int             `json:"maxSuccessions" comment:"最大连续登录天数"`
	Loginip        string          `json:"loginip" comment:"登录IP"`
	Loginfailure   int             `json:"loginfailure" comment:"失败次数"`
	Joinip         string          `json:"joinip" comment:"加入IP"`
	Jointime       int             `json:"jointime" comment:"加入时间"`
	RecommendNum   int             `json:"recommend_num" comment:"推荐人数"`
	Token          string          `json:"token" comment:"Token"`
	Status         string          `json:"status" comment:"状态"`
	Verification   string          `json:"verification" comment:"验证"`
	LoginTime      time.Time       `json:"loginTime" comment:"登录时间"`
	OpenStatus     int             `json:"open_status"`
	common.ControlBy
}

func (s *LineUserUpdateReq) Generate(model *models.LineUser) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.GroupId = s.GroupId
	model.Pid = s.Pid
	model.Username = s.Username
	model.Nickname = s.Nickname
	model.Password = s.Password
	model.Salt = s.Salt
	model.Email = s.Email
	model.Mobile = s.Mobile
	model.Area = s.Area
	model.Avatar = s.Avatar
	model.Level = s.Level
	model.Gender = s.Gender
	model.Bio = s.Bio
	model.Money = s.Money
	model.Score = s.Score
	model.InviteCode = s.InviteCode
	model.Successions = s.Successions
	model.MaxSuccessions = s.MaxSuccessions
	model.Loginip = s.Loginip
	model.Loginfailure = s.Loginfailure
	model.Joinip = s.Joinip
	model.Jointime = s.Jointime
	model.Token = s.Token
	model.Status = s.Status
	model.Verification = s.Verification
	model.LoginTime = s.LoginTime
}

func (s *LineUserUpdateReq) GetId() interface{} {
	return s.Id
}

// LineUserGetReq 功能获取请求参数
type LineUserGetReq struct {
	Id int `uri:"id"`
}

func (s *LineUserGetReq) GetId() interface{} {
	return s.Id
}

// LineUserDeleteReq 功能删除请求参数
type LineUserDeleteReq struct {
	Ids []int `json:"ids"`
}

func (s *LineUserDeleteReq) GetId() interface{} {
	return s.Ids
}

// FrontedUserVerifyEmailReq 验证邮箱验证码参数
type FrontedUserVerifyEmailReq struct {
	Email      string `json:"email"`
	VerifyCode string `json:"verify_code"`
}

type FrontedSendVerifyEmailReq struct {
	Email string `json:"email"`
	Type  int    `json:"type"`
}

type FrontedSendVerifySmsReq struct {
	PhoneAreaCode string `json:"phone_area_code"` // 区域电话代码
	Phone         string `json:"phone"`           // 手机号码
	Type          int    `json:"type" comment:"类型 0-注册 1-忘记密码"`
}

func (receiver *FrontedSendVerifyEmailReq) CheckParams() int {
	// 邮箱校验
	if receiver.Email == "" {
		return statuscode.EmailRequired
	}
	if !emailhelper.CheckIsEmail(receiver.Email) {
		return statuscode.EmailFormatInvalid
	}
	return statuscode.OK
}

func (receiver FrontedSendVerifySmsReq) CheckParams() int {
	// 短信校验
	if receiver.PhoneAreaCode == "" || receiver.Phone == "" {
		return statuscode.ParameterInvalid
	}
	if receiver.PhoneAreaCode == "+86" {
		if !utility.IsMobileChina(receiver.Phone) {
			return statuscode.PhoneFormatInvalid
		}
	}
	return statuscode.OK
}

// FrontedLoginReq 登录请求
type FrontedLoginReq struct {
	Email         string `json:"email"`
	Phone         string `json:"phone"`
	PhoneAreaCode string `json:"phone_area_code"`
	Password      string `json:"password"`   // 密码
	LoginType     int    `json:"login_type"` // 1 手机，2 邮箱
}

func (receiver FrontedLoginReq) CheckParams() int {
	if receiver.Password == "" {
		return statuscode.PasswordRequired
	}
	if receiver.LoginType == 2 {

		if receiver.Email == "" {
			return statuscode.EmailRequired
		}
		if !emailhelper.CheckIsEmail(receiver.Email) {
			return statuscode.EmailFormatInvalid
		}
		return statuscode.OK
	}

	if receiver.LoginType == 1 { //手机号登录
		if receiver.Phone == "" {
			return statuscode.PhoneRequired
		}
		return statuscode.OK
	}
	return statuscode.ParamErr
}

type AddApiKeyReq struct {
	ExchangeType string `json:"exchange_type"`
	ApiName      string `json:"api_name"`
	ApiKey       string `json:"api_key"`
	ApiSecret    string `json:"api_secret"`
	ApiIp        string `json:"api_ip"`
}

func (a AddApiKeyReq) CheckParams() int {
	if a.ExchangeType == "" || a.ApiKey == "" || a.ApiSecret == "" {
		return statuscode.ParamErr
	}
	return statuscode.OK
}

// OpenStatusReq 开启或者闭关状态
type OpenStatusReq struct {
	Status int `json:"status"`
}

type RechargeListReq struct {
	Coin string `json:"coin"`
}

// ApiRestrictions 检查api 是否可用
type ApiRestrictions struct {
	IpRestrict                   bool  `json:"ipRestrict"`
	CreateTime                   int64 `json:"createTime"`
	EnableReading                bool  `json:"enableReading"`
	EnableWithdrawals            bool  `json:"enableWithdrawals"`
	EnableInternalTransfer       bool  `json:"enableInternalTransfer"`
	EnableMargin                 bool  `json:"enableMargin"`
	EnableFutures                bool  `json:"enableFutures"`
	PermitsUniversalTransfer     bool  `json:"permitsUniversalTransfer"`
	EnableVanillaOptions         bool  `json:"enableVanillaOptions"`
	EnableFixApiTrade            bool  `json:"enableFixApiTrade"`
	EnableFixReadOnly            bool  `json:"enableFixReadOnly"`
	EnableSpotAndMarginTrading   bool  `json:"enableSpotAndMarginTrading"`
	EnablePortfolioMarginTrading bool  `json:"enablePortfolioMarginTrading"`
}

type RechargeAddressListReq struct {
	UserId    int    `json:"user_id"`
	NetWorkId int    `json:"network_id"`
	CoinCode  string `json:"coin"`
}

type VtsRechargePreOrderReq struct {
	CoinCode string          `json:"coin_code" form:"coin_code"`
	Amount   decimal.Decimal `json:"amount" form:"amount"`
}

type VtsRechargePreOrderResp struct {
	PaymentUrl    string `json:"paymentUrl"`
	PriceAmount   string `json:"priceAmount"`
	PriceCurrency string `json:"priceCurrency"`
}

type PersonnalInviteLogPageReq struct {
	dto.Pagination `search:"-"`
	UserId         int `json:"userId" form:"userId"`
}

type InviteLogResp struct {
	Id           int             `json:"id"`
	UserId       int             `json:"userId"`
	RegisterTime int64           `json:"registerTime"`
	NickName     string          `json:"nickName"`
	Childs       []InviteLogResp `json:"childs"`
}

type LineUserPropertyResp struct {
	SpotTotalAmount    decimal.Decimal `json:"spotTotalAmount"`    //现货U总资产
	SpotFreeAmount     decimal.Decimal `json:"spotFreeAmount"`     //现货U可用余额
	FuturesTotalAmount decimal.Decimal `json:"futuresTotalAmount"` //合约U总资产
	FuturesFreeAmount  decimal.Decimal `json:"futuresFreeAmount"`  //合约U可用余额
}

type LineUserAppResp struct {
	ExpirationTimeUnix int64           `json:"expirationTimeUnix"`
	MinOrderAmount     decimal.Decimal `json:"minOrderAmount"`
}

type LineUserOrderSetReq struct {
	MinOrderAmount decimal.Decimal `json:"minOrderAmount"`
}

type LineUserResetPwdReq struct {
	Pwd           string `json:"pwd"`
	CheckPwd      string `json:"check_pwd" comment:"确认密码"`
	PhoneAreaCode string `json:"phone_area_code" comment:"区号"`
	Code          string `json:"code"`
	Email         string `json:"email"`
	Phone         string `json:"phone"`
	Type          int    `json:"type" comment:"类型 1-手机验证码 2-邮箱验证码"`
}

// 参数校验
func (e *LineUserResetPwdReq) Valid() int {
	if e.Pwd == "" {
		return statuscode.ParameterInvalid
	}

	if e.Code == "" {
		return statuscode.ParameterInvalid
	}

	if e.CheckPwd != e.Pwd {
		return statuscode.UserResetPasswordInconsistency
	}

	switch e.Type {
	case 1:
		if e.Phone == "" {
			return statuscode.ParameterInvalid
		}

		if e.PhoneAreaCode == "" {
			return statuscode.ParameterInvalid
		}
	case 2:
		if e.Email == "" {
			return statuscode.ParameterInvalid
		}
	default:
		return statuscode.ParameterInvalid
	}

	return statuscode.OK
}

type LineUserReloadPropertyReq struct {
	UserId int `json:"userId"`
}
