package dev_services

import (
	"fmt"

	"cpone/db"
)

func NewServicesPiechart(u Piechart, b Barchart, uStore db.AppStore, bStore db.AppStore) *ServicesPiechart {

	return &ServicesPiechart{
		Piechart:      u,
		Barchart:      b,
		BarChartStore: bStore,
		PieChartStore: uStore,
	}
}

// type Option struct {
// 	Title struct {
// 		Text string
// 		Left string
// 	}
// 	Tooltip struct {
// 		Trigger string
// 	}
// 	Legend struct {
// 		Top    string
// 		Left   string
// 		Orient string
// 	}
// 	Series []Series
// }

type SeriesData struct {
	Value int    `json:"value"`
	Name  string `json:"name"`
}

type Series struct {
	Label struct {
		Position  string `json:"position"`
		Formatter string `json:"formatter"`
	} `json:"label"`
	Name      string   `json:"name"`
	Type      string   `json:"type"`
	Radius    []string `json:"radius"`
	ItemStyle struct {
		BorderRadius int    `json:"borderRadius"`
		BorderColor  string `json:"borderColor"`
		BorderWidth  int    `json:"borderWidth"`
	} `json:"itemStyle"`
	Data     []SeriesData `json:"data"`
	Emphasis struct {
		ItemStyle struct {
			ShadowBlur    int    `json:"shadowBlur"`
			ShadowOffsetX int    `json:"shadowOffsetX"`
			ShadowColor   string `json:"shadowColor"`
		} `json:"itemStyle"`
	} `json:"emphasis"`
}

type Barchart struct {
	Title struct {
		Text string `json:"text"`
	} `json:"title"`
	Dataset struct {
		Source [][]interface{} `json:"source"`
	} `json:"dataset"`
	Grid struct {
		ContainLabel bool `json:"containLabel"`
	} `json:"grid"`
	XAxis struct {
		Name string `json:"name"`
	} `json:"xAxis"`
	YAxis struct {
		Type string `json:"type"`
	} `json:"yAxis"`
	VisualMap struct {
		Orient    string `json:"orient"`
		Left      string `json:"left"`
		Min       int    `json:"min"`
		Max       int    `json:"max"`
		Show      bool   `json:"show"`
		Dimension int    `json:"dimension"`
		InRange   struct {
			Color []string `json:"color"`
		} `json:"inRange"`
	} `json:"visualMap"`
	Series []struct {
		Label struct {
			Position  string `json:"position"`
			Show      bool   `json:"show"`
			Formatter string `json:"formatter"`
		} `json:"label"`
		Type   string `json:"type"`
		Encode struct {
			X string `json:"x"`
			Y string `json:"y"`
		} `json:"encode"`
	} `json:"series"`
}

type Piechart struct {
	// ID          int       `json:"id"`
	// Xsamplename string    `json:"Xsamplename"`
	// Email       string    `json:"email"`
	// CreatedAt   time.Time `json:"created_at,omitempty"`

	Title struct {
		Text    string `json:"text"`
		SubText string `json:"subtext"`
		Left    string `json:"left"`
	} `json:"title"`
	Tooltip struct {
		Trigger string `json:"trigger"`
	} `json:"tooltip"`
	Legend struct {
		Top    string `json:"top"`
		Left   string `json:"left"`
		Orient string `json:"orient"`
	} `json:"legend"`
	Series []Series `json:"series"`
}

type ServicesPiechart struct {
	Piechart      Piechart
	Barchart      Barchart
	BarChartStore db.AppStore
	PieChartStore db.AppStore
}

// func (su *ServicesPiechart) GetAllXsamples() ([]Piechart, error) {
// 	query := `SELECT id, Xsamplename, email, created_at FROM Xsamples ORDER BY created_at DESC`

// 	rows, err := su.PieChartStore.Db.Query(query)
// 	if err != nil {
// 		return []Piechart{}, err
// 	}
// 	// We close the resource
// 	defer rows.Close()

