package tiktok_store

import (
	"fmt"
	tiktokShop "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/tiktok_api"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/partner"
	"strings"
)

var (
	AfsVendorStatus2StatusMap = map[string]int{
		tiktokShop.CallbackRefundOrderMsgTagId:        model.AfsOrderStatusWait4Approve, // 发起售后申请待审核
		tiktokShop.CallbackUpdateRefundOrderMsgTagId:  model.AfsOrderStatusWait4Approve, // 修改售后待审核
		tiktokShop.CallbackRefundOrderSuccessMsgTagId: model.AfsOrderStatusFinished,     // 退款成功
		tiktokShop.CallbackRefundOrderRefuseMsgTagId:  model.AfsOrderStatusFailed,       // 拒绝退款
		tiktokShop.CallbackRefundShopMsgTagId:         model.AfsOrderStatusFailed,       // 拒绝退货
		tiktokShop.CallbackReturnApplyAgreedMsgTagId:  model.AfsOrderStatusFinished,     // 同意退货
		tiktokShop.CallbackReturnRefundAgreedMsgTagId: model.AfsOrderStatusFinished,     // 同意退款
		tiktokShop.CallbackRefundClosedMsgTagId:       model.AfsOrderStatusFailed,       // 驳回
	}

	AfsTagIDMap = map[string]string{
		tiktokShop.CallbackRefundOrderMsgTagId:               "1",
		tiktokShop.CallbackUpdateRefundOrderMsgTagId:         "1",
		tiktokShop.CallbackRefundOrderSuccessMsgTagId:        "1",
		tiktokShop.CallbackRefundOrderRefuseMsgTagId:         "1",
		tiktokShop.CallbackRefundShopMsgTagId:                "1",
		tiktokShop.CallbackReturnApplyAgreedMsgTagId:         "1",
		tiktokShop.CallbackReturnRefundAgreedMsgTagId:        "1",
		tiktokShop.CallbackExchangeComfirmedMsgTagId:         "1",
		tiktokShop.CallbackArbitrateDiscussUploadMsgTagId:    "1",
		tiktokShop.CallbackArbitrateServiceInterveneMsgTagId: "1",
		tiktokShop.CallbackArbitrateCancelledMsgTagId:        "1",
		tiktokShop.CallbackArbitrateAuditedMsgTagId:          "1",
		tiktokShop.CallbackArbitrateSubmitedMsgTagId:         "1",
		tiktokShop.CallbackArbitrateSubmitingMsgTagId:        "1",
		tiktokShop.CallbackArbitrateAppliedMsgTagId:          "1",
		tiktokShop.CallbackExpirationChangeMsgTagId:          "1",
		tiktokShop.CallbackRefundClosedMsgTagId:              "1",
		tiktokShop.CallbackBuyerReturnGoodsMsgTagId:          "1",
		tiktokShop.CallbackSpecialRefundMsgTagId:             "1",
		tiktokShop.CallbackSpecialRefundSuccessMsgTagId:      "1",
		tiktokShop.CallbackAuditAgreeResendMsgTagId:          "1",
		tiktokShop.CallbackResendFillLogisticsMsgTagId:       "1",
	}

	// ReasonCodeMap 申请退货理由,可能会更新
	ReasonCodeMap = map[int]string{
		1:  "商品已发出，如买家不再需要请拒收后申请仅退款或收到后申请退货退款",
		2:  "商品已经签收,如买家不再需要可以申请退货退款",
		3:  "买家误操作/取消申请",
		4:  "问题已解决，待用户收货",
		5:  "商品已发出，如买家不再需要请拒收后申请仅退款或收到后申请退货退款",
		6:  "买家误操作/取消申请",
		7:  "协商一致，用户取消退款",
		8:  "已与买家协商补偿，包括差价、赠品、额外补偿",
		9:  "已与买家协商补发商品",
		10: "已与买家协商换货",
		11: "买家上传的单号有误，商家尚未收到货，请核实正确物流单号后重新上传",
		12: "退货与原订单不符（商品不符、退货地址不符）",
		13: "退回商品影响二次销售",
		14: "买家误操作/取消申请",
		15: "协商一致，用户取消退款",
		16: "买家误操作/取消申请",
		17: "协商一致，用户取消退款",
		18: "商品影响二次销售",
		19: "定制商品不支持七天无理由退货，定制商品不接受质量问题以外的退货",
		20: "定制商品不支持七天无理由退货，定制商品不接受质量问题以外的退货",
		21: "买家申请的金额有误",
		22: "运费未协商一致",
		23: "商品没问题，买家未举证或凭证无效",
		24: "已在约定时间发货",
		25: "运费未协商一致",
		26: "商品已经签收，如买家不再需要可以申请退货退款",
		27: "商品没问题，买家未举证或举证无效",
		28: "已在约定时间发货",
		29: "买家申请的金额有误",
		30: "发票没问题，买家未举证",
		31: "发票已补寄",
		32: "买家发票信息不完整",
		33: "运费未协商一致",
		34: "申请时间已超7天无理由退换货时间",
		35: "不支持买家主观原因退换货",
		36: "买家填错号码",
		37: "已完成服务，买家未提供凭证或凭证无效",
		38: "买家填错号码",
		39: "已完成服务，买家未提供凭证或凭证无效",
		40: "和达人达成一致，取消终止",
		41: "其他",
		42: "其他",
		43: "未收到货/退货单号有误",
		44: "退货与原订单不符（商品不符、退货地址不符）",
		45: "商家已发货",
		46: "商品已经签收，如买家不再需要可以申请退货退款",
		47: "已与买家协商一致仅退款",
		48: "问题已解决，待用户确认收货",
		49: "已与买家协商一致延迟发货",
		50: "未少发漏发",
		51: "已与买家协商补偿，包括差价、赠品、额外补偿",
		52: "已与买家协商一致延迟发货",
		53: "其他",
		54: "本单已购买【养死包赔】保险，请从保险理赔入口申请理赔",
		55: "本单已购买【开箱无忧】保险，请从保险理赔入口申请理赔",
		56: "商品没问题，买家未举证或举证无效",
		57: "已完成服务，买家未举证或举证无效",
		58: "本单已购买【食安保】保险，请从保险理赔入口申请理赔",
	}
)

