package goMetrix

import (
	"Toolbox/goDataverseStrict/metrics"
	"fmt"
	"log"
	"net/http"
	"sort"
)

const (
	addr = "localhost:8008"
	dir  = ""
	//PLATZHALTER = "Þłæŧ←ħæłŧ€¶"
)

func sortMyDingse[dings metrics.MetricsType | metrics.MetricsDataCountType | metrics.SearchType | metrics.ReturnformatType, bums metrics.InputLevel0 | metrics.InputLevel1 | metrics.InputLevel2 | metrics.InputLevel3 | bool](dstring map[dings]bums) (raus []dings) {
	var klaus []string

	for i, _ := range dstring {
		klaus = append(klaus, string(i))
	}

	sort.Strings(klaus)

	return
}

func Handler(w http.ResponseWriter, r *http.Request) {

	var a [4]string

	for i := int8(0); i <= 3; i++ {
		tmp, _ := metrics.FilterStacklist(metrics.OneJumpPoints, i)
		for j, v := range tmp {
			if i == 3 || (i < 3 && len(v) > 0) {
				log.Println("Hallo", j, v, len(v))
			}

			a[i] += fmt.Sprintf("<span id=\"%s\" %s>", metrics.IDRequestHideDisplayPrefix+string(v), func() string {
				if i < 3 && len(v) > 0 {
					return ""
				} else { // i == 3 Resolve-Format ist vorerst nicht verhandelbar, nur zu DEBUG-Zwecken sichtbar machen.
					return "class=\"element-hide\""
				}
			}())

			a[i] += fmt.Sprintf("<input type=\"radio\" id=\"%s\" name=\"form_types_radio\" value=\"%s\" %s/><label for=\"%s\"><b>%s</b></label>",
				metrics.IDRequestCriteriaPrefix+string(v),
				string(v),
				func() string {
					if j == 0 && v != "" {
						return "checked"
					} else {
						return ""
					}
				}(),
				metrics.IDRequestCriteriaPrefix+string(v),
				string(v))
			if i == 2 {
				switch v {
				default: //Total ist default //case string(metrics.ST1_tota
					//a[i] += ""
				case string(metrics.ST2tomonth):
					a[i] += `<label for="` + metrics.IDRequestTextPrefix + string(metrics.ST2tomonth) + `"> Bis &lt;yyyy-mm&gt; (inklusive)</label>
		    <input name="form_searches" type="text" size="10" id="` + metrics.IDRequestTextPrefix + string(metrics.ST2tomonth) + `" value="2022-03" onSelect="this.enabled;getElementById('` + metrics.IDRequestTextPrefix + string(metrics.ST1total) + `').checked=false" onchange="checkoneuncheckany(ids,'` + metrics.IDRequestTextPrefix + string(metrics.ST2tomonth) + `')" onmouseover="enableonedisableany(ids, '` + metrics.IDRequestTextPrefix + string(metrics.ST2tomonth) + `')">`
				case string(metrics.ST3pastdays):
					a[i] += `<label for="` + metrics.IDRequestTextPrefix + string(metrics.ST3pastdays) + `"> Die letzten Tage</label>
					<input name="form_searches" type="text" size="10" id="` + metrics.IDRequestTextPrefix + string(metrics.ST3pastdays) + `" value="30" onchange="checkoneuncheckany(ids,'` + metrics.IDRequestTextPrefix + string(metrics.ST3pastdays) + `')" onmouseover="enableonedisableany(ids, '` + metrics.IDRequestTextPrefix + string(metrics.ST3pastdays) + `')">`
				case string(metrics.ST4monthly):
					a[i] += `<label for="` + metrics.IDRequestTextPrefix + string(metrics.ST4monthly) + `"> &lt;yyyy-mm&gt; bis &lt;yyyy-mm&gt;</label>
					<input name="form_searches" type="text" size="25" id="` + metrics.IDRequestTextPrefix + string(metrics.ST4monthly) + `" value="2020-11 bis 2021-08" onchange="checkoneuncheckany(ids,'` + metrics.IDRequestTextPrefix + string(metrics.ST4monthly) + `')" onmouseover="enableonedisableany(ids, '` + metrics.IDRequestTextPrefix + string(metrics.ST4monthly) + `')">
		`
				case string(metrics.ST5tree):
					a[i] += `<label for="` + metrics.IDRequestTextPrefix + string(metrics.ST5tree) + `"></label>
					<input name="form_searches" type="text" size="10" id="` + metrics.IDRequestTextPrefix + string(metrics.ST5tree) + `" value="2022-12" onchange="checkoneuncheckany(ids,'` + metrics.IDRequestTextPrefix + string(metrics.ST5tree) + `')" onmouseover="enableonedisableany(ids, '` + metrics.IDRequestTextPrefix + string(metrics.ST5tree) + `')">`
				}
			}
			a[i] += `<br></span>`
		}

	}

	/*

	 <label for="`+metrics.IDRequestTextPrefix+string(metrics.ST1_total)+`">Total</label>
	    <input name="form_searches" type="checkbox" id="`+metrics.IDRequestTextPrefix+string(metrics.ST1_total)+`" onchange="checkoneuncheckany(ids,'`+metrics.IDRequestTextPrefix+string(metrics.ST1_total)+`')" onmouseover="enableonedisableany(ids, '`+metrics.IDRequestTextPrefix+string(metrics.ST1_total)+`')" checked>







	*/

	/*
		//for i1, v1 := range metrics.OneJumpPointSet {
		for _, i1 := range sortMyDingse(metrics.OldOneJumpPointSet) {
			v1 := metrics.OldOneJumpPointSet[i1]

			s1 += fmt.Sprintf("<input type=\"radio\" id=\"input_%s\" name=\"form_types_radio\" value=\"%s\"><label for=\"input_%s\">%s</label><br>", string(i1), string(i1), string(i1), string(i1))
			if !done2 {
				done2 = true
				//for i2, v2 := range v1 {
				for _, i2 := range sortMyDingse(v1) {
					v2 := v1[i2]

					s2 += fmt.Sprintf("<input type=\"radio\" id=\"input_%s\" name=\"form_types_radio\" value=\"%s\" disabled><label for=\"input_%s\">%s</label><br>", string(i2), string(i2), string(i2), string(i2))
					if !done3 {
						done3 = true
						//for i3, v3 := range v2 {
						for _, i3 := range sortMyDingse(v2) {
							v3 := v2[i3]

							s3 += fmt.Sprintf("<input type=\"radio\" id=\"input_%s\" name=\"form_types_radio\" value=\"%s\" disabled><label for=\"input_%s\">%s</label><br>", string(i3), string(i3), string(i3), string(i3))
							if !done4 {
								done4 = true
								//for i4, _ := range v3 {
								for _, i4 := range sortMyDingse(v3) {
									//v4 := v3[i4]

									s4 += fmt.Sprintf("<input type=\"radio\" id=\"input_%s\" name=\"form_types_radio\" value=\"%s\" disabled><label for=\"input_%s\">%s</label><br>", string(i4), string(i4), string(i4), string(i4))
								}
							}
						}
					}
				}
			}
		}
	*/

	fmt.Fprintf(w, `<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>goMetrix</title>
	<link rel="stylesheet" href="/css/metrics.css">
    <script src="https://golang.org/misc/wasm/wasm_exec.js"></script>
    <script>
      if (!WebAssembly.instantiateStreaming) {
        // polyfill
        WebAssembly.instantiateStreaming = async (resp, importObject) => {
          const source = await (await resp).arrayBuffer();
          return await WebAssembly.instantiate(source, importObject);
        };
      }

      const go = new Go();
      let mod, inst;
      WebAssembly.instantiateStreaming(fetch("/wasm/app.wasm"), go.importObject)
        .then((result) => {
          go.run(result.instance);
        })
        .catch((err) => {
          console.error(err);
        });
    </script>
    <script>
        const ids = new Array(
        	"`+metrics.IDRequestTextPrefix+string(metrics.ST1total)+`",
			"`+metrics.IDRequestTextPrefix+string(metrics.ST2tomonth)+`",
			"`+metrics.IDRequestTextPrefix+string(metrics.ST3pastdays)+`",
			"`+metrics.IDRequestTextPrefix+string(metrics.ST4monthly)+`",
			"`+metrics.IDRequestTextPrefix+string(metrics.ST5tree)+`"
			);
		
		function pseudoSubmit2(){
			alert("läuft");
		}

 	   function enableonedisableany(ids, id) {
			for (let i = 0; i < ids.length; i++){
				//alert(ids[i]);
				//document.getElementById(ids[i]).checked = false;
			    document.getElementById(ids[i]).disabled = true;
			}
			document.getElementById(id).disabled = false;
			}

		function checkoneuncheckany(ids,id){
			for (let i = 0; i < ids.length; i++){
			    document.getElementById(ids[i]).checked = false;
			}
			document.getElementById(id).checked = true;
			}
    </script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<div id="formarea">
<p>
<table><tr>
<td valign="top" width="20%"><form id="form_metrictype">`+a[0]+`</form></td>
<td valign="top" width="20%"><form id="form_metricsdatacount">`+a[1]+`</form></td>
<td valign="top" width="50%"><form id="form_searchtype">`+a[2]+`</form></td>
<td valign="top" width="5%"><form id="form_returnformat">`+a[3]+`</form></td>
<td valign="top" width="5%"><input type="submit" onclick="pseudoSubmit2();" id="`+metrics.IDFormSubmit+`" value="Ok"  /></td>
</td></tr></table>



</p>
<!--<p>
<form id="form_results">
  <input type="hidden" name="form_results_radio" value="`+string(metrics.RT1json)+`" checked>
  <input type="radio" id="`+metrics.IDRequestTextPrefix+string(metrics.RT1json)+`" name="form_results_radio" value="`+string(metrics.RT1json)+`" checked>
  <label for="`+metrics.IDRequestTextPrefix+string(metrics.RT1json)+`">`+string(metrics.RT1json)+`</label><br>
  <input type="radio" id="`+metrics.IDRequestTextPrefix+string(metrics.RT2csv)+`" name="form_results_radio" value="`+string(metrics.RT2csv)+`">
  <label for="`+metrics.IDRequestTextPrefix+string(metrics.RT2csv)+`">`+string(metrics.RT2csv)+`</label><br>
</form>
</p>-->
<!--<p>
<form id="form_searches">
    <label for="`+metrics.IDRequestTextPrefix+string(metrics.ST1total)+`">Total</label>
    <input name="form_searches" type="checkbox" id="`+metrics.IDRequestTextPrefix+string(metrics.ST1total)+`" onchange="checkoneuncheckany(ids,'`+metrics.IDRequestTextPrefix+string(metrics.ST1total)+`')" onmouseover="enableonedisableany(ids, '`+metrics.IDRequestTextPrefix+string(metrics.ST1total)+`')" checked>
    <label for="`+metrics.IDRequestTextPrefix+string(metrics.ST2tomonth)+`">Bis &lt;yyyy-mm&gt; (inklusive)</label>
    <input name="form_searches" type="text" size="10" id="`+metrics.IDRequestTextPrefix+string(metrics.ST2tomonth)+`" value="2022-03" onSelect="this.enabled;getElementById('`+metrics.IDRequestTextPrefix+string(metrics.ST1total)+`').checked=false" onchange="checkoneuncheckany(ids,'`+metrics.IDRequestTextPrefix+string(metrics.ST2tomonth)+`')" onmouseover="enableonedisableany(ids, '`+metrics.IDRequestTextPrefix+string(metrics.ST2tomonth)+`')">
    <label for="`+metrics.IDRequestTextPrefix+string(metrics.ST3pastdays)+`">Die letzten Tage</label>
    <input name="form_searches" type="text" size="10" id="`+metrics.IDRequestTextPrefix+string(metrics.ST3pastdays)+`" value="30" onchange="checkoneuncheckany(ids,'`+metrics.IDRequestTextPrefix+string(metrics.ST3pastdays)+`')" onmouseover="enableonedisableany(ids, '`+metrics.IDRequestTextPrefix+string(metrics.ST3pastdays)+`')">
    <label for="`+metrics.IDRequestTextPrefix+string(metrics.ST4monthly)+`">&lt;yyyy-mm&gt; bis &lt;yyyy-mm&gt;</label>
    <input name="form_searches" type="text" size="25" id="`+metrics.IDRequestTextPrefix+string(metrics.ST4monthly)+`" value="2020-11 bis 2021-08" onchange="checkoneuncheckany(ids,'`+metrics.IDRequestTextPrefix+string(metrics.ST4monthly)+`')" onmouseover="enableonedisableany(ids, '`+metrics.IDRequestTextPrefix+string(metrics.ST4monthly)+`')">
    <label for="`+metrics.IDRequestTextPrefix+string(metrics.ST5tree)+`">Tree</label>
    <input name="form_searches" type="text" size="10" id="`+metrics.IDRequestTextPrefix+string(metrics.ST5tree)+`" value="2022-12" onchange="checkoneuncheckany(ids,'`+metrics.IDRequestTextPrefix+string(metrics.ST5tree)+`')" onmouseover="enableonedisableany(ids, '`+metrics.IDRequestTextPrefix+string(metrics.ST5tree)+`')">
</form>
</p>-->
</div>
<!--<div id="testarea"></div>-->
<div id="injectarea">&#128039;<!--&#x1F427;--></div>
<div id="chartarea"></div>
<script id="scriptarea"></script>
</body>
</html>`)

	/*
	   DEBUG:

	   	fmt.Fprintf(w, "😱<br>")

	   	md, me := metrics.MetricsTotal("http://localhost:8080", "f6933c5d-8e97-4e7a-b21a-c57447cca421", metrics.RT1json, metrics.MT1dataverses)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsTotal("http://localhost:8080", "f6933c5d-8e97-4e7a-b21a-c57447cca421", metrics.RT2csv, metrics.MT4downloads)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsTotal("http://localhost:8080", "f6933c5d-8e97-4e7a-b21a-c57447cca421", metrics.RT1json, metrics.MT2datasets)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsTotal("http://localhost:8080", "f6933c5d-8e97-4e7a-b21a-c57447cca421", metrics.RT2csv, metrics.MT3files)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	ts, _ := time.Parse("2006-01", "2021-06")
	   	md, me = metrics.MetricsToMonth("http://localhost:8080", "", metrics.RT2csv, metrics.MT2datasets, ts)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsToMonth("http://localhost:8080", "", metrics.RT2csv, metrics.MT4downloads, ts)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsPastDays("http://localhost:8080", "", metrics.RT2csv, metrics.MT2datasets, 30)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsMonthly("http://localhost:8080", "", metrics.RT2csv, metrics.MT4downloads)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsMonthly("http://localhost:8080", "", metrics.RT1json, metrics.MT4downloads)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsReallyMonthly("http://localhost:8080", "", metrics.RT1json, metrics.MT4downloads)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	//fmt.Println(metrics.ListUsers("http://localhost:8080", "f6933c5d-8e97-4e7a-b21a-c57447cca421", true))

	   	ts, _ = time.Parse("2006-01", "2020-12")
	   	md, me = metrics.MetricsTree("http://localhost:8080", "", metrics.RT1json, ts)
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	md, me = metrics.MetricsTree("http://localhost:8080", "", metrics.RT1json, time.Time{})
	   	fmt.Fprintf(w, "%s<br> %s<br><br>", me, md)

	   	//fmt.Fprintf(w, "%v", time.Now().Format("2006-02"))
	*/
}
