package forms

import (
	"errors"
	"fmt"
	"gadmin/package/gmdata"
	"gadmin/utility/character"
	"time"
)

type CdkAward struct {
	Materials map[int64]int64 `json:"material"`
	Equipment []int64         `json:"equipment"`
}
type CdkAddReq struct {
	Name      string   `json:"name" form:"name"`
	Cdk       string   `json:"cdk" form:"cdk"`
	Num       int      `json:"num"  form:"num"`
	TypeId    int64    `json:"type_id" form:"type_id"`
	Openid    string   `json:"openid"  form:"type_id"`
	Times     int64    `json:"times" form:"times"`
	ExpiredAt int64    `json:"expired_at" form:"expired_at"`
	Operator  string   `json:"operator" form:"operator"`
	CdkAward  CdkAward `json:"materials" form:"materials"`
	ServerIds []int32  `json:"server_ids" form:"server_ids"`
}

func (req *CdkAddReq) Check() error {

	if req.Name == "" {
		return errors.New("生成名称不能为空")
	}
	if req.Name == "" {
		return errors.New("生成名称不能为空")
	}
	if req.Num < 1 {
		return errors.New("生成数量不能为空")
	}

	if req.TypeId != 1 && req.TypeId != 2 {
		return errors.New("生成类型不正确")
	}

	if req.TypeId == 1 { // 全部玩家时不能填指定玩家ID
		req.Openid = ""
	}

	if req.Times < 1 {
		return errors.New("可用次数不能为空")
	}

	if req.ExpiredAt <= 0 {
		return errors.New("请选择过期时间")
	}

	if req.Operator == "" {
		return errors.New("请填写生成人")
	}

	if req.Openid != "" && req.Num != 1 {
		return errors.New("指定用户Openid时，生成数量只能是1")
	}

	//if req.Openid != "" && req.Times != 1 {
	//	return errors.New("指定用户Openid时，可用次数只能是1")
	//}

	if req.Cdk != "" && req.Num != 1 {
		return errors.New("自定义兑换码时，生成数量只能是1")
	}

	if req.Cdk != "" && !character.IsLetterNumber(req.Cdk) {
		return errors.New("兑换码只支持26位大小写字母和阿拉伯数字")
	}

	req.ExpiredAt = req.ExpiredAt / 1000

	if len(req.CdkAward.Materials) == 0 && len(req.CdkAward.Equipment) == 0 {
		return errors.New("装备和材料至少需要添加一个")
	}

	if len(req.CdkAward.Materials) > 0 {
		for id, v := range req.CdkAward.Materials {
			if id == 0 {
				return errors.New("请检查添加的材料，请删掉多余的添加输入框")
			}
			if v < 1 {
				return errors.New(fmt.Sprintf("请检查添加的材料，数量不能小于1，材料ID：%v", id))
			}
			if !gmdata.IsMaterial(int(id)) && id != 2001 && id != 2002 {
				return errors.New(fmt.Sprintf("请检查添加的材料，材料ID未在表中找到，材料ID：%v", id))
			}
		}
	}

	if len(req.CdkAward.Equipment) > 0 {
		for _, v := range req.CdkAward.Equipment {
			if v == 0 {
				return errors.New("请检查添加的装备，请确认是正确的装备ID")
			}
			if !gmdata.IsEquipment(v) {
				return errors.New(fmt.Sprintf("请检查添加的装备，装备ID未在表中找到，装备ID：%v", v))
			}

			if v != gmdata.GetEquipmentStartID(v) {
				return errors.New(fmt.Sprintf("请检查添加的装备，目前只能添加1级装备，装备ID：%v", v))
			}
		}
	}

	if len(req.ServerIds) == 0 {
		return errors.New("请选择有效的服务器")
	}

	if len(req.ServerIds) > 1 {
		for _, serverId := range req.ServerIds {
			if serverId == 0 {
				return errors.New("选择全服时不能再选择其他服务器")
			}
		}
	}
	return nil
}

type CdkDelReq struct {
	ID int64 `json:"id"  form:"id"`
}

type CdkListReq struct {
	ListReq
	Sn        string  `json:"sn" form:"sn"`
	Cdk       string  `json:"cdk" form:"cdk"`
	CreatedAt []int64 `json:"created_at" form:"created_at[]"`
	Time      []time.Time
	Name      string `json:"name" form:"name"`
}

func (req *CdkListReq) Check() error {
	if req.CreatedAt != nil && len(req.CreatedAt) != 2 {
		return errors.New("生成时间必须选择一个区间或者留空")
	}

	if len(req.CreatedAt) == 2 {
		if req.CreatedAt[0] > req.CreatedAt[1] {
			return errors.New("生成时间选择的区间值不合理")
		}
		// 默认是毫秒
		req.CreatedAt[0] = req.CreatedAt[0] / 1000
		req.CreatedAt[1] = req.CreatedAt[1] / 1000
	}

	return nil
}

type CdkReceivedReq struct {
	ListReq
	Cid int64 `json:"cid" form:"cid"`
}

func (req *CdkReceivedReq) Check() error {

	return nil
}

type CdkInvalidReq struct {
	Sn     string `json:"sn" form:"sn"`
	Status int32  `json:"status" form:"status"`
}

type CdkRedeemCodeListReq struct {
	ListReq
	Sn        string  `json:"sn" form:"sn"`
	Cdk       string  `json:"cdk" form:"cdk"`
	CreatedAt []int64 `json:"created_at" form:"created_at[]"`
	Time      []time.Time
}

func (req *CdkRedeemCodeListReq) Check() error {
	if req.CreatedAt != nil && len(req.CreatedAt) != 2 {
		return errors.New("生成时间必须选择一个区间或者留空")
	}

	if len(req.CreatedAt) == 2 {
		if req.CreatedAt[0] > req.CreatedAt[1] {
			return errors.New("生成时间选择的区间值不合理")
		}
		// 默认是毫秒
		req.CreatedAt[0] = req.CreatedAt[0] / 1000
		req.CreatedAt[1] = req.CreatedAt[1] / 1000
	}

	return nil
}