// AgreeOrRefuseRefund todo 审核售后单申请 退货退款(同意退货,确认收货之后会自动退款) 审核售后有点问题
func (c *PurchaseHandler) AgreeOrRefuseRefund(ctx *jxcontext.Context, order *model.AfsOrder, approveType int, reason string, review map[string]interface{}) (err error) {
	tiktokApi := getAPI(order.VendorOrgCode, order.JxStoreID, "")
	//afsOrderDetail, err := tiktokApi.QueryAfsOrderDetail(order.AfsOrderID, false)
	//if err != nil {
	//	return err
	//}

	if approveType == partner.AfsApproveTypeRefused { // 拒绝退货
		// tiktokShop.AfterSaleEmuRefuseToReturnOneApply
		// tiktokShop.AfterSaleEmuRefuseToReturnTwoApply
		// tiktokShop.AfterSaleEmuRefuseOnlyRefundApply
		// tiktokShop.AfterSaleEmuRefundChangeGoodsOneApply
		// tiktokShop.AfterSaleEmuRefundChangeGoodsTwoApply
		// tiktokShop.AfterSaleEmuRefundReissueApply

		//switch afsOrderDetail.Data.ProcessInfo.AfterSaleInfo.AfterSaleType {
		//case :
		//
		//}

		errList := make([]string, 0, 0)
		// 拒绝退款
		if err = tiktokApi.AfterSaleOperate(tiktokShop.AfterSaleEmuRefuseToReturnOneApply, order.AfsOrderID, reason, utils.Str2Int64(order.VendorStoreID)); err != nil {
			errList = append(errList, err.Error())
		} else {
			return nil
		}
		if err = tiktokApi.AfterSaleOperate(tiktokShop.AfterSaleEmuRefuseOnlyRefundApply, order.AfsOrderID, reason, utils.Str2Int64(order.VendorStoreID)); err != nil {
			errList = append(errList, err.Error())
		} else {
			return nil
		}

		if err = tiktokApi.AfterSaleOperate(tiktokShop.AfterSaleEmuRefuseToReturnTwoApply, order.AfsOrderID, reason, utils.Str2Int64(order.VendorStoreID)); err != nil {
			errList = append(errList, err.Error())
		} else {
			return nil
		}
		if len(errList) < 3 {
			return nil
		}
		return fmt.Errorf("%s", strings.Join(errList, "--"))
	} else if approveType == partner.AfsApproveTypeRefund { // 同意
		// tiktokShop.AfterSaleEmuAgreeToReturnOneApply
		// tiktokShop.AfterSaleEmuAgreeToReturnTwoApply
		// tiktokShop.AfterSaleEmuReturnGoodsToRefundApply
		// tiktokShop.AfterSaleEmuAgreeOnlyRefundApply
		// tiktokShop.AfterSaleEmuAgreeChangeGoodsOneApply
		// tiktokShop.AfterSaleEmuAgreeChangeGoodsTwoApply
		// tiktokShop.AfterSaleEmuChangeGoodsToRefundApply
		// tiktokShop.AfterSaleEmuAgreePreSaleReturnApply
		// tiktokShop.AfterSaleEmuAgreeReissueApply

		errList := make([]string, 0, 0)
		if err = tiktokApi.AfterSaleOperate(tiktokShop.AfterSaleEmuAgreeToReturnOneApply, order.AfsOrderID, reason, utils.Str2Int64(order.VendorStoreID)); err != nil {
			errList = append(errList, err.Error())
		} else {
			return nil
		}
		// 同意仅退款
		if err = tiktokApi.AfterSaleOperate(tiktokShop.AfterSaleEmuAgreeOnlyRefundApply, order.AfsOrderID, reason, utils.Str2Int64(order.VendorStoreID)); err != nil {
			errList = append(errList, err.Error())
		} else {
			return nil
		}

		if err = tiktokApi.AfterSaleOperate(tiktokShop.AfterSaleEmuAgreeToReturnTwoApply, order.AfsOrderID, reason, utils.Str2Int64(order.VendorStoreID)); err != nil {
			errList = append(errList, err.Error())
		} else {
			return nil
		}

		return fmt.Errorf("%s", strings.Join(errList, "--"))
	} else if approveType == partner.AfsApproveTypeRefusedToRefundMoney { // 退货转退款
		err = tiktokApi.AfterSaleOperate(tiktokShop.AfterSaleEmuReturnGoodsToRefundApply, order.AfsOrderID, "退货转退款", utils.Str2Int64(order.VendorStoreID))
	}
	return err
}

