// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"fmt"
	"sync"
	"time"

	"go.wit.com/gui"
	"go.wit.com/lib/gadgets"
	"go.wit.com/lib/protobuf/virtpb"
	"go.wit.com/log"
)

type stdHypervisorTableWin struct {
	sync.Mutex
	win    *gadgets.GenericWindow   // the machines gui window
	box    *gui.Node                // the machines gui parent box widget
	pb     *virtpb.Hypervisors      // the protobuf
	TB     *virtpb.HypervisorsTable // the gui table buffer
	update bool                     // if the window should be updated
}

func (w *stdHypervisorTableWin) Toggle() {
	if w == nil {
		return
	}
	if w.win == nil {
		return
	}
	w.win.Toggle()
}

func newHypervisorsWindow() *stdHypervisorTableWin {
	dwin := new(stdHypervisorTableWin)
	dwin.win = gadgets.NewGenericWindow("virtigo current hypervisors", "things to do")
	dwin.win.Custom = func() {
		log.Info("test delete window here")
	}

	// make a box at the bottom of the window for the protobuf table
	dwin.box = dwin.win.Bottom.Box().SetProgName("TBOX")
	return dwin
}

// default table protobuf window
func (dw *stdHypervisorTableWin) doStdHypervisors(pb *virtpb.Hypervisors) {
	dw.Lock()
	defer dw.Unlock()

	// erase the old table
	if dw.TB != nil {
		dw.TB.Delete()
		dw.TB = nil
	}

	// init the table
	dw.pb = pb
	t := dw.pb.NewTable("HypervisorsPB Off")
	t.NewUuid()
	t.SetParent(dw.box)

	// pick the columns
	t.AddHostname()
	t.AddMemory()
	t.AddCpus()
	t.AddKillcount()
	t.AddTimeFunc("last poll", func(h *virtpb.Hypervisor) time.Time {
		// hm := me.hmap[h]
		// tmp := hm.lastpoll
		// log.Info("poll age", h.Hostname, virtpb.FormatDuration(time.Since(tmp)))
		return time.Now()
	})
	t.AddStringFunc("droplets", func(h *virtpb.Hypervisor) string {
		/*
			var totalDroplets int
			var totalUnknownDroplets int
			// dur := time.Since(h.lastpoll)
			// tmp := virtpb.FormatDuration(dur)
			// fmt.Fprintln(w, h.pb.Hostname, "killcount =", h.killcount, "lastpoll:", tmp)
			hm := me.hmap[h]
			for name, _ := range hm.lastDroplets {
				totalDroplets += 1
				d := me.cluster.FindDropletByName(name)
				if d == nil {
					totalUnknownDroplets += 1
				}
			}
			log.Printf("Total Droplets %d total libvirt only droplets = %d\n", totalDroplets, totalUnknownDroplets)
			return fmt.Sprintf("%d", totalDroplets)
		*/
		return "todo"
	})

	// display the protobuf
	dw.TB = t
	f := func(e *virtpb.Hypervisor) {
		log.Info("std HypervisorWindow() something here", e.Hostname)
		// m.Enabled = true
	}
	dw.TB.Custom(f)

	dw.TB.ShowTable()
}

// default table protobuf window
func (dw *stdHypervisorTableWin) doNewStdHypervisors(pb *virtpb.Hypervisors) {
	dw.Lock()
	defer dw.Unlock()

	// erase the old table
	if dw.TB != nil {
		dw.TB.Delete()
		dw.TB = nil
	}

	// init the table
	dw.pb = pb
	t := dw.pb.NewTable("HypervisorsPB Off")
	t.NewUuid()
	t.SetParent(dw.box)

	// pick the columns
	t.AddHostname()
	t.AddMemory()
	t.AddCpus()
	t.AddKillcount()
	t.AddTimeFunc("last poll", func(h *virtpb.Hypervisor) time.Time {
		// hm := me.hmap[h]
		// tmp := hm.lastpoll
		// log.Info("poll age", h.Hostname, virtpb.FormatDuration(time.Since(tmp)))
		return time.Now()
	})
	t.AddStringFunc("droplets", func(h *virtpb.Hypervisor) string {
		var totalDroplets int
		var totalUnknownDroplets int
		// dur := time.Since(h.lastpoll)
		// tmp := virtpb.FormatDuration(dur)
		// fmt.Fprintln(w, h.pb.Hostname, "killcount =", h.killcount, "lastpoll:", tmp)
		hm := me.hmap[h]
		for name, _ := range hm.lastDroplets {
			totalDroplets += 1
			d := me.cluster.FindDropletByName(name)
			if d == nil {
				totalUnknownDroplets += 1
			}
		}
		// log.Printf("Total Droplets %d total libvirt only droplets = %d\n", totalDroplets, totalUnknownDroplets)
		return fmt.Sprintf("%d", totalDroplets)
	})

	// display the protobuf
	dw.TB = t
	f := func(e *virtpb.Hypervisor) {
		log.Info("std HypervisorWindow() something here", e.Hostname)
		// m.Enabled = true
	}
	dw.TB.Custom(f)

	dw.TB.ShowTable()
}