// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.

package controllers

import (
	"fmt"
	"os"
	"strconv"

	"git.smarteching.com/goffee/core"
	"git.smarteching.com/goffee/core/template/components"
)

// Show home page
func Themedemo(c *core.Context) *core.Response {

	// check if template engine is enabled
	TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
	if TemplateEnableStr == "" {
		TemplateEnableStr = "false"
	}
	TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)

	if TemplateEnable {

		// first, include all compoments
		type templateData struct {
			PageCard components.PageCard
		}

		// now fill data of the components
		tmplData := templateData{
			PageCard: components.PageCard{
				CardTitle: "Card title",
				CardBody:  "Loerm ipsum at deim",
			},
		}

		return c.Response.Template("custom_theme_base.html", tmplData)

	} else {

		message := "{\"message\": \"Error, template not enabled\"}"
		return c.Response.Json(message)

	}

}

// Show form element page
func Themeform(c *core.Context) *core.Response {

	// check if template engine is enabled
	TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
	if TemplateEnableStr == "" {
		TemplateEnableStr = "false"
	}
	TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)

	if TemplateEnable {

		// first, include all compoments
		type templateData struct {
			FormText       components.FormInput
			FormEmail      components.FormInput
			FormButton     components.FormButton
			FormSelectCity components.FormSelect
			FormTextarea   components.FormTextarea
			FormRadio      components.FormRadio
			FormCheckbox   components.FormCheckbox
		}

		// for select options
		var allOptions []components.FormSelectOption
		var option components.FormSelectOption
		option.Value = "ch"
		option.Caption = "China"
		allOptions = append(allOptions, option)
		option.Value = "ba"
		option.Caption = "Buenos Aires"
		allOptions = append(allOptions, option)
		option.Value = "fr"
		option.Caption = "France"
		selectedOption := option
		allOptions = append(allOptions, option)

		// for radio options
		var allOptionsr []components.FormRadioItem
		var optionr components.FormRadioItem
		optionr.ID = "citysch"
		optionr.Name = "citys"
		optionr.Value = "china"
		optionr.Label = "China"
		allOptionsr = append(allOptionsr, optionr)
		optionr.ID = "citysba"
		optionr.Name = "citys"
		optionr.Value = "buenosaires"
		optionr.Label = "Buenos Aires"
		//optionr.IsDisabled = true
		allOptionsr = append(allOptionsr, optionr)

		// for radio options
		var allOptionsc []components.FormCheckboxItem
		var optionc components.FormCheckboxItem
		optionc.ID = "citysch"
		optionc.Name = "citys"
		optionc.Value = "china"
		optionc.Label = "China"
		allOptionsc = append(allOptionsc, optionc)
		optionc.ID = "citysba"
		optionc.Name = "citys"
		optionc.Value = "buenosaires"
		optionc.Label = "Buenos Aires"
		allOptionsc = append(allOptionsc, optionc)
		optionc.ID = "sogas"
		optionc.Name = "sogas"
		optionc.Value = "Sogamoso"
		optionc.Label = "Sogamoso"
		//optionc.IsChecked = true
		allOptionsc = append(allOptionsc, optionc)

		// now fill data of the components
		tmplData := templateData{
			FormText: components.FormInput{
				ID:          "text",
				Label:       "Name",
				Type:        "text",
				Hint:        "This is sample hint",
				Placeholder: "Enter your name",
			},
			FormEmail: components.FormInput{
				ID:          "email",
				Label:       "Email",
				Type:        "email",
				IsRequired:  true,
				Placeholder: "Enter your email address",
			},
			FormButton: components.FormButton{
				Text:      "Login",
				IsSubmit:  true,
				TypeClass: "primary",
			},
			FormSelectCity: components.FormSelect{
				ID:             "city",
				Label:          "Select city",
				AllOptions:     allOptions,
				SelectedOption: selectedOption,
			},
			FormTextarea: components.FormTextarea{
				ID:    "text",
				Label: "Example textarea",
			},
			FormRadio: components.FormRadio{
				Label:     "Radio buttons",
				AllRadios: allOptionsr,
			},
			FormCheckbox: components.FormCheckbox{
				Label:       "Checkbox options",
				AllCheckbox: allOptionsc,
			},
		}
		return c.Response.Template("custom_theme_formpage.html", tmplData)

	} else {

		message := "{\"message\": \"Error, template not enabled\"}"
		return c.Response.Json(message)

	}

}

