package controllers_join

import (
	"encoding/json"
	"kkscrap-go/locals"
	models_join "kkscrap-go/models/join"
	"net/http"
	"strings"

	"github.com/dabory/abango-rest"
	e "github.com/dabory/abango-rest/etc"
	"github.com/labstack/echo"
)

type SimpleJoinPageReq struct {
	QueryVars locals.QueryVars
	PageVars  locals.PageVars
	Page      []models_join.CartItemJoin
}

func SimpleJoinPage(c echo.Context) error {

	v := c.Get("receiver").(SimpleJoinPageReq)
	y := c.Get("abango").(abango.Controller)

	vCom := &struct {
		models_join.SimplePageRet
	}{}
	vCom.QueryVars = v.QueryVars
	vCom.PageVars = v.PageVars

	code := v.QueryVars.QueryName

	if strings.Contains(code, "select-option/") {
		vRet := &struct {
			models_join.SimpleSelectOptionPageRet
		}{}

		vRet.QueryVars = vCom.QueryVars
		vRet.PageVars = vCom.PageVars
		if err := vRet.GetPage(&y); err == nil {
			ret, _ := json.Marshal(vRet)
			return c.JSONBlob(http.StatusOK, ret)
		} else {
			return c.String(607, e.LogStr("453dfaw3rad", "Issue in QueryName: "+v.QueryVars.QueryName))
		}
	}

	var ms locals.MapStore
	if code == "cart-item" {
		vRet := &struct {
			models_join.CartItemPageRet
		}{}

		if err := vCom.SimpleJoinPage(&y, &ms); err != nil {
			return c.String(607, err.Error())
		}

		vRet.Page = ms.Get("page").(models_join.CartItemPage).Page
		vRet.QueryVars = vCom.QueryVars
		vRet.PageVars = vCom.PageVars

		ret, _ := json.Marshal(vRet)
		return c.JSONBlob(http.StatusOK, ret)

	} else if code == "sorder-item-total" {
		vRet := &struct {
			models_join.SorderItemTotalPageRet
		}{}

		if err := vCom.SimpleJoinPage(&y, &ms); err != nil {
			return c.String(607, err.Error())
		}

		vRet.Page = ms.Get("page").(models_join.SorderItemTotalPage).Page
		vRet.QueryVars = vCom.QueryVars
		vRet.PageVars = vCom.PageVars

		ret, _ := json.Marshal(vRet)
		return c.JSONBlob(http.StatusOK, ret)

	} else if code == "post-member-user" {
		vRet := &struct {
			models_join.PostMemberUserPageRet
		}{}

		if err := vCom.SimpleJoinPage(&y, &ms); err != nil {
			return c.String(607, err.Error())
		}

		vRet.Page = ms.Get("page").(models_join.PostMemberUserPage).Page
		vRet.QueryVars = vCom.QueryVars
		vRet.PageVars = vCom.PageVars

		ret, _ := json.Marshal(vRet)
		return c.JSONBlob(http.StatusOK, ret)

		///// Crypto ////
	} else if code == "my-net-group-wallet" {
		vRet := &struct {
			models_join.MyNetGroupWalletPageRet
		}{}

		if err := vCom.SimpleJoinPage(&y, &ms); err != nil {
			return c.String(607, err.Error())
		}

		vRet.Page = ms.Get("page").(models_join.MyNetGroupWalletPage).Page
		vRet.QueryVars = vCom.QueryVars
		vRet.PageVars = vCom.PageVars

		ret, _ := json.Marshal(vRet)
		return c.JSONBlob(http.StatusOK, ret)

	} else if code == "my-wallet-token" {
		vRet := &struct {
			models_join.MyWalletTokenPageRet
		}{}

		if err := vCom.SimpleJoinPage(&y, &ms); err != nil {
			return c.String(607, err.Error())
		}

		vRet.Page = ms.Get("page").(models_join.MyWalletTokenPage).Page
		vRet.QueryVars = vCom.QueryVars
		vRet.PageVars = vCom.PageVars

		ret, _ := json.Marshal(vRet)
		return c.JSONBlob(http.StatusOK, ret)

	} else if code == "my-wallet-token-not" {
		vRet := &struct {
			models_join.MyWalletTokenNotPageRet
		}{}

		if err := vCom.SimpleJoinPage(&y, &ms); err != nil {
			return c.String(607, err.Error())
		}

		vRet.Page = ms.Get("page").(models_join.MyWalletTokenNotPage).Page
		vRet.QueryVars = vCom.QueryVars
		vRet.PageVars = vCom.PageVars

		ret, _ := json.Marshal(vRet)
		return c.JSONBlob(http.StatusOK, ret)

	} else {
		return c.String(709, e.LogStr("453dfaw3rad", "QueryName Not Found in JoinPage Controller for '"+v.QueryVars.QueryName+"'"))
	}

}