// 	Xsamples := []Piechart{}
// 	for rows.Next() {
// 		rows.Scan(
// 			&su.Piechart.ID,
// 			&su.Piechart.Xsamplename,
// 			&su.Piechart.Email,
// 			&su.Piechart.CreatedAt,
// 		)

// 		Xsamples = append(Xsamples, su.Piechart)
// 	}

// 	return Xsamples, nil
// }

// func (su *ServicesPiechart) GetXsampleById(id int) (Piechart, error) {

// 	query := `SELECT id, Xsamplename, email, created_at FROM Xsamples
// 		WHERE id = ?`

// 	stmt, err := su.PieChartStore.Db.Prepare(query)
// 	if err != nil {
// 		return Piechart{}, err
// 	}

// 	defer stmt.Close()

// 	su.Piechart.ID = id
// 	err = stmt.QueryRow(
// 		su.Piechart.ID,
// 	).Scan(
// 		&su.Piechart.ID,
// 		&su.Piechart.Xsamplename,
// 		&su.Piechart.Email,
// 		&su.Piechart.CreatedAt,
// 	)
// 	if err != nil {
// 		return Piechart{}, err
// 	}

// 	return su.Piechart, nil
// }

// Peserta MCU -- MCU001
func GetPieChartMcu001(code string, id int) (string, Piechart, error) {
	var title = "Peserta  MCU"
	option := Piechart{
		Title: struct {
			Text    string `json:"text"`
			SubText string `json:"subtext"`
			Left    string `json:"left"`
		}{
			Text:    title,
			SubText: "Total Peserta 506",
			Left:    "center",
		},
		Tooltip: struct {
			Trigger string `json:"trigger"`
		}{
			Trigger: "item",
		},
		Legend: struct {
			Top    string `json:"top"`
			Left   string `json:"left"`
			Orient string `json:"orient"`
		}{
			Top:    "bottom",
			Left:   "center",
			Orient: "vertical",

			// KALAU BERTABRAKAN DGN ATASNYA SETTING PAKE DIBAWAH INI
			// Top:    "bottom",
			// Left:   "center",
			// Orient: "horizontal",
		},
		Series: []Series{
			{
				Label: struct {
					Position  string `json:"position"`
					Formatter string `json:"formatter"`
				}{
					Position:  "inner",
					Formatter: "{d}%",
				},
				Name:   "Access From",
				Type:   "pie",
				Radius: []string{"20%", "50%"},
				ItemStyle: struct {
					BorderRadius int    `json:"borderRadius"`
					BorderColor  string `json:"borderColor"`
					BorderWidth  int    `json:"borderWidth"`
				}{
					BorderRadius: 10,
					BorderColor:  "#fff",
					BorderWidth:  2,
				},
				Data: []SeriesData{
					{Value: 506, Name: "Sudah MCU : 506 Peserta"},
					{Value: 54, Name: "Belum MCU : 54 Peserta"},
				},
				Emphasis: struct {
					ItemStyle struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					} `json:"itemStyle"`
				}{
					ItemStyle: struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					}{
						ShadowBlur:    10,
						ShadowOffsetX: 0,
						ShadowColor:   "rgba(0, 0, 0, 0.5)",
					},
				},
			},
		},
	}
	return title, option, nil
}