func ThemeElements(c *core.Context) *core.Response {
	// check if template engine is enabled
	TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
	if TemplateEnableStr == "" {
		TemplateEnableStr = "false"
	}
	TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)

	if TemplateEnable {
		type templateData struct {
			Buttons   []components.FormButton
			Hrefs     []components.ContentHref
			Badges    []components.ContentBadge
			Dropdowns []components.ContentDropdown
			Lists     []components.ContentList
			Menus     []components.PageNav
		}
		buttons := []components.FormButton{
			{
				Text:      "primary",
				TypeClass: "primary",
			},
			{
				Text:      "secondary",
				TypeClass: "secondary",
			},
			{
				Text:      "success",
				TypeClass: "success",
			},
			{
				Text:      "danger",
				TypeClass: "danger",
			},
			{
				Text:      "warning",
				TypeClass: "warning",
			},
			{
				Text:      "info",
				TypeClass: "info",
			},
			{
				Text:      "light",
				TypeClass: "light",
			},
			{
				Text:      "dark",
				TypeClass: "dark",
			},
			{
				Text:      "link",
				TypeClass: "link",
			},
			{
				Text:       "disabled",
				TypeClass:  "primary",
				IsDisabled: true,
			},
			{
				Text:      "outline-primary",
				TypeClass: "outline-primary",
			},
			{
				Text:      "outline-secondary",
				TypeClass: "outline-secondary",
			},
			{
				Text:      "outline-success",
				TypeClass: "outline-success",
			},
			{
				Text:      "outline-danger",
				TypeClass: "outline-danger",
			},
			{
				Text:      "outline-warning",
				TypeClass: "outline-warning",
			},
			{
				Text:      "outline-info",
				TypeClass: "outline-info",
			},
			{
				Text:      "outline-light",
				TypeClass: "outline-light",
			},
			{
				Text:      "outline-dark",
				TypeClass: "outline-dark",
			},
		}
		hrefs := []components.ContentHref{
			{
				Text:     "href",
				Link:     "#",
				IsButton: false,
			},
			{
				Text:      "link",
				Link:      "#",
				IsButton:  false,
				TypeClass: "link",
			},
			{
				Text:      "button",
				Link:      "#",
				IsButton:  true,
				TypeClass: "primary",
			},
			{
				Text:       "href disabled",
				Link:       "#",
				IsButton:   false,
				IsDisabled: true,
			},
			{
				Text:       "link disabled",
				Link:       "#",
				TypeClass:  "link",
				IsDisabled: true,
			},
			{
				Text:       "button disabled",
				Link:       "#",
				IsButton:   true,
				TypeClass:  "primary",
				IsDisabled: true,
			},
		}
		badges := []components.ContentBadge{
			{
				Text:      "primary",
				TypeClass: "primary",
			},
			{
				Text:      "secondary",
				TypeClass: "secondary",
			},
			{
				Text:      "success",
				TypeClass: "success",
			},
			{
				Text:      "danger",
				TypeClass: "danger",
			},
			{
				Text:      "warning",
				TypeClass: "warning",
			},
			{
				Text:      "info",
				TypeClass: "info",
			},
			{
				Text:      "light",
				TypeClass: "light",
			},
			{
				Text:      "dark",
				TypeClass: "dark",
			},
			{
				Text:      "outline",
				TypeClass: "primary",
				IsOutline: true,
			},
			{
				Text:      "outline",
				TypeClass: "success",
				IsOutline: true,
			},
			{
				Text:      "outline",
				TypeClass: "danger",
				IsOutline: true,
			},
			{
				Text:      "outline",
				TypeClass: "warning",
				IsOutline: true,
			},
		}
		dropdowns := []components.ContentDropdown{
			// dropdown
			{
				Label: "dropdown",
				Items: []components.ContentDropdownItem{
					{
						Text: "item ",
						Link: "#",
					},
					{
						Text:       "item disabled",
						Link:       "#",
						IsDisabled: true,
					},
				},
			},
			// dropdown
			{
				Label:     "primary",
				TypeClass: "primary",
				Items: []components.ContentDropdownItem{
					{
						Text: "item ",
						Link: "#",
					},
					{
						Text:     "item ",
						Link:     "#",
						IsActive: true,
					},
					{
						Text:       "item disabled",
						Link:       "#",
						IsDisabled: true,
					},
				},
			},
			// dropdown
			{
				Label:     "outline",
				TypeClass: "outline-primary",
				Items: []components.ContentDropdownItem{
					{
						Text: "item ",
						Link: "#",
					},
				},
			},
			// dropdown
			{
				Label:      "disabled",
				TypeClass:  "primary",
				IsDisabled: true,
				// items
			},
		}
		list := []components.ContentList{
			// basic list
			{
				Items: []components.ContentListItem{
					{
						Text: "item 1",
					},
					{
						Text:       "item 2",
						EndElement: "end text",
					},
					{
						Text:       "item disabled",
						IsDisabled: true,
					},
				},
			},
			// description list
			{
				Items: []components.ContentListItem{
					{
						Text:        "item 1",
						Description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ",
					},
					{
						Text:        "item 2",
						Description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
					},
					{
						Text:        "item disabled",
						Description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
						IsDisabled:  true,
					},
				},
			},
			// list with class
			{
				Items: []components.ContentListItem{
					{
						Text:      "class primary",
						TypeClass: "primary",
					},
					{
						Text:      "class success",
						TypeClass: "success",
					},
					{
						Text:      "class danger",
						TypeClass: "danger",
					},
				},
			},
		}
		menus := []components.PageNav{
			// nav
			{
				NavClass: "nav-pills",
				NavItems: []components.PageNavItem{
					{
						Text:     "item active",
						Link:     "#",
						IsActive: true,
					},
					{
						Text:     "item",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:     "item",
						Link:     "#",
						IsActive: false,
						ChildItems: []components.PageNavItem{
							{
								Text: "item ",
								Link: "#",
							},
							{
								Text:       "item disabled",
								Link:       "#",
								IsDisabled: true,
							},
						},
					},
					{
						Text:       "item disabled",
						Link:       "#",
						IsDisabled: true,
					},
				},
			},
			// nav
			{
				NavClass: "",
				NavItems: []components.PageNavItem{
					{
						Text:     "item active",
						Link:     "#",
						IsActive: true,
					},
					{
						Text:     "item",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:     "item",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:       "item disabled",
						Link:       "#",
						IsDisabled: true,
					},
				},
			},
			// nav underline
			{
				NavClass: "nav-underline",
				NavItems: []components.PageNavItem{
					{
						Text:     "item active",
						Link:     "#",
						IsActive: true,
					},
					{
						Text:     "item",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:     "item",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:       "item disabled",
						Link:       "#",
						IsDisabled: true,
					},
				},
			},
			// nav tabs
			{
				NavClass: "",
				IsTab:    true,
				NavItems: []components.PageNavItem{
					{
						Text:     "tab active",
						Link:     "#",
						IsActive: true,
					},
					{
						Text:     "tab",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:     "tab",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:       "tab disabled",
						Link:       "#",
						IsDisabled: true,
					},
				},
			},
			// nav vertical
			{
				NavClass:   "",
				IsVertical: true,
				NavItems: []components.PageNavItem{
					{
						Text:     "item active",
						Link:     "#",
						IsActive: true,
					},
					{
						Text:     "item",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:     "item",
						Link:     "#",
						IsActive: false,
					},
					{
						Text:       "item disabled",
						Link:       "#",
						IsDisabled: true,
					},
				},
			},
		}

		tmplData := templateData{
			Buttons:   buttons,
			Hrefs:     hrefs,
			Badges:    badges,
			Dropdowns: dropdowns,
			Lists:     list,
			Menus:     menus,
		}
		return c.Response.Template("custom_theme_elements.html", tmplData)

	} else {

		message := "{\"message\": \"Error, template not enabled\"}"
		return c.Response.Json(message)
	}
}

