package q_bida

import (
	"errors"
	"git.rosy.net.cn/baseapi/utils"
	"net/http"
	"strings"
	"time"
)

// GetToken 获取登录状态
func (a *Api) GetToken() error {
	if a.account == "" {
		a.account = Account
	}
	if a.password == "" {
		a.password = Password
	}
	if a.token != "" && a.expirationTime > time.Now().Unix() {
		return nil
	}

	data := &LoginParamsRes{}
	result, err := a.AccessInfo(BaseQBiDaUrl, LogInUrl, http.MethodPost, map[string]interface{}{"account": a.account, "password": a.password})
	if err != nil && !strings.Contains(err.Error(), "成功") {
		return err
	}

	if err := utils.Map2StructByJson(result, data, false); err != nil {
		return err
	}

	if data.Code != 0 {
		return errors.New(data.Msg)
	}

	a.SetToken(data.Data.Token)
	a.SetExpirationTime(time.Now().AddDate(0, 0, 7).Unix())
	return nil
}

// GetExpressPrice 获取快递费用
func (a *Api) GetExpressPrice(param *GetExpressPriceReq) (*GetExpressPriceRes, error) {
	if err := a.CheckTokenExpiration(); err != nil {
		return nil, err
	}

	result, err := a.AccessInfo(BaseQBiDaUrl, GetWayBailMoneyUrl, http.MethodPost, utils.Struct2FlatMap(param))
	if err != nil && !strings.Contains(err.Error(), "成功") {
		return nil, err
	}

	resultData := &GetExpressPriceRes{}
	if err := utils.Map2StructByJson(result, resultData, false); err != nil {
		return nil, err
	}
	if resultData.Code != 0 {
		return nil, errors.New(resultData.Msg)
	}
	return resultData, nil
}

// CreateOrder 创建订单
func (a *Api) CreateOrder(param *MakeOrderReq) (string, error) {
	if err := a.CheckTokenExpiration(); err != nil {
		return "", err
	}

	result, _ := a.AccessInfo(BaseQBiDaUrl, MakeWayBailOrderUrl, http.MethodPost, utils.Struct2FlatMap(param))

	resultData := &MakeOrderRes{}
	if err := utils.Map2StructByJson(result, resultData, false); err != nil {
		return "", err
	}
	if resultData.Code != 0 {
		return "", errors.New(resultData.Msg)
	}

	return resultData.OrderNo, nil
}

// CancelOrder 取消订单
func (a *Api) CancelOrder(param *CancelOrderReq) error {
	if err := a.CheckTokenExpiration(); err != nil {
		return err
	}

	result, err := a.AccessInfo(BaseQBiDaUrl, CancelWayBailOrderUrl, http.MethodPost, utils.Struct2FlatMap(param))
	if err != nil && !strings.Contains(err.Error(), "成功") {
		err = nil //TODO 测试部分，记得删除
		//return err
	}

	resultData := &PublicParams{}
	if err := utils.Map2StructByJson(result, resultData, false); err != nil {
		return err
	}
	if resultData.Code != 0 && resultData.Msg != "success" {
		return errors.New(resultData.Msg)
	}
	return nil
}

// GetOrderDetail 获取订单详情
func (a *Api) GetOrderDetail(param *GetOrderDetailReq) (*OrderDetail, error) {
	if err := a.CheckTokenExpiration(); err != nil {
		return nil, err
	}

	result, err := a.AccessInfo(BaseQBiDaUrl, GetOrderDetailUrl, http.MethodGet, utils.Struct2FlatMap(param))
	if err != nil && !strings.Contains(err.Error(), "成功") {
		return nil, err
	}

	resultData := &GetOrderDetailRes{}
	if err := utils.Map2StructByJson(result, resultData, false); err != nil {
		return nil, err
	}
	if resultData.Code != 0 {
		return nil, err
	}

	return resultData.Data, nil
}