// Kepersertaan MCU Berdasarkan Jenis Kelamin -- MCU002
func GetPieChartMcu002(code string, id int) (string, Piechart, error) {
	var title = "Kepesertaan  MCU"
	option := Piechart{
		Title: struct {
			Text    string `json:"text"`
			SubText string `json:"subtext"`
			Left    string `json:"left"`
		}{
			Text:    title,
			SubText: "Berdasarkan Jenis Kelamin",
			Left:    "center",
		},
		Tooltip: struct {
			Trigger string `json:"trigger"`
		}{
			Trigger: "item",
		},
		Legend: struct {
			Top    string `json:"top"`
			Left   string `json:"left"`
			Orient string `json:"orient"`
		}{
			Top:    "bottom",
			Left:   "center",
			Orient: "vertical",

			// KALAU BERTABRAKAN DGN ATASNYA SETTING PAKE DIBAWAH INI
			// Top:    "bottom",
			// Left:   "center",
			// Orient: "horizontal",
		},
		Series: []Series{
			{
				Label: struct {
					Position  string `json:"position"`
					Formatter string `json:"formatter"`
				}{
					Position:  "inner",
					Formatter: "{d}%",
				},
				Name:   "Access From",
				Type:   "pie",
				Radius: []string{"20%", "50%"},
				ItemStyle: struct {
					BorderRadius int    `json:"borderRadius"`
					BorderColor  string `json:"borderColor"`
					BorderWidth  int    `json:"borderWidth"`
				}{
					BorderRadius: 10,
					BorderColor:  "#fff",
					BorderWidth:  2,
				},
				Data: []SeriesData{
					{Value: 143, Name: "Laki-laki : 143 Peserta"},
					{Value: 114, Name: "Perempuan : 114 Peserta"},
				},
				Emphasis: struct {
					ItemStyle struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					} `json:"itemStyle"`
				}{
					ItemStyle: struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					}{
						ShadowBlur:    10,
						ShadowOffsetX: 0,
						ShadowColor:   "rgba(0, 0, 0, 0.5)",
					},
				},
			},
		},
	}
	return title, option, nil
}

// Peserta MCU berdasarkan umur
func GetPieChartMcu003(code string, id int) (string, Piechart, error) {
	var title = "Peserta MCU"
	option := Piechart{
		Title: struct {
			Text    string `json:"text"`
			SubText string `json:"subtext"`
			Left    string `json:"left"`
		}{
			Text:    title,
			SubText: "Berdasarkan Umur",
			Left:    "center",
		},
		Tooltip: struct {
			Trigger string `json:"trigger"`
		}{
			Trigger: "item",
		},
		Legend: struct {
			Top    string `json:"top"`
			Left   string `json:"left"`
			Orient string `json:"orient"`
		}{
			Top:    "bottom",
			Left:   "center",
			Orient: "vertical",

			// KALAU BERTABRAKAN DGN ATASNYA SETTING PAKE DIBAWAH INI
			// Top:    "bottom",
			// Left:   "center",
			// Orient: "horizontal",
		},
		Series: []Series{
			{
				Label: struct {
					Position  string `json:"position"`
					Formatter string `json:"formatter"`
				}{
					Position:  "inner",
					Formatter: "{d}%",
				},
				Name:   "Access From",
				Type:   "pie",
				Radius: []string{"20%", "50%"},
				ItemStyle: struct {
					BorderRadius int    `json:"borderRadius"`
					BorderColor  string `json:"borderColor"`
					BorderWidth  int    `json:"borderWidth"`
				}{
					BorderRadius: 10,
					BorderColor:  "#fff",
					BorderWidth:  2,
				},
				Data: []SeriesData{
					{Value: 188, Name: "< 30 Tahun : 188 Peserta"},
					{Value: 194, Name: "30 - < 40 Tahun : 194 Peserta"},
					{Value: 123, Name: "40 - < 50 Tahun : 123 Peserta"},
					{Value: 55, Name: "≥ 50 Tahun : 55 Peserta"},
				},
				Emphasis: struct {
					ItemStyle struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					} `json:"itemStyle"`
				}{
					ItemStyle: struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					}{
						ShadowBlur:    10,
						ShadowOffsetX: 0,
						ShadowColor:   "rgba(0, 0, 0, 0.5)",
					},
				},
			},
		},
	}
	return title, option, nil
}

