package cooper

import (
	"PPOSUtils/utils"
	"encoding/json"
	"errors"
	"net/url"
	"reflect"
)

type Request struct {
	Client            *CooperClient
	params            url.Values
	apiAddress        string
	publicParamsSlice []string //必传参数名
}

func (that *Request) initParams() *Request {
	if that.params == nil {
		that.params = make(url.Values)
	}
	return that
}

//微信支付配置\支付宝支付配置\银联支付配置，json格式
func (that *Request) SetPayCfg(value string) *Request {
	that.initParams()
	that.params.Set("pay_cfg", value)
	return that
}

//微信退款证书配置，json格式
func (that *Request) SetWxCertCfg(value string) *Request {
	that.initParams()
	that.params.Set("wx_cert_cfg", value)
	return that
}

func (that *Request) SetCompanyID(companyID int64) *Request {
	that.initParams()
	that.params.Set("company_id", utils.ToString(companyID))
	return that
}

func (that *Request) GetCompanyID() string {
	that.initParams()
	return that.params.Get("company_id")
}

func (that *Request) SetBusinessID(businessID int64) *Request {
	that.initParams()
	that.params.Set("business_id", utils.ToString(businessID))
	return that
}

func (that *Request) GetBusinessID() string {
	that.initParams()
	return that.params.Get("business_id")
}

func (that *Request) SetStadiumID(stadiumID int64) *Request {
	that.initParams()
	that.params.Set("stadium_id", utils.ToString(stadiumID))
	return that
}

func (that *Request) GetStadiumID() string {
	that.initParams()
	return that.params.Get("stadium_id")
}

func (that *Request) SetSalesID(salesID int64) *Request {
	that.initParams()
	that.params.Set("sales_id", utils.ToString(salesID))
	return that
}

func (that *Request) GetSalesID() string {
	that.initParams()
	return that.params.Get("sales_id")
}

func (that *Request) SetAccountID(accountID int64) *Request {
	that.initParams()
	that.params.Set("account_id", utils.ToString(accountID))
	return that
}

func (that *Request) GetAccountID() string {
	that.initParams()
	return that.params.Get("sales_id")
}

func (that *Request) SetAccountType(accountType int64) *Request {
	that.initParams()
	that.params.Set("account_type", utils.ToString(accountType))
	return that
}

func (that *Request) GetAccountType() string {
	that.initParams()
	return that.params.Get("account_type")
}

func (that *Request) SetMpID(mpID int64) *Request {
	that.initParams()
	that.params.Set("mp_id", utils.ToString(mpID))
	return that
}

func (that *Request) GetMpID() string {
	that.initParams()
	return that.params.Get("mp_id")
}

func (that *Request) SetPersonnelID(personnelID int64) *Request {
	that.initParams()
	that.params.Set("personnel_id", utils.ToString(personnelID))
	return that
}

func (that *Request) GetPersonnelID() string {
	that.initParams()
	return that.params.Get("personnel_id")
}

func (that *Request) SetMemberID(memberID int64) *Request {
	that.initParams()
	that.params.Set("member_id", utils.ToString(memberID))
	return that
}

func (that *Request) GetMemberID() string {
	that.initParams()
	return that.params.Get("member_id")
}

func (that *Request) ParseList(data interface{}) (*struct {
	Count int
	Page  int
	Size  int
	List  interface{}
}, error) {
	_json, _err := json.Marshal(data)
	if _err != nil {
		return nil, _err
	}
	_list := &struct {
		Count int
		Page  int
		Size  int
		List  interface{}
	}{}
	if _err := json.Unmarshal(_json, &_list); _err != nil {
		return nil, _err
	}
	return _list, nil
}

func (that *Request) ParseResponeMp(mode interface{}, data interface{}, isMustValidate ...bool) error {
	_mp, _ok := data.(map[string]interface{})
	if !_ok {
		return errors.New(that.apiAddress + "parse response error, data not map")
	}
	v := reflect.ValueOf(mode)
	if !v.IsNil() {
		t := reflect.TypeOf(mode)
		fieldNum := t.Elem().NumField()
		for i := 0; i < fieldNum; i++ {
			_type := t.Elem().Field(i).Type
			_k := t.Elem().Field(i).Tag.Get("json")
			if _k != "" {
				_v, _ok := _mp[_k]
				//非强制性验证字段属性
				if len(isMustValidate) > 0 && isMustValidate[0] == false && !_ok {
					continue
				}
				//强制验证字段必填
				if !_ok {
					return errors.New("response error:" + _k)
				}
				//字段所属类型
				switch _type.String() {
				case "int64":
					v.Elem().Field(i).SetInt(utils.ToInt64(_v))
				case "string":
					v.Elem().Field(i).SetString(utils.ToString(_v, true))
				case "bool":
					if _bool, _ok := _v.(bool); _ok {
						v.Elem().Field(i).SetBool(_bool)
					} else {
						v.Elem().Field(i).SetBool(false)
					}
				}
			}
		}
	}
	return nil
}