// Show form element page
func Themecontent(c *core.Context) *core.Response {

	// check if template engine is enabled
	TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
	if TemplateEnableStr == "" {
		TemplateEnableStr = "false"
	}
	TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)

	if TemplateEnable {

		// first, include all compoments
		type templateData struct {
			ContentTable components.ContentTable
		}

		// TABLES
		// for th head
		var allTh []components.ContentTableTH
		var th components.ContentTableTH
		th.Value = "Column heading 1"
		allTh = append(allTh, th)
		th.Value = "Column heading 2"
		allTh = append(allTh, th)
		th.ID = "ba"
		th.Value = "Column heading 3"
		allTh = append(allTh, th)
		th.Value = "Column badge"
		th.ValueType = "badge" // column type badge
		allTh = append(allTh, th)
		th.Value = "Column action"
		th.ValueType = "href" // column type href
		allTh = append(allTh, th)

		// for td items
		var allTd [][]components.ContentTableTD
		//var vals []components.ContentTableTD
		// rows
		for i := 1; i <= 10; i++ {
			vals := make([]components.ContentTableTD, len(allTh))
			for b := 0; b < len(allTh)-2; b++ {
				vals[b].Value = fmt.Sprintf("%s%d%d", "TD data: ", i, b)
				vals[b].ID = fmt.Sprintf("%s%d%d", "idtd_", i, b)
			}
			// column badge
			vals[len(allTh)-2].Value = components.ContentBadge{
				Text:      "success",
				TypeClass: "success",
			}
			// last column href
			vals[len(allTh)-1].Value = components.ContentHref{
				Text: "edit",
				Link: "#",
			}
			allTd = append(allTd, vals)
		}

		// now fill data of the components
		tmplData := templateData{
			ContentTable: components.ContentTable{
				ID:    "table_demo",
				AllTH: allTh,
				AllTD: allTd,
			},
		}
		return c.Response.Template("custom_theme_contentpage.html", tmplData)

	} else {

		message := "{\"message\": \"Error, template not enabled\"}"
		return c.Response.Json(message)

	}

}
