package public_handlers

import (
	"cpone/models"
	"cpone/utils"
	surveymcu "cpone/views/public/surveymcu"
	public_toast "cpone/views/public/toast"

	"github.com/a-h/templ"
	"github.com/labstack/echo/v4"
	"go.uber.org/zap"
)

type SurveyMcuService interface {
	GetSurveyMcuByNoreg(nomorRegistrasi string) ([]models.SurveyMcu, error)
}

func NewSurveyMcuHandler(us SurveyMcuService) *SurveyMcuHandler {
	return &SurveyMcuHandler{
		SurveyMcuService: us,
	}
}

type SurveyMcuHandler struct {
	SurveyMcuService SurveyMcuService
}

func (lh *SurveyMcuHandler) HandlerShowSurveyMcu(c echo.Context) error {

	var flagxError bool = true
	var msgx string = ""

	nomorRegistrasi := c.Param("nomorRegistrasi")
	// nomorRegistrasi := "1"
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	dataSurveyMcu, err := lh.SurveyMcuService.GetSurveyMcuByNoreg(nomorRegistrasi)

	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
			zap.Any("prm", nomorRegistrasi),
		)
		// return err
		if flagxError {
			msgx = "Error Get Survey MCU By No Reg"
			si := public_toast.PublicToastV1KartuKontrolShow("Error", msgx, "danger")
			return utils.View(c, si)
		}
	}

	flagxError = false
	logger.Info("SHOW DATA SURVEY MCU handler",
		zap.Any("return", dataSurveyMcu),
		zap.Any("prm", nomorRegistrasi),
	)

	stringUrl := "surveymcuproses/" + nomorRegistrasi
	var hxInclude string = ""
	var idClearMsg []models.IdClearErrorMessage

	if len(dataSurveyMcu) > 0 {
		hxInclude = utils.ConcatHxIncludeSurveyMcu(dataSurveyMcu)
	}

	si := surveymcu.ShowSurveyMcu("Survey Mcu",
		surveymcu.MainContentSurveyMcuLayout(dataSurveyMcu, stringUrl, surveymcu.ErrorMessageSurveyMcu("", "", idClearMsg, "alertSaranMcu", public_toast.PublicToastV1KartuKontrolHide()), hxInclude), surveymcu.CssSurveyMcu(), surveymcu.JsSurveyMcu())

	return utils.View(c, si)
}

func (lh *SurveyMcuHandler) HandlerProcessSurveyMcu(c echo.Context) error {

	var flagxError bool = true
	var msgx string = ""

	nomorRegistrasi := c.Param("nomorRegistrasi")
	stringUrlBackToKartuKontrol := "/kartukontrol/" + nomorRegistrasi
	// nomorRegistrasi := "1"
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	dataSurveyMcu, err := lh.SurveyMcuService.GetSurveyMcuByNoreg(nomorRegistrasi)

	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
			zap.Any("prm", nomorRegistrasi),
		)
		// return err
		if flagxError {
			msgx = "Error Get Survey MCU By No Reg"
			si := public_toast.PublicToastV1KartuKontrolShow("Error", msgx, "danger")
			return utils.View(c, si)
		}
	}

	logger.Info("SHOW DATA SURVEY MCU handler",
		zap.Any("return", dataSurveyMcu),
		zap.Any("prm", nomorRegistrasi),
	)

	// form := c.Request().PostForm

	// stringUrl := "surveymcuproses/" + nomorRegistrasi

	logger.Info("CEK POST handler",
		zap.Any("c.FormValue Saran", c.FormValue("saranmcu-11")),
	)

	// proses validasi

	var si templ.Component
	var idFormKosong string = ""
	var stringMessage string = ""
	var isFormEmpty bool = false
	var idClearMsg []models.IdClearErrorMessage
	var idSaranMcuAlert string = ""
	// var idSaranMcuClear string = ""

	hasError := false

	if len(dataSurveyMcu) > 0 {
		for _, v := range dataSurveyMcu {
			// radio button
			if len(v.DataListItemSurveyMcu) > 0 {
				for _, x := range v.DataListItemSurveyMcu {

					if len(c.FormValue("radio-"+x.IDItemSurvey)) > 0 {
						idClearMsg = append(idClearMsg, models.IdClearErrorMessage{IdItem: x.IDItemSurvey})
					}

					if "default-"+x.NameRadioButton == c.FormValue("default-"+x.NameRadioButton) && len(c.FormValue("radio-"+x.IDItemSurvey)) == 0 {
						stringMessage = "Pertanyaan ke-" + x.IDItemSurvey + " wajib diisi"
						idFormKosong = utils.GetIdSurveyMcu(x.IDItemSurvey)
						isFormEmpty = true
						hasError = true
						flagxError = true
						break
					}
				}
			}

			if hasError {
				break
			}
		}
	}

	// saran mcu
	if !hasError && len(dataSurveyMcu) > 0 {
		for _, v := range dataSurveyMcu {
			if len(v.DataListItemSurveyMcuSaran) > 0 {
				for _, x := range v.DataListItemSurveyMcuSaran {
					if len(c.FormValue("saranmcu-"+x.IDItemSurveyMcuSaran)) > 0 {
						idClearMsg = append(idClearMsg, models.IdClearErrorMessage{IdItem: x.IDItemSurveyMcuSaran})
					}

					if "default-"+x.NameSurveyMcuSaran == c.FormValue("default-"+x.NameSurveyMcuSaran) && len(c.FormValue("saranmcu-"+x.IDItemSurveyMcuSaran)) == 0 {
						stringMessage = "Saran Wajib Diisi"
						idFormKosong = utils.GetIdSurveyMcu(x.IDItemSurveyMcuSaran)
						isFormEmpty = true
						hasError = true
						break
					}
				}
			}
			if hasError {
				break
			}
		}
	}

	if isFormEmpty {
		// if flagxError {
		// 	msgx = "Error Get Survey MCU By No Reg"
		// 	si := public_toast.PublicToastV1KartuKontrolShow("Error", msgx, "danger")
		// 	return utils.View(c, si)
		// }
		si = surveymcu.ErrorMessageSurveyMcu(stringMessage, idFormKosong, idClearMsg, idSaranMcuAlert, public_toast.PublicToastV1KartuKontrolShow("Warning", stringMessage, "warning"))
	} else {
		flagxError = false
		si = surveymcu.ErrorMessageSurveyMcu("", "", idClearMsg, "", public_toast.PublicToastV1KartuKontrolHide())
		logger.Info("redirect",
			zap.Any("redirect", stringUrlBackToKartuKontrol),
		)

		// redirect ke kartu kontrol
		c.Response().Header().Set("HX-Redirect", stringUrlBackToKartuKontrol)
	}

	return utils.View(c, si)
}
