package lakala

import (
	"encoding/json"
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
	"net/http"
	"time"
)

// AggregatePay 主扫聚合支付(小程序支付)
func (a *API) AggregatePay(param *AggregatePayReq) (*AggregatePayResp, error) {
	reqParameter := map[string]interface{}{
		"req_data": utils.Struct2Map(param, "", false),
		"version":  Version3,
		"req_time": utils.Time2TimeStrByFormat(time.Now(), TimeFormat),
	}
	globals.SugarLogger.Debugf("-------AggregatePay := %s", utils.Format4Output(param, false))
	result, err := a.AccessAPISign(PayProdUrl, PayActive, http.MethodPost, "", reqParameter)
	if err != nil {
		return nil, err
	}
	globals.SugarLogger.Debugf("-------result := %s", utils.Format4Output(result, false))
	globals.SugarLogger.Debugf("-------err := %v", err)

	if result["code"].(string) != PaySuccess {
		return nil, fmt.Errorf(result["msg"].(string))
	}

	bodyResult, err := json.Marshal(result["resp_data"])
	if err != nil {
		return nil, err
	}

	resp := &AggregatePayResp{}
	if err = json.Unmarshal(bodyResult, resp); err != nil {
		return nil, err
	}

	return resp, nil
}

// AggregateRefund 聚合退款
func (a *API) AggregateRefund(param *AggregateRefundReq) (*AggregateRefundResp, error) {
	reqParameter := map[string]interface{}{
		"req_data": utils.Struct2Map(param, "", false),
		"version":  Version3,
		"req_time": utils.Time2TimeStrByFormat(time.Now(), TimeFormat),
	}
	result, err := a.AccessAPISign(PayProdUrl, RefundActive, http.MethodPost, "", reqParameter)
	if err != nil {
		return nil, err
	}

	if result["code"].(string) != PaySuccess {
		return nil, fmt.Errorf(result["msg"].(string))
	}

	bodyResult, err := json.Marshal(result["resp_data"])
	if err != nil {
		return nil, err
	}

	resp := &AggregateRefundResp{}
	if err = json.Unmarshal(bodyResult, resp); err != nil {
		return nil, err
	}

	return resp, nil

}

// PayStatusQuery 支付查询
func (a *API) PayStatusQuery(param *PayStatusQueryReq) (*PayStatusQueryResp, error) {
	reqParameter := map[string]interface{}{
		"req_data": utils.Struct2Map(param, "", false),
		"version":  Version3,
		"req_time": utils.Time2TimeStrByFormat(time.Now(), TimeFormat),
	}
	result, err := a.AccessAPISign(PayProdUrl, PayQueryActive, http.MethodPost, "", reqParameter)
	if err != nil {
		return nil, err
	}

	if result["code"].(string) != PaySuccess {
		return nil, fmt.Errorf(result["msg"].(string))
	}

	bodyResult, err := json.Marshal(result["resp_data"])
	if err != nil {
		return nil, err
	}

	resp := &PayStatusQueryResp{}
	if err = json.Unmarshal(bodyResult, resp); err != nil {
		return nil, err
	}

	return resp, nil
}

// ClosePay 关闭支付订单
func (a *API) ClosePay(param *ClosePayMiniOrder) error {
	reqParameter := map[string]interface{}{
		"req_data": utils.Struct2Map(param, "", false),
		"version":  Version3,
		"req_time": utils.Time2TimeStrByFormat(time.Now(), TimeFormat),
	}
	result, err := a.AccessAPISign(PayProdUrl, ClosePayActive, http.MethodPost, "", reqParameter)
	if err != nil {
		return err
	}

	if result["code"].(string) != PaySuccess {
		return fmt.Errorf(result["msg"].(string))
	}

	return nil
	//
	//bodyResult, err := json.Marshal(result["resp_data"])
	//if err != nil {
	//	return nil, err
	//}
	//
	//resp := &PayStatusQueryResp{}
	//if err = json.Unmarshal(bodyResult, resp); err != nil {
	//	return nil, err
	//}
	//
	//return resp, nil
}

type ClosePayMiniOrder struct {
	MerchantNo       string `json:"merchant_no"`
	TermNo           string `json:"term_no"`
	OriginOutTradeNo string `json:"origin_out_trade_no"`
	OriginTradeNo    string `json:"origin_trade_no"`
	LocationInfo     struct {
		RequestIp   string `json:"request_ip"`
		BaseStation string `json:"base_station"`
		Location    string `json:"location"`
	} `json:"location_info"`
}

// ScannerPayMicroPay 扫码枪扫码支付
func (a *API) ScannerPayMicroPay(param *PayMicroPayReq) (*PayMicroPayResp, error) {
	reqParameter := map[string]interface{}{
		"req_data": utils.Struct2Map(param, "", false),
		"version":  Version3,
		"req_time": utils.Time2TimeStrByFormat(time.Now(), TimeFormat),
	}
	result, err := a.AccessAPISign(PayProdUrl, PayMicropayActive, http.MethodPost, "", reqParameter)
	if err != nil {
		return nil, err
	}

	switch result["code"].(string) {
	case PaySuccess, "BBS10000", "BBS11105":
		bodyResult, err := json.Marshal(result["resp_data"])
		if err != nil {
			return nil, err
		}

		resp := &PayMicroPayResp{}
		if err = json.Unmarshal(bodyResult, resp); err != nil {
			return nil, err
		}
		return resp, nil
	default:
		return nil, fmt.Errorf(result["msg"].(string))
	}

}
