package forms

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

type FightingListReq struct {
	ListReq
	Id         int64   `json:"id" form:"id"`
	UserId     int64   `json:"user_id" form:"playerId"`
	ChapterId  int32   `json:"chapter_id" form:"chapter_id"`
	Difficulty int32   `json:"difficulty" form:"difficulty"`
	EventAt    []int64 `json:"event_at" form:"event_at[]"`
}

func (req *FightingListReq) Check() error {

	if req.EventAt != nil && len(req.EventAt) != 2 {
		return errors.New("时间必须选择一个区间或者留空")
	}

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

		// 默认是毫秒
		req.EventAt[0] = req.EventAt[0] / 1000
		req.EventAt[1] = req.EventAt[1] / 1000
	}

	return nil
}

type FightingExportReq struct {
	Id         int64   `json:"id" form:"id"`
	UserId     int64   `json:"user_id" form:"playerId"`
	ChapterId  int32   `json:"chapter_id" form:"chapter_id"`
	Difficulty int32   `json:"difficulty" form:"difficulty"`
	EventAt    string  `json:"event_at" form:"event_at"`
	EventStamp []int64 `json:"-"`
}

func (req *FightingExportReq) Check() error {

	eventAt := strings.Split(req.EventAt, ",")
	if len(eventAt) != 2 {
		return errors.New("必须选择一个时间区间且不能大于一天")
	}

	if cast.ToInt64(eventAt[0]) > cast.ToInt64(eventAt[1]) {
		return errors.New("时间选择的区间值不合理")
	}

	// 默认是毫秒
	req.EventStamp = append(req.EventStamp, cast.ToInt64(eventAt[0])/1000, cast.ToInt64(eventAt[1])/1000)
	if (req.EventStamp[1] - req.EventStamp[0]) > 24*3600 {
		return errors.New("不能导出大于一天的数据")
	}
	
	return nil
}