// Status Index Masa Tubuh (BMI) -- MCU006
func GetPieChartMcu006(code string, id int) (string, Piechart, error) {
	var title = "Status Index Masa Tubuh (BMI)"
	option := Piechart{
		Title: struct {
			Text    string `json:"text"`
			SubText string `json:"subtext"`
			Left    string `json:"left"`
		}{
			Text:    title,
			SubText: "",
			Left:    "center",
		},
		Tooltip: struct {
			Trigger string `json:"trigger"`
		}{
			Trigger: "item",
		},
		Legend: struct {
			Top    string `json:"top"`
			Left   string `json:"left"`
			Orient string `json:"orient"`
		}{
			Top:    "bottom",
			Left:   "center",
			Orient: "vertical",

			// KALAU BERTABRAKAN DGN ATASNYA SETTING PAKE DIBAWAH INI
			// Top:    "bottom",
			// Left:   "center",
			// Orient: "horizontal",
		},
		Series: []Series{
			{
				Label: struct {
					Position  string `json:"position"`
					Formatter string `json:"formatter"`
				}{
					Position:  "inner",
					Formatter: "{d}%",
				},
				Name:   "Access From",
				Type:   "pie",
				Radius: []string{"20%", "50%"},
				ItemStyle: struct {
					BorderRadius int    `json:"borderRadius"`
					BorderColor  string `json:"borderColor"`
					BorderWidth  int    `json:"borderWidth"`
				}{
					BorderRadius: 10,
					BorderColor:  "#fff",
					BorderWidth:  2,
				},
				Data: []SeriesData{
					{Value: 19, Name: "Underweight : 19 Peserta"},
					{Value: 104, Name: "Normal : 104 Peserta"},
					{Value: 230, Name: "Obese 1  : 230 Peserta"},
					{Value: 99, Name: "Obese 2 : 99 Peserta"},
				},
				Emphasis: struct {
					ItemStyle struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					} `json:"itemStyle"`
				}{
					ItemStyle: struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					}{
						ShadowBlur:    10,
						ShadowOffsetX: 0,
						ShadowColor:   "rgba(0, 0, 0, 0.5)",
					},
				},
			},
		},
	}

	return title, option, nil
}

// Kriteria Hipertensi -- MCU007
func GetPieChartMcu007(code string, id int) (string, Piechart, error) {
	var title = "Kriteria Hipertensi"
	option := Piechart{
		Title: struct {
			Text    string `json:"text"`
			SubText string `json:"subtext"`
			Left    string `json:"left"`
		}{
			Text:    title,
			SubText: "",
			Left:    "center",
		},
		Tooltip: struct {
			Trigger string `json:"trigger"`
		}{
			Trigger: "item",
		},
		Legend: struct {
			Top    string `json:"top"`
			Left   string `json:"left"`
			Orient string `json:"orient"`
		}{
			Top:    "bottom",
			Left:   "center",
			Orient: "vertical",

			// KALAU BERTABRAKAN DGN ATASNYA SETTING PAKE DIBAWAH INI
			// Top:    "bottom",
			// Left:   "center",
			// Orient: "horizontal",
		},
		Series: []Series{
			{
				Label: struct {
					Position  string `json:"position"`
					Formatter string `json:"formatter"`
				}{
					Position:  "inner",
					Formatter: "{d}%",
				},
				Name:   "Access From",
				Type:   "pie",
				Radius: []string{"20%", "50%"},
				ItemStyle: struct {
					BorderRadius int    `json:"borderRadius"`
					BorderColor  string `json:"borderColor"`
					BorderWidth  int    `json:"borderWidth"`
				}{
					BorderRadius: 10,
					BorderColor:  "#fff",
					BorderWidth:  2,
				},
				Data: []SeriesData{
					{Value: 237, Name: "Normal : 237 Peserta"},
					{Value: 215, Name: "Prehipertensi : 215 Peserta"},
					{Value: 66, Name: "Hipertensi Grade I  : 66 Peserta"},
					{Value: 33, Name: "Hipertensi Grade II : 33 Peserta"},
				},
				Emphasis: struct {
					ItemStyle struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					} `json:"itemStyle"`
				}{
					ItemStyle: struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					}{
						ShadowBlur:    10,
						ShadowOffsetX: 0,
						ShadowColor:   "rgba(0, 0, 0, 0.5)",
					},
				},
			},
		},
	}

	return title, option, nil
}

