package dev_handlers

import (
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"strings"
	"time"

	kelainanglobal "cpone/views/dev/mcu/kelainan_global"
	mcupeserta "cpone/views/dev/mcu/peserta"
	"cpone/views/dev/piechart"
	"cpone/views/dev/xsample"

	services "cpone/services/dev"

	"github.com/a-h/templ"
	"github.com/labstack/echo/v4"
)

type PiechartService interface {
	GetPieChart(string, int) (string, services.Piechart, error)
	GetBarChart(code string, id int) (string, services.Barchart, error)
}

func NewPiechartHandler(us PiechartService) *PiechartHandler {
	return &PiechartHandler{
		PiechartService: us,
	}
}

type PiechartHandler struct {
	PiechartService PiechartService
}

func (uh *PiechartHandler) Hello(c echo.Context) error {
	helo := xsample.ShowHelo("Hello World", xsample.HelloWorld("Hello World"), xsample.CssHelo(), xsample.JsHelo())
	return uh.View(c, helo)
}

// example pie chart
func (uh *PiechartHandler) ShowPieChart(c echo.Context) error {
	// helo := piechart.MainPieChart("main_001")
	var code = "mcu006"
	title, udata, err := uh.PiechartService.GetPieChart(code, 1)
	if err != nil {
		// fmt.Println(err)
		return err
	}

	jsonData, err := json.MarshalIndent(udata, "", "    ")
	if err != nil {
		fmt.Println("Error:", err)
		return err
	}

	// randomID := md5.Sum([]byte(title + time.Now().String()))
	seed := title + time.Now().String()
	hash := md5.Sum([]byte(seed))
	randomID := hex.EncodeToString(hash[:])
	randomID = strings.ReplaceAll(randomID, "-", "")

	// randomID := hex.EncodeToString(md5.Sum([]byte(title + time.Now().String())))
	helo := piechart.ShowPieChart("Chart", piechart.MainPieChart(randomID, string(jsonData)), piechart.CssPieChart(), piechart.JsPieChart())
	return uh.View(c, helo)
}
func (uh *PiechartHandler) ShowBarChart(c echo.Context) error {
	// helo := piechart.MainPieChart("main_001")
	var code = "mcu004"
	title, udata, err := uh.PiechartService.GetBarChart(code, 1)
	if err != nil {
		// fmt.Println(err)
		return err
	}
	var code2 = "mcu005"
	title2, udata2, err := uh.PiechartService.GetBarChart(code2, 1)
	if err != nil {
		// fmt.Println(err)
		return err
	}

	jsonData, err := json.MarshalIndent(udata, "", "    ")
	if err != nil {
		fmt.Println("Error:", err)
		return err
	}
	jsonData2, err := json.MarshalIndent(udata2, "", "    ")
	if err != nil {
		fmt.Println("Error:", err)
		return err
	}

	// randomID := md5.Sum([]byte(title + time.Now().String()))
	seed := title + time.Now().String()
	hash := md5.Sum([]byte(seed))
	randomID := hex.EncodeToString(hash[:])
	randomID = strings.ReplaceAll(randomID, "-", "")

	seed2 := title2 + time.Now().String()
	hash2 := md5.Sum([]byte(seed2))
	randomID2 := hex.EncodeToString(hash2[:])
	randomID2 = strings.ReplaceAll(randomID2, "-", "")

	// randomID := hex.EncodeToString(md5.Sum([]byte(title + time.Now().String())))
	helo := kelainanglobal.ShowKelainanGlobal("Kelainan global", kelainanglobal.MainKelainanGlobal(randomID, string(jsonData), randomID2, string(jsonData2)), kelainanglobal.CssKelainanGlobal(), kelainanglobal.JsKelainanGlobal())
	return uh.View(c, helo)
}

// mcu peserta
func (uh *PiechartHandler) ShowMcuPeserta(c echo.Context) error {

	// 1
	var code001 = "mcu001"
	title001, udata001, err001 := uh.PiechartService.GetPieChart(code001, 1)
	if err001 != nil {
		return err001
	}

	jsonData001, err001 := json.MarshalIndent(udata001, "", "    ")
	if err001 != nil {
		fmt.Println("Error:", err001)
		return err001
	}

	seed001 := title001 + time.Now().String()
	hash001 := md5.Sum([]byte(seed001))
	randomID001 := hex.EncodeToString(hash001[:])
	randomID001 = strings.ReplaceAll(randomID001, "-", "")

	// 2
	var code002 = "mcu002"
	title002, udata002, err002 := uh.PiechartService.GetPieChart(code002, 1)
	if err002 != nil {
		return err002
	}

	jsonData002, err002 := json.MarshalIndent(udata002, "", "    ")
	if err002 != nil {
		fmt.Println("Error:", err002)
		return err002
	}

	seed002 := title002 + time.Now().String()
	hash002 := md5.Sum([]byte(seed002))
	randomID002 := hex.EncodeToString(hash002[:])
	randomID002 = strings.ReplaceAll(randomID002, "-", "")

	// 3
	var code003 = "mcu003"
	title003, udata003, err003 := uh.PiechartService.GetPieChart(code003, 1)
	if err003 != nil {
		return err003
	}

	jsonData003, err003 := json.MarshalIndent(udata003, "", "    ")
	if err003 != nil {
		fmt.Println("Error:", err003)
		return err003
	}

	seed003 := title003 + time.Now().String()
	hash003 := md5.Sum([]byte(seed003))
	randomID003 := hex.EncodeToString(hash003[:])
	randomID003 = strings.ReplaceAll(randomID003, "-", "")

	helo := mcupeserta.ShowMcuPeserta("Peserta", mcupeserta.MainMcuPeserta(
		randomID001, string(jsonData001),
		randomID002, string(jsonData002),
		randomID003, string(jsonData003),
	),
		mcupeserta.CssMcuPeserta(),
		mcupeserta.JsMcuPeserta(),
	)

	return uh.View(c, helo)
}

func (uh *PiechartHandler) View(c echo.Context, cmp templ.Component) error {
	c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)

	return cmp.Render(c.Request().Context(), c.Response().Writer)
}
