package order

import (
	"fmt"
	"fusenapi/constants"
	"fusenapi/model/gmodel"
	"fusenapi/utils/format"
	"time"
)

type AmountCurrencyReq struct {
	ExchangeRate     int64  `json:"exchange_rate"`     // 换算汇率
	CurrentAmount    int64  `json:"current_amount"`    // 当前金额
	OriginalAmount   int64  `json:"original_amount"`   // 原始金额
	CurrentCurrency  string `json:"current_currency"`  // 当前货币
	OriginalCurrency string `json:"original_currency"` // 原始货币
}

// 汇率换算
func GetAmountCurrency(req *AmountCurrencyReq) gmodel.AmountCurrency {
	if req.CurrentAmount != 0 {
		if req.CurrentCurrency == req.OriginalCurrency {
			req.CurrentAmount = req.OriginalAmount
		} else {
			req.CurrentAmount = req.OriginalAmount * req.ExchangeRate
		}
	}

	return gmodel.AmountCurrency{
		ExchangeRate:     req.ExchangeRate,
		CurrentAmount:    req.CurrentAmount,
		OriginalAmount:   req.OriginalAmount,
		CurrentCurrency:  req.CurrentCurrency,
		OriginalCurrency: req.OriginalCurrency,
	}
}

type GetAmountInfoReq struct {
	ExchangeRate     int64
	Initiate         int64
	Current          int64
	Change           int64
	ChangeRemark     string
	Metadata         map[string]interface{}
	CurrentCurrency  string
	OriginalCurrency string
}

// Change       AmountCurrency         `json:"change,omitempty"`        // 变动金额
// ChangeRemark string                 `json:"change_remark,omitempty"` // 变动备注
// Current      AmountCurrency         `json:"current"`                 // 当前金额
// Initiate     AmountCurrency         `json:"initiate"`                // 初始金额
// Metadata     map[string]interface{} `json:"metadata"`                // 额外明细

func GetAmountInfo(req GetAmountInfoReq) gmodel.AmountInfo {
	return gmodel.AmountInfo{
		Change: GetAmountCurrency(&AmountCurrencyReq{
			ExchangeRate:     req.ExchangeRate,
			CurrentAmount:    req.Change,
			OriginalAmount:   req.Change,
			CurrentCurrency:  req.OriginalCurrency,
			OriginalCurrency: req.OriginalCurrency,
		}),
		ChangeRemark: req.ChangeRemark,
		Current: GetAmountCurrency(&AmountCurrencyReq{
			ExchangeRate:     req.ExchangeRate,
			CurrentAmount:    req.Current,
			OriginalAmount:   req.Current,
			CurrentCurrency:  req.OriginalCurrency,
			OriginalCurrency: req.OriginalCurrency,
		}),
		Initiate: GetAmountCurrency(&AmountCurrencyReq{
			ExchangeRate:     req.ExchangeRate,
			CurrentAmount:    req.Initiate,
			OriginalAmount:   req.Initiate,
			CurrentCurrency:  req.OriginalCurrency,
			OriginalCurrency: req.OriginalCurrency,
		}),
		Metadata: req.Metadata,
	}
}

type GetAmountCurrencyUSDReq struct {
	ExchangeRate     int64  `json:"exchange_rate"`     // 换算汇率
	CurrentAmount    int64  `json:"current_amount"`    // 当前金额
	OriginalAmount   int64  `json:"original_amount"`   // 原始金额
	CurrentCurrency  string `json:"current_currency"`  // 当前货币
	OriginalCurrency string `json:"original_currency"` // 原始货币
}

// 处理金额(元)
func GetAmountCurrencyFormat(req *gmodel.AmountCurrency) (res gmodel.AmountCurrency) {
	return gmodel.AmountCurrency{
		ExchangeRate:     format.CentitoDollarStr(req.ExchangeRate.(float64)),
		CurrentAmount:    format.CentitoDollarStr(req.CurrentAmount.(float64)),
		OriginalAmount:   format.CentitoDollarStr(req.OriginalAmount.(float64)),
		CurrentCurrency:  req.CurrentCurrency,
		OriginalCurrency: req.OriginalCurrency,
	}
}

// 处理金额(元)
func GetAmountInfoFormat(req *gmodel.AmountInfo) gmodel.AmountInfo {
	Current := GetAmountCurrencyFormat(&req.Current)
	fmt.Println(Current)
	return gmodel.AmountInfo{
		Change:       GetAmountCurrencyFormat(&req.Change),
		ChangeRemark: req.ChangeRemark,
		Current:      GetAmountCurrencyFormat(&req.Current),
		Initiate:     GetAmountCurrencyFormat(&req.Initiate),
		Metadata:     req.Metadata,
	}
}

// 生成订单编号
func GenerateOrderNumber() string {
	t := time.Now()
	orderNumber := fmt.Sprintf("%d%02d%02d%08d", t.Year(), t.Month(), t.Day(), t.UnixNano()%100000000)
	fmt.Println(orderNumber)
	return orderNumber
}

// 初始化订单状态
func GenerateOrderStatusLink(deliveryMethod int64, noTime time.Time, expectedTime time.Time) []gmodel.OrderStatus {
	var list []gmodel.OrderStatus

	var orderStatus []constants.OrderStatusCode
	if deliveryMethod == constants.DELIVERYMETHODDIRECTMAIL {
		orderStatus = constants.OrderStatusUserDIRECTMAIL
	} else {
		orderStatus = constants.OrderStatusUserCLOUDSTORE
	}
	for _, v := range orderStatus {
		list = append(list, gmodel.OrderStatus{
			StatusCode:  v,
			StatusTitle: constants.OrderStatusMessage[v],
		})
	}
	list[0].Ctime = &noTime
	list[0].Utime = &noTime
	list[len(list)-1].ExpectedTime = &expectedTime
	return list
}

// 获取订单状态
func GetOrderStatusLinkUser(deliveryMethod int64, statusLink []gmodel.OrderStatus) []gmodel.OrderStatus {
	var list []gmodel.OrderStatus

	var orderStatus []constants.OrderStatusCode
	if deliveryMethod == constants.DELIVERYMETHODDIRECTMAIL {
		orderStatus = constants.OrderStatusUserDIRECTMAIL
	} else {
		orderStatus = constants.OrderStatusUserCLOUDSTORE
	}
	for _, v := range statusLink {
		for _, orderStatusCode := range orderStatus {
			if v.StatusCode == orderStatusCode {
				list = append(list, v)
			}
		}
	}
	return list
}

// 获取订单当前状态
func GenerateOrderStatusCurrent() gmodel.OrderStatus {
	return gmodel.OrderStatus{}
}