package utils

import (
	"fmt"
	"primesys_email/src/logger"
	"strings"
	"time"

	"github.com/xuri/excelize/v2"
)

func ThinBorders(color string) []excelize.Border {
	return []excelize.Border{
		{Type: "top", Color: color, Style: 1},
		{Type: "bottom", Color: color, Style: 1},
		{Type: "left", Color: color, Style: 1},
		{Type: "right", Color: color, Style: 1},
	}
}

func DefaultString(value *string) string {
	if value == nil || strings.TrimSpace(*value) == "" {
		return "-"
	}
	return *value
}

func DefaultFloat(value *float64) string {
	if value == nil {
		return "-"
	}
	return fmt.Sprintf("%.2f", *value)
}

func DefaultTime(value *int64) string {
	if value == nil || *value < 0 {
		return ""
	}

	loc, err := time.LoadLocation("Asia/Kolkata")

	if err != nil {
		logger.Error("Error in time location")
		return ""
	}

	return time.Unix(*value, 0).In(loc).Local().Format("15:04")
}

func DefaultInt(value *int32) string {
	if value == nil {
		return "<->"
	}
	return fmt.Sprint(*value)
}

func TitleStyle(f *excelize.File) int {
	return CreateStyle(f, map[string]any{
		"font":      &excelize.Font{Family: "Arial", Size: 13, Bold: true, Color: "1A1A1A"},
		"alignment": &excelize.Alignment{Horizontal: "center", Vertical: "center"},
	})
}

func DateStyle(f *excelize.File) int {
	return CreateStyle(f, map[string]any{
		"font":      &excelize.Font{Family: "Arial", Size: 13, Bold: true, Color: "5C70C4"},
		"alignment": &excelize.Alignment{Horizontal: "center", Vertical: "center"},
	})
}

func MetaLabelStyle(f *excelize.File) int {
	return CreateStyle(f, map[string]any{
		"font": &excelize.Font{Family: "Arial", Size: 8, Color: "555555"},
	})
}

func MetaValueStyle(f *excelize.File) int {
	return CreateStyle(f, map[string]any{
		"font": &excelize.Font{Family: "Arial", Size: 8, Bold: true, Color: "1A1A1A"},
	})
}

func HeaderStyle(f *excelize.File) int {
	return CreateStyle(f, map[string]any{
		"font": &excelize.Font{Family: "Arial", Size: 8, Bold: true, Color: "FFFFFF"},
		"fill": excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{"015958"}},
		"alignment": &excelize.Alignment{
			Horizontal: "center", Vertical: "center", WrapText: true,
		},
		"border": []excelize.Border{
			{Type: "top", Color: "014545", Style: 1},
			{Type: "bottom", Color: "014545", Style: 1},
			{Type: "left", Color: "014545", Style: 1},
			{Type: "right", Color: "014545", Style: 1},
		},
	})
}

func DataStyle(f *excelize.File, even bool) int {
	bg := "FFFFFF"
	if even {
		bg = "F4FAFA"
	}
	return CreateStyle(f, map[string]any{
		"font": &excelize.Font{Family: "Arial", Size: 8, Color: "222222"},
		"fill": excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{bg}},
		"alignment": &excelize.Alignment{
			Horizontal: "center", Vertical: "center", WrapText: true,
		},
		"border": []excelize.Border{
			{Type: "top", Color: "DDDDEE", Style: 1},
			{Type: "bottom", Color: "DDDDEE", Style: 1},
			{Type: "left", Color: "DDDDEE", Style: 1},
			{Type: "right", Color: "DDDDEE", Style: 1},
		},
	})
}

func NameStyle(f *excelize.File) int {
	return CreateStyle(f, map[string]any{
		"font": &excelize.Font{Family: "Arial", Size: 8, Bold: true, Color: "015958"},
		"alignment": &excelize.Alignment{
			Horizontal: "left", Vertical: "center",
		},
	})
}

func RemarkStyles(f *excelize.File) []int {
	colors := []struct {
		bg string
		fg string
	}{
		{"E1F5EE", "085041"}, // Done
		{"FCEBEB", "791F1F"}, // Off
		{"FAEEDA", "633806"}, // Not Done
	}

	styles := make([]int, 0, len(colors))
	for _, c := range colors {
		styles = append(styles, CreateStyle(f, map[string]any{
			"font": &excelize.Font{
				Family: "Arial", Size: 8, Bold: true, Color: c.fg,
			},
			"fill": excelize.Fill{
				Type: "pattern", Pattern: 1, Color: []string{c.bg},
			},
			"alignment": &excelize.Alignment{
				Horizontal: "center", Vertical: "center", WrapText: true,
			},
			"border": []excelize.Border{
				{Type: "top", Color: "DDDDEE", Style: 1},
				{Type: "bottom", Color: "DDDDEE", Style: 1},
				{Type: "left", Color: "DDDDEE", Style: 1},
				{Type: "right", Color: "DDDDEE", Style: 1},
			},
		}))
	}
	return styles
}

func RemarkStyleFor(
	f *excelize.File,
	remark string,
	styles []int,
) int {
	switch {
	case strings.Contains(remark, "All work done"):
		return styles[0]
	case strings.Contains(remark, "Device was Off"):
		return styles[1]
	default:
		return styles[2]
	}
}

func CreateStyle(f *excelize.File, style map[string]any) int {
	id, err := f.NewStyle(&excelize.Style{
		Font:      style["font"].(*excelize.Font),
		Alignment: OptionalAlignment(style["alignment"]),
		Fill:      OptionalFill(style["fill"]),
		Border:    OptionalBorders(style["border"]),
	})
	if err != nil {
		return 0
	}
	return id
}

func OptionalAlignment(v any) *excelize.Alignment {
	if v == nil {
		return nil
	}
	return v.(*excelize.Alignment)
}

func OptionalFill(v any) excelize.Fill {
	if v == nil {
		return excelize.Fill{}
	}
	return v.(excelize.Fill)
}

func OptionalBorders(v any) []excelize.Border {
	if v == nil {
		return nil
	}
	return v.([]excelize.Border)
}

func DefaultValue(value string) string {
	if value == "" {
		return "-"
	}
	return value
}

func DerefFloat64(value *float64) float64 {
	if value == nil {
		return 0
	}
	return *value
}