// Visus Jauh -- MCU008
func GetPieChartMcu008(code string, id int) (string, Piechart, error) {
	var title = "Visus Jauh"
	option := Piechart{
		Title: struct {
			Text    string `json:"text"`
			SubText string `json:"subtext"`
			Left    string `json:"left"`
		}{
			Text:    title,
			SubText: "",
			Left:    "center",
		},
		Tooltip: struct {
			Trigger string `json:"trigger"`
		}{
			Trigger: "item",
		},
		Legend: struct {
			Top    string `json:"top"`
			Left   string `json:"left"`
			Orient string `json:"orient"`
		}{
			Top:    "bottom",
			Left:   "center",
			Orient: "vertical",

			// KALAU BERTABRAKAN DGN ATASNYA SETTING PAKE DIBAWAH INI
			// Top:    "bottom",
			// Left:   "center",
			// Orient: "horizontal",
		},
		Series: []Series{
			{
				Label: struct {
					Position  string `json:"position"`
					Formatter string `json:"formatter"`
				}{
					Position:  "inner",
					Formatter: "{d}%",
				},
				Name:   "Access From",
				Type:   "pie",
				Radius: []string{"20%", "50%"},
				ItemStyle: struct {
					BorderRadius int    `json:"borderRadius"`
					BorderColor  string `json:"borderColor"`
					BorderWidth  int    `json:"borderWidth"`
				}{
					BorderRadius: 10,
					BorderColor:  "#fff",
					BorderWidth:  2,
				},
				Data: []SeriesData{
					{Value: 237, Name: "Normal : 237 Peserta"},
					{Value: 194, Name: "Kelainan Refraksi : 194 Peserta"},
					{Value: 64, Name: "Kelainan Refraksi terkoreksi : 64 Peserta"},
					{Value: 56, Name: "Kelainan Refraksi tidak terkoreksi : 56 Peserta"},
				},
				Emphasis: struct {
					ItemStyle struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					} `json:"itemStyle"`
				}{
					ItemStyle: struct {
						ShadowBlur    int    `json:"shadowBlur"`
						ShadowOffsetX int    `json:"shadowOffsetX"`
						ShadowColor   string `json:"shadowColor"`
					}{
						ShadowBlur:    10,
						ShadowOffsetX: 0,
						ShadowColor:   "rgba(0, 0, 0, 0.5)",
					},
				},
			},
		},
	}

	return title, option, nil
}

// Kelainan MCU - MCU004
func GetBarChartMcu004(code string, id int) (string, Barchart, error) {
	title := "Kelainan MCU"
	option := Barchart{}
	option.Title = struct {
		Text string "json:\"text\""
	}{title}
	option.Dataset = struct {
		Source [][]interface{} "json:\"source\""
	}{[][]interface{}{
		{"score", "amount", "product", "percentage"},
		{200, 447, "Body Mass Index"},
		{150, 314, "Pemeriksaan Mata"},
		{125, 314, "Fungsi Lemak"},
		{100, 254, "Autospirometri"},
		{90, 200, "Hematologi"},
		{80, 180, "ECG"},
		{75, 160, "Hiperuricemia"},
		{70, 125, "Tekanan Darah"},
		{60, 118, "Urine Lengkap"},
		{50, 87, "Fungsi Hati"},
	}}
	option.Grid.ContainLabel = true
	option.XAxis.Name = "amount"
	option.YAxis.Type = "category"
	option.VisualMap.Orient = "horizontal"
	option.VisualMap.Left = "center"
	option.VisualMap.Min = 10
	option.VisualMap.Max = 100
	option.VisualMap.Show = false
	option.VisualMap.Dimension = 0
	option.VisualMap.InRange.Color = []string{"#42aaf5", "#00eaf2", "#035bff"}
	option.Series = []struct {
		Label struct {
			Position  string "json:\"position\""
			Show      bool   "json:\"show\""
			Formatter string "json:\"formatter\""
		} "json:\"label\""
		Type   string "json:\"type\""
		Encode struct {
			X string "json:\"x\""
			Y string "json:\"y\""
		} "json:\"encode\""
	}{
		{Label: struct {
			Position  string "json:\"position\""
			Show      bool   "json:\"show\""
			Formatter string "json:\"formatter\""
		}{Position: "right", Show: true, Formatter: "{@[3]} %"},
			Type: "bar", Encode: struct {
				X string "json:\"x\""
				Y string "json:\"y\""
			}{X: "amount", Y: "product"}},
	}

	return title, option, nil

}

