package ctrl_v1

import (
	"gd_management_gateway/errors"
	"gd_management_gateway/params/param_base"
	"gd_management_gateway/params/param_v1_0"
	"gd_management_gateway/rpc_apis"
	"gd_management_gateway/rpc_apis/gd_management"
	"fmt"
	"time"

	"gd_management_gateway/common.in/httper"
	"gd_management_gateway/common.in/task"
	"gd_management_gateway/common.in/utils"
	"go.uber.org/zap"

	"github.com/astaxie/beego"
)

// Operations about base api
type H5ApiController struct {
	metadata interface{} // 中继元数据
	beego.Controller
	LogID string
}

// @Title add h5 api
// @Description 添加 h5 api
// @Param   token    header   string true "token"
// @Param   uid    header   string true "uid"
// @Param   h5_api_name    query   string true "h5_api_name"
// @Param   h5_api_router    query   string true "h5_api_router"
// @Param   h5_api_host   query   string true "h5_api_host"
// @Success 200 	{object} params.param_v1_0.ManagementAddH5ApiResp "响应信息"
// @Failure 500 	服务器错误
// @router /h5_api [post]
func (u *H5ApiController) ManagementAddH5Api() {
	u.LogID = fmt.Sprintf("ManagementAddH5Api[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementAddH5ApiReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementAddH5ApiReq{}
		mreq.Router = req.Router
		mreq.Host = req.Host
		mreq.Name = req.Name

		reply, err := rpc_apis.Management.ManagementAddH5Api(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementAddH5Api"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementAddH5ApiResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.H5ApiId = reply.H5ApiId
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title del h5 api
// @Description 删除h5 api
// @Param   token    header   string true "token"
// @Param   uid    header   string true "uid"
// @Param   h5_api_id    query   int64 true "h5_api_id"
// @Success 200 	{object} params.param_v1_0.ManagementDelH5ApiResp "响应信息"
// @Failure 500 	服务器错误
// @router /h5_api [delete]
func (u *H5ApiController) ManagementDelH5Api() {
	u.LogID = fmt.Sprintf("ManagementDelH5Api[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementDelH5ApiReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementDelH5ApiReq{}
		mreq.H5ApiId = req.H5ApiId

		_, err := rpc_apis.Management.ManagementDelH5Api(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementDelH5Api"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementDelH5ApiResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title update h5 api
// @Description 更新h5 api
// @Param   token    header   string true "token"
// @Param   uid    header   string true "uid"
// @Param   h5_api_id    query   int64 true "h5_api_id"
// @Param   h5_api_name    query   string true "h5_api_name"
// @Param   h5_api_router    query   string true "h5_api_router"
// @Param   h5_api_host    query   string true "h5_api_host"
// @Success 200 	{object} params.param_v1_0.ManagementUpdateH5ApiResp "响应信息"
// @Failure 500 	服务器错误
// @router /h5_api [put]
func (u *H5ApiController) ManagementUpdateH5Api() {
	u.LogID = fmt.Sprintf("ManagementUpdateH5Api[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementUpdateH5ApiReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementUpdateH5ApiReq{}
		mreq.Router = req.Router
		mreq.Host = req.Host
		mreq.Name = req.Name
		mreq.H5ApiId = req.H5ApiId

		_, err := rpc_apis.Management.ManagementUpdateH5Api(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementUpdateH5Api"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementUpdateH5ApiResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title set h5 api state
// @Description 禁用启用h5 api
// @Param   token    header   string true "token"
// @Param   uid    header   string true "uid"
// @Param   h5_api_id    query   int64 true "h5_api_id"
// @Param   is_enable    query   bool true "is_enable"
// @Success 200 	{object} params.param_v1_0.ManagementSetH5ApiStateResp "响应信息"
// @Failure 500 	服务器错误
// @router /h5_api_state [put]
func (u *H5ApiController) ManagementSetH5ApiState() {
	u.LogID = fmt.Sprintf("ManagementSetH5ApiState[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementSetH5ApiStateReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementSetH5ApiStateReq{}
		mreq.IsEnable = req.IsEnable
		mreq.H5ApiId = req.H5ApiId

		_, err := rpc_apis.Management.ManagementSetH5ApiState(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementSetH5ApiState"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementSetH5ApiStateResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title get h5 api list
// @Description 获取h5 api列表
// @Param   token    header   string true "token"
// @Param   uid    header   string true "uid"
// @Param   is_all    query   bool true "is_all"
// @Param   page_number    query   int true "page_number"
// @Success 200 	{object} params.param_v1_0.ManagementGetH5ApiListResp "响应信息"
// @Failure 500 	服务器错误
// @router /h5_api_list [get]
func (u *H5ApiController) ManagementGetH5ApiList() {
	u.LogID = fmt.Sprintf("ManagementGetH5ApiList[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementGetH5ApiListReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementGetH5ApiListReq{}
		mreq.PageNumber = req.PageNumber
		mreq.IsAll = req.IsAll

		reply, err := rpc_apis.Management.ManagementGetH5ApiList(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementGetH5ApiList"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementGetH5ApiListResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Total = reply.Total
		resp.PageNumber = reply.PageNumber
		resp.PageSize = reply.PageSize
		resp.H5ApiList = reply.H5ApiList
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title h5 api add base api
// @Description h5 api 添加基础api
// @Param   token    header   string true "token"
// @Param   uid    header   string true "uid"
// @Param   data    body   params.param_v1_0.H5ApiAddBaseApiData true "data"
// @Success 200 	{object} params.param_v1_0.ManagementH5ApiAddBaseApiResp "响应信息"
// @Failure 500 	服务器错误
// @router /h5_api_base_api [post]
func (u *H5ApiController) ManagementH5ApiAddBaseApi() {
	u.LogID = fmt.Sprintf("ManagementH5ApiAddBaseApi[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementH5ApiAddBaseApiReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementH5ApiAddBaseApiReq{}
		mreq.H5ApiId = req.Data.H5ApiId
		mreq.BaseApiIds = req.Data.BaseApiIds

		_, err := rpc_apis.Management.ManagementH5ApiAddBaseApi(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementH5ApiAddBaseApi"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementH5ApiAddBaseApiResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title h5 api get base api
// @Description h5 api 获取基础api 列表
// @Param   token    header   string true "token"
// @Param   uid    header   string true "uid"
// @Param   h5_api_id  query   int64 true "h5 api id"
// @Success 200 	{object} params.param_v1_0.ManagementH5ApiGetBaseApiListResp "响应信息"
// @Failure 500 	服务器错误
// @router /h5_api_base_api_list [get]
func (u *H5ApiController) ManagementH5ApiGetBaseApiList() {
	u.LogID = fmt.Sprintf("ManagementH5ApiGetBaseApiList[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementH5ApiGetBaseApiListReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementH5ApiGetBaseApiListReq{}
		mreq.H5ApiId = req.H5ApiId

		reply, err := rpc_apis.Management.ManagementH5ApiGetBaseApiList(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementH5ApiGetBaseApiList"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementH5ApiGetBaseApiListResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.BaseApiList = reply.BaseApiList
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}
