package lakala

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

// QueryBillBalance 查询账户余额
func (a *API) QueryBillBalance(param *QueryBillBalanceReq) (*QueryBillBalanceResp, error) {
	reqParameter := map[string]interface{}{
		"reqData":   utils.Struct2Map(param, "", false),
		"reqId":     utils.GetUUID(),
		"ver":       "1.0.0",
		"timestamp": time.Now().Unix(),
	}
	result, err := a.AccessAPISign(BillProdUrl, BillQuery, http.MethodPost, "", reqParameter)
	if err != nil {
		return nil, err
	}

	if result["retCode"].(string) != Success {
		return nil, fmt.Errorf(result["retMsg"].(string))
	}

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

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

	return resp, nil
}

// EwalletWithdrawD1 账户D1提现
func (a *API) EwalletWithdrawD1(param *EwalletWithdrawD1Req) (string, string, error) {
	reqParameter := map[string]interface{}{
		"reqData":   utils.Struct2Map(param, "", false),
		"reqId":     utils.GetUUID(),
		"ver":       "1.0.0",
		"timestamp": time.Now().Unix(),
	}
	result, err := a.AccessAPISign(BillProdUrl, BillDrawD1, http.MethodPost, "", reqParameter)
	if err != nil {
		return "", "", err
	}
	if result["retCode"].(string) != Success {
		return "", "", fmt.Errorf(result["retMsg"].(string))
	}

	return result["respData"].(map[string]interface{})["drawJnl"].(string), result["respData"].(map[string]interface{})["merOrderNo"].(string), nil
}

// EwalletWithdrawQuery 提现结果查询
func (a *API) EwalletWithdrawQuery(param *EwalletWithdrawQueryReq) (*EwalletWithdrawQueryResp, error) {
	reqParameter := map[string]interface{}{
		"reqData":   utils.Struct2Map(param, "", false),
		"reqId":     utils.GetUUID(),
		"ver":       "1.0.0",
		"timestamp": time.Now().Unix(),
	}
	result, err := a.AccessAPISign(BillProdUrl, BillDrawD1Query, http.MethodPost, "", reqParameter)
	if err != nil {
		return nil, err
	}

	if result["retCode"].(string) != Success {
		return nil, fmt.Errorf(result["retMsg"].(string))
	}

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

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

	return resp, nil

}

// SettleDrawPattern 设置提款模式
func (a *API) SettleDrawPattern(param *SettleDrawPatternReq) error {
	reqParameter := map[string]interface{}{
		"reqData":   utils.Struct2Map(param, "", false),
		"reqId":     utils.GetUUID(),
		"ver":       "1.0.0",
		"timestamp": time.Now().Unix(),
	}
	result, err := a.AccessAPISign(BillProdUrl, BillSettleProfile, http.MethodPost, "", reqParameter)
	if err != nil {
		return err
	}

	if result["retCode"].(string) != Success {
		return fmt.Errorf(result["retMsg"].(string))
	}

	return nil
}

// EwalletSettleQuery 提款模式查询
func (a *API) EwalletSettleQuery(bmcpNo, mercId string) (*EwalletSettleQueryResp, error) {
	reqParameter := map[string]interface{}{
		"reqData":   map[string]string{"bmcpNo": bmcpNo, "mercId": mercId},
		"reqId":     utils.GetUUID(),
		"ver":       "1.0.0",
		"timestamp": time.Now().Unix(),
	}
	result, err := a.AccessAPISign(BillProdUrl, BillSettleQuery, http.MethodPost, "", reqParameter)
	if err != nil {
		return nil, err
	}

	if result["retCode"].(string) != Success {
		return nil, fmt.Errorf(result["retMsg"].(string))
	}

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

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