// Kelainan MCU - MCU004
func GetBarChartMcu005(code string, id int) (string, Barchart, error) {
	title := "Kelainan Fisik"
	option := Barchart{}
	option.Title = struct {
		Text string "json:\"text\""
	}{title}
	option.Dataset = struct {
		Source [][]interface{} "json:\"source\""
	}{[][]interface{}{
		{"score", "amount", "product", "percentage"},
		{200, 400, "Obesitas Stage 1"},
		{150, 300, "Pre Hipertensi"},
		{133, 250, "Kelainan Refraksi"},
		{125, 180, "Overweight"},
		{100, 125, "Obesitas Stage 2"},
		{80, 100, "Hipertensi grade 1"},
		{70, 80, "Kel. Refrakti terkoreksi"},
		{60, 70, "Kel. Refrakti tidak terkoreksi"},
		{55, 55, "Hipertensi grade 2"},
	}}
	option.Grid.ContainLabel = true
	option.XAxis.Name = "amount"
	option.YAxis.Type = "category"
	option.VisualMap.Orient = "horizontal"
	option.VisualMap.Left = "center"
	option.VisualMap.Min = 10
	option.VisualMap.Max = 100
	option.VisualMap.Show = false
	option.VisualMap.Dimension = 0
	option.VisualMap.InRange.Color = []string{"#42aaf5", "#00eaf2", "#035bff"}
	option.Series = []struct {
		Label struct {
			Position  string "json:\"position\""
			Show      bool   "json:\"show\""
			Formatter string "json:\"formatter\""
		} "json:\"label\""
		Type   string "json:\"type\""
		Encode struct {
			X string "json:\"x\""
			Y string "json:\"y\""
		} "json:\"encode\""
	}{
		{Label: struct {
			Position  string "json:\"position\""
			Show      bool   "json:\"show\""
			Formatter string "json:\"formatter\""
		}{Position: "right", Show: true, Formatter: "{@[3]} %"},
			Type: "bar", Encode: struct {
				X string "json:\"x\""
				Y string "json:\"y\""
			}{X: "amount", Y: "product"}},
	}

	return title, option, nil

}
func (su *ServicesPiechart) GetPieChart(code string, id int) (string, Piechart, error) {
	if code == "mcu001" {
		return GetPieChartMcu001(code, id)
	}

	if code == "mcu002" {
		return GetPieChartMcu002(code, id)
	}

	if code == "mcu003" {
		return GetPieChartMcu003(code, id)
	}

	if code == "mcu006" {
		return GetPieChartMcu006(code, id)
	}

	if code == "mcu007" {
		return GetPieChartMcu007(code, id)
	}

	if code == "mcu008" {
		return GetPieChartMcu008(code, id)
	}

	return "", Piechart{}, fmt.Errorf("code " + code + " not found")
}
func (su *ServicesPiechart) GetBarChart(code string, id int) (string, Barchart, error) {

	if code == "mcu004" {
		return GetBarChartMcu004(code, id)
	}
	if code == "mcu005" {
		return GetBarChartMcu005(code, id)
	}

	return "", Barchart{}, fmt.Errorf("code " + code + " not found")
}
