package controllers

import (
	"net/http"

	"git.rosy.net.cn/baseapi/platformapi/jdapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxcallback/orderman"
	"git.rosy.net.cn/jx-callback/business/partner/purchase/jd"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
	"github.com/astaxie/beego/server/web"
)

// Operations about JDOrder
type Djsw2Controller struct {
	web.Controller
}

func (c *Djsw2Controller) handleMsg(handler func(*jdapi.API, interface{}) *jdapi.CallbackResponse) (callbackResponse *jdapi.CallbackResponse) {
	callbackMsg, _, callbackResponse := jdapi.GetCallbackMsg(getUsefulRequest(c.Ctx))
	if callbackResponse == nil {
		callbackResponse = handler(api.FakeJdAPI, callbackMsg.Param)
	}
	return callbackResponse
}

func (c *Djsw2Controller) orderStatus() {
	if c.Ctx.Input.Method() == http.MethodPost {
		callbackResponse := c.handleMsg(func(a *jdapi.API, obj interface{}) (callbackResponse *jdapi.CallbackResponse) {
			if orderMsg, ok := obj.(*jdapi.CallbackOrderMsg); ok {
				orderman.FixedOrderManager.OnNewFakeJdOrder(orderMsg.BillID)
			}
			return nil
		})
		c.Data["json"] = c.transferResponse("orderStatus", callbackResponse)
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}

func (c *Djsw2Controller) NewOrder() {
	c.orderStatus()
}

func (c *Djsw2Controller) OrderAdjust() {
	c.nullOperation()
}

func (c *Djsw2Controller) OrderWaitOutStore() {
	c.nullOperation()
}

func (c *Djsw2Controller) PickFinishOrder() {
	c.nullOperation()
}

func (c *Djsw2Controller) DeliveryOrder() {
	c.nullOperation()
}

func (c *Djsw2Controller) FinishOrder() {
	c.nullOperation()
}

func (c *Djsw2Controller) LockOrder() {
	c.nullOperation()
}

func (c *Djsw2Controller) UnlockOrder() {
	c.nullOperation()
}

func (c *Djsw2Controller) UserCancelOrder() {
	c.nullOperation()
}

func (c *Djsw2Controller) ApplyCancelOrder() {
	c.nullOperation()
}

func (c *Djsw2Controller) VenderAuditApplyCancelOrder() {
	c.nullOperation()
}

func (c *Djsw2Controller) PushDeliveryStatus() {
	if c.Ctx.Input.Method() == http.MethodPost {
		callbackResponse := c.handleMsg(func(a *jdapi.API, obj interface{}) (callbackResponse *jdapi.CallbackResponse) {
			callbackResponse = jd.OnWaybillMsg(obj.(*jdapi.CallbackDeliveryStatusMsg))
			return callbackResponse
		})
		c.Data["json"] = c.transferResponse("PushDeliveryStatus", callbackResponse)
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}

func (c *Djsw2Controller) OrderCommentPush() {
	c.nullOperation()
}

func (c *Djsw2Controller) Token() {
	urlValues, err := utils.HTTPBody2Values(c.Ctx.Input.RequestBody, false)
	if err == nil {
		globals.SugarLogger.Info(utils.Format4Output(utils.URLValues2Map(urlValues), false))
	}
	c.Data["json"] = c.transferResponse("Token", nil)
	c.ServeJSON()
}

func (c *Djsw2Controller) StockIsHave() {
	c.nullOperation()
}

func (c *Djsw2Controller) SinglePromoteCreate() {
	c.nullOperation()
}

func (c *Djsw2Controller) StoreCrud() {
	c.nullOperation()
}

func (c *Djsw2Controller) transferResponse(funcName string, inCallbackResponse *jdapi.CallbackResponse) (outCallbackResponse *jdapi.CallbackResponse) {
	if globals.IsCallbackAlwaysReturnSuccess() {
		return jdapi.SuccessResponse
	}
	if inCallbackResponse == nil {
		return jdapi.SuccessResponse
	}
	return inCallbackResponse
}

func (c *Djsw2Controller) EndOrderFinance() {
	c.nullOperation()
}

func (c *Djsw2Controller) FinanceAdjustment() {
	c.nullOperation()
}

func (c *Djsw2Controller) DeliveryCarrierModify() {
	c.nullOperation()
}

func (c *Djsw2Controller) NewApplyAfterSaleBill() {
	c.nullOperation()
}

func (c *Djsw2Controller) UpdateApplyAfterSaleBill() {
	c.nullOperation()
}

func (c *Djsw2Controller) NewAfterSaleBill() {
	c.nullOperation()
}

func (c *Djsw2Controller) AfterSaleBillStatus() {
	c.nullOperation()
}

func (c *Djsw2Controller) OrderAccounting() {
	c.nullOperation()
}

func (c *Djsw2Controller) nullOperation() {
	c.Data["json"] = c.transferResponse("nullOperation", nil)
	c.ServeJSON()
}

func (c *Djsw2Controller) UpdateSku() {
	c.nullOperation()
}

func (c *Djsw2Controller) OrderAddTips() {
	c.nullOperation()
}
