package main

import (
	"Toolbox/goDataverseStrict/metrics"
	"errors"
	"syscall/js"

	"git.archium.org/archium_public/ebkTools/wasm"
	archiumwasm "git.archium.org/archium_public/ebkTools/wasm"
)

const ca0 = `<canvas id="barChart"></canvas>`

const insebody0 = ` const ctx = document.getElementById('barChart');

  //var labelsa = ['A','B','C','D','E','F','G','H'];
  //var dataaa = [860,1140,1060,1060,1070,1110,1330,2210,7830,2478];
  //var dataab = [1600,1700,1700,1900,2000,2700,4000,5000,6000,7000];

  var dataa = {
      labels: [], //labelsa,
      datasets: [{
      label: "Gesamtanzahl",
      data: [], //[1,11,10,60,10,70,111,13],
      borderColor: "red",
      backgroundColor: "red",
      borderWidth: 1,
      fill: true
    },{
      label: "Differenz",
      data: [], //[16,17,17,19,20,27,40,50],
      borderColor: "blue",
      backgroundColor: "blue",
      borderWidth: 1,
      fill: false
    }]
    };


    var fubagchart = new Chart(ctx, {
    type: 'bar',
    data: dataa,
    options: {
      scales: {
        y: {
          beginAtZero: true
        }
      }
    }
  });
`

func helpTpGetLevelStatus(injectpoint js.Value, meisleisz *[]metrics.OneJumpPointSet, level int8) (result0 []wasm.Checkerboard, result1 string) {
	a, _ := metrics.FilterStacklist(*meisleisz, level)
	var b []string
	for _, v := range a {
		b = append(b, metrics.IDRequestCriteriaPrefix+v)
	}

	result0 = wasm.GetStatusFromBulkOfIds(injectpoint, b)

	for _, v := range wasm.GetStatusFromBulkOfIds(injectpoint, b) {
		if v.Checked {
			result1 = v.Value
			break
		}
	}

	return
}

func (gothis *lokalRequestType) changeTicker(s string, l int8) {
	dombase := js.Global().Get("document")

	//closureGetIdWhichJustChanged := getIdWhichJustChanged()
	//log.Println("DIE BACK:", s, l)
	//s, l := closureGetIdWhichJustChanged(dombase, metrics.OneJumpPoints)

	//FIXME: Aktivierung und Deaktivierung von Datensätzen pro Level funktioniert nicht mehr!? Neu machen, im Moment ignoriere ich das, damit Elfrun arbeiten kann.
	actualizeForms(dombase, metrics.OneJumpPoints, int8(l), s)
	//actualizeForms(dombase, metrics.OneJumpPoints, 2, s)

}

func (gothis *lokalRequestType) pseudoSubmit(jsthis js.Value, args []js.Value) any {
	dombase := js.Global().Get("document")

	gothis.Level0MetricsType, gothis.Level1MetricsDataCountType, gothis.Level2SearchType, gothis.Level21UserInput, gothis.Level3ReturnformatType = func() string {
		_, b := helpTpGetLevelStatus(dombase, &metrics.OneJumpPoints, 0)
		return b
	}(),
		func() string {
			_, b := helpTpGetLevelStatus(dombase, &metrics.OneJumpPoints, 1)
			return b
		}(),
		func() string {
			_, b := helpTpGetLevelStatus(dombase, &metrics.OneJumpPoints, 2)
			return b
		}(),
		func() string {
			_, b := helpTpGetLevelStatus(dombase, &metrics.OneJumpPoints, 2)
			switch b {
			case string(metrics.ST1total):
				return ""
			default:
				//case string(metrics.ST2_tomonth):
				//case string(metrics.ST3_pastdays):
				//case string(metrics.ST4_monthly):
				//case string(metrics.ST5_tree):
				return dombase.Call(archiumwasm.DOCUMENTS_METHOD_getElementById, metrics.IDRequestTextPrefix+b).Get("value").String()
			}
		}(),
		func() string {
			_, b := helpTpGetLevelStatus(dombase, &metrics.OneJumpPoints, 3)
			return b
		}()

	//triggerResultprintation(metrics.MetricsType(checked_type), metrics.ReturnformatType(checked_result), newcheckers, bodyInject)

	return nil
}

func removeAllDataGo(this js.Value, args []js.Value) interface{} {
	chart := args[0]
	//log.Println(len(args))
	//log.Printf("%v\n", args)
	//_ = js.Global().Call("alert", countDatasets)

	for datasetCounter, countDatasets, countDatas := 0, chart.Get("data").Get("datasets").Index(0).Get("data").Get("length").Int(), chart.Get("data").Get("datasets").Get("length").Int(); datasetCounter < countDatasets; datasetCounter++ {

		chart.Get("data").Get("labels").Call("pop")

		for i := 0; i < countDatas; i++ {
			chart.Get("data").Get("datasets").Index(i).Get("data").Call("pop")
		}
	}

	chart.Call("update")
	return nil
}

func addAllDataGo(this js.Value, args []js.Value) any {
	chart, newdata := args[0], args[1]

	var numberOfValues int

	if numberOfValues = newdata.Index(0).Get("length").Int(); numberOfValues != newdata.Index(0).Get("length").Int() || numberOfValues != newdata.Index(2).Get("length").Int() {
		return errors.New("Inconsistent input data array!")
	} else {
		for i := 0; i < numberOfValues; i++ {
			chart.Get("data").Get("labels").Call("push", newdata.Index(0).Index(i))
			chart.Get("data").Get("datasets").Index(0).Get("data").Call("push", newdata.Index(1).Index(i))
			chart.Get("data").Get("datasets").Index(1).Get("data").Call("push", newdata.Index(2).Index(i))
		}

		//log.Println(chart, newdata)
		//log.Println(newdata.Index(0), newdata.Index(0).Index(0))

		chart.Call("update")
		return nil
	}
}
