package tiktok_api

import (
	"errors"
	"fmt"
	instantShopping_cancelDelivery_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/instantShopping_cancelDelivery/request"
	instantShopping_createDelivery_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/instantShopping_createDelivery/request"
	instantShopping_getDeliveryListByOrderId_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/instantShopping_getDeliveryListByOrderId/request"
	instantShopping_getDeliveryListByOrderId_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/instantShopping_getDeliveryListByOrderId/response"
	superm_applyPlatformPickUp_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_applyPlatformPickUp/request"
	superm_cancelPlatformPickUp_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_cancelPlatformPickUp/request"
	superm_createVirtualMobile_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_createVirtualMobile/request"
	superm_createVirtualMobile_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_createVirtualMobile/response"
	superm_getDispatcherInfo_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_getDispatcherInfo/request"
	superm_getPlatformPickUpEstimatedCharge_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_getPlatformPickUpEstimatedCharge/request"
	superm_getStoreAutoCallRiderInfo_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_getStoreAutoCallRiderInfo/request"
	superm_getStoreAutoCallRiderInfo_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_getStoreAutoCallRiderInfo/response"
	superm_setStoreAutoCallRider_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_setStoreAutoCallRider/request"
	"git.rosy.net.cn/baseapi/utils"
)

// /superm/getDispatcherInfo 运力呼叫询价/运力取消询价(正向)
func (a *API) GetDispatcherInfo(storeID int64, shopOrderID string, dispatcherFeeType int32) (int64, error) {
	request := superm_getDispatcherInfo_request.New()
	request.Param = &superm_getDispatcherInfo_request.SupermGetDispatcherInfoParam{
		StoreID:           storeID,
		ShopOrderID:       shopOrderID,
		DispatcherFeeType: dispatcherFeeType,
	}
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return 0, err
	}
	if response.Code != RequestSuccessCode {
		return 0, errors.New(response.SubMsg + ":" + response.LogId)
	}
	return response.Data.DispatcherFee, nil
}

// ShopOrderDispatcher 取消运力,运力接口改版,取消和重新呼叫分开
func (a *API) ShopOrderDispatcher(storeID int64, shopOrderID string, dispatcherType int32) error {
	switch dispatcherType {
	case DispatcherFeeTypeCancel:
		request := instantShopping_cancelDelivery_request.New()
		request.Param = &instantShopping_cancelDelivery_request.InstantShoppingCancelDeliveryParam{
			ShopOrderId: shopOrderID,
			StoreId:     storeID,
			CancelCode:  0,
		}
		response, err := request.Execute(a.accessTokenObj)
		if err != nil {
			return err
		}
		if response.Code != RequestSuccessCode && response.Code != 90000 {
			return errors.New(response.SubMsg + ":" + response.LogId)
		}
		return nil
	case DispatcherFeeTypeCall:
		request := instantShopping_createDelivery_request.New()
		request.Param = &instantShopping_createDelivery_request.InstantShoppingCreateDeliveryParam{
			ShopOrderId: shopOrderID,
			StoreId:     storeID,
		}
		response, err := request.Execute(a.accessTokenObj)
		if err != nil {
			return err
		}
		if response.Code != RequestSuccessCode {
			return errors.New(response.SubMsg + ":" + response.LogId)
		}
		return nil
	}

	// 老版本
	//request := superm_orderDispatcher_request.New()
	//request.Param = &superm_orderDispatcher_request.SupermOrderDispatcherParam{
	//	StoreID:        storeID,
	//	ShopOrderID:    shopOrderID,
	//	DispatcherType: dispatcherType,
	//}
	return fmt.Errorf("抖音运力呼叫/取消type异常")
}

// /superm/setStoreAutoCallRider 订单自动呼叫运力 设置操作
func (a *API) SetStoreAutoCallRider(params *superm_setStoreAutoCallRider_request.SupermSetStoreAutoCallRiderParam) error {
	request := superm_setStoreAutoCallRider_request.New()
	request.Param = params
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		return errors.New(response.SubMsg + ":" + response.LogId)
	}
	return nil
}