// 确认收到退货
func (c *PurchaseHandler) ConfirmReceivedReturnGoods(ctx *jxcontext.Context, order *model.AfsOrder) (err error) {
	err = fmt.Errorf("暂不支持的API调用")
	return err
}

// 发起全款退款
func (c *PurchaseHandler) RefundOrder(ctx *jxcontext.Context, order *model.GoodsOrder, reason string) (err error) {
	return fmt.Errorf("%s不支持售后全额退款，请让买家发起退款", model.VendorChineseNames[model.VendorIDDD])
}

// 发起部分退款
func (c *PurchaseHandler) PartRefundOrder(ctx *jxcontext.Context, order *model.GoodsOrder, refundSkuList []*model.OrderSku, reason string) (err error) {
	return c.AdjustOrder(ctx, order, refundSkuList, reason)
}

func (c *PurchaseHandler) GetOrderAfsInfo(ctx *jxcontext.Context, vendorOrderID, afsOrderID string) (orderAfsInfo *partner.OrderAfsInfo, err error) {
	order, err := partner.CurOrderManager.LoadOrder(vendorOrderID, model.VendorIDDD)
	if err != nil {
		return nil, err
	}

	list, err := getAPI(order.VendorOrgCode, 0, "").QueryAfsOrderDetail(afsOrderID, false)
	if err != nil {
		return nil, err
	}

	orderAfsInfo = &partner.OrderAfsInfo{}
	orderAfsInfo.VendorOrderID = order.VendorOrderID
	orderAfsInfo.AfsOrderID = afsOrderID
	orderAfsInfo.AfsTotalShopMoney = order.TotalShopMoney - list.Data.ProcessInfo.AfterSaleInfo.RealRefundAmount
	return orderAfsInfo, err
}