// /superm/getStoreAutoCallRiderInfo 查询自动呼叫运力配置
func (a *API) GetStoreAutoCallRiderInfo(storeID int64) (*superm_getStoreAutoCallRiderInfo_response.AutoCallInfo, error) {
	request := superm_getStoreAutoCallRiderInfo_request.New()
	request.Param = &superm_getStoreAutoCallRiderInfo_request.SupermGetStoreAutoCallRiderInfoParam{
		StoreID: storeID,
	}
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.SubMsg + ":" + response.LogId)
	}
	return response.Data.AutoCallInfo, nil
}

// /superm/createVirtualMobile 隐私小号获取
func (a *API) CreateVirtualMobile(shopOrderID int64) (*superm_createVirtualMobile_response.SupermCreateVirtualMobileData, error) {
	request := superm_createVirtualMobile_request.New()
	request.Param = &superm_createVirtualMobile_request.SupermOrderDispatcherParam{
		ShopOrderID: shopOrderID,
	}
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.SubMsg + ":" + response.LogId)
	}
	return response.Data, nil
}

// /superm/getPlatformPickUpEstimate]dCharge 运力呼叫询价/运力取消询价(逆向)
func (a *API) GetPlatformPickUpEstimatedCharge(afterSaleID int64, inquiryType int32) (int64, error) {
	request := superm_getPlatformPickUpEstimatedCharge_request.New()
	request.Param = &superm_getPlatformPickUpEstimatedCharge_request.SupermGetPlatformPickUpEstimatedChargeParam{
		AfterSaleID: afterSaleID,
		InquiryType: inquiryType,
	}
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return 0, err
	}
	if response.Code != RequestSuccessCode {
		return 0, errors.New(response.SubMsg + ":" + response.LogId)
	}
	return response.Data.PlatformPickupEstimatedCharge, err
}

// /superm/applyPlatformPickUp 呼叫运力售后取件
func (a *API) ApplyPlatformPickUp(afterSaleID int64) (string, error) {
	request := superm_applyPlatformPickUp_request.New()
	request.Param = &superm_applyPlatformPickUp_request.SupermApplyPlatformPickUpParam{
		AfterSaleID: afterSaleID,
	}
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return "", err
	}
	if response.Code != RequestSuccessCode {
		return "", errors.New(response.SubMsg + ":" + response.LogId)
	}
	return response.Data.LogisticsID, err
}

// GetShipmentInfo 查询运力订单信息
func (a *API) GetShipmentInfo(shopOrderID, afterSaleID, shipmentType int64) (*instantShopping_getDeliveryListByOrderId_response.InstantShoppingGetDeliveryListByOrderIdData, error) {
	request := instantShopping_getDeliveryListByOrderId_request.New()
	request.Param = &instantShopping_getDeliveryListByOrderId_request.InstantShoppingGetDeliveryListByOrderIdParam{
		ShopOrderId: utils.Int64ToStr(shopOrderID),
	}
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.SubMsg + ":" + response.LogId)
	}

	return response.Data, nil
	//request := superm_getShipmentInfo_request.New()
	//request.Param = &superm_getShipmentInfo_request.SupermGetShipmentInfoParam{
	//	ShopOrderID:  shopOrderID,
	//	ShipmentType: shipmentType,
	//}
	//response, err := request.Execute(a.accessTokenObj)
	//if err != nil {
	//	return nil, err
	//}
	//
	//if strings.Contains(response.SubMsg, "订单不存在") || strings.Contains(response.SubMsg, "未找到当前物流订单, LO") {
	//	return &superm_getShipmentInfo_response.ShipmentInfo{
	//		ShopOrderID:    shopOrderID,
	//		AfterSaleID:    0,
	//		TrackNo:        "",
	//		ShopID:         0,
	//		ShipmentStatus: -1,
	//		ShipmentError:  0,
	//		RiderName:      "",
	//		RiderPhone:     "",
	//		RiderLongitude: "",
	//		RiderLatitude:  "",
	//		OccurredTime:   "",
	//	}, nil
	//}
	//
	//return response.Data.ShipmentInfo, err
}

// /superm/cancelPlatformPickUp 取消运力 (逆向)
func (a *API) CancelPlatformPickUp(afterSaleID int64) error {
	request := superm_cancelPlatformPickUp_request.New()
	request.Param = &superm_cancelPlatformPickUp_request.SupermCancelPlatformPickUpParam{
		AfterSaleID: afterSaleID,
	}
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		return errors.New(response.SubMsg + ":" + response.LogId)
	}
	return err
}
