package monitor

import (
	"encoding/json"
	"fmt"
	"strconv"
	"time"

	"be.ems/lib/dborm"
	"be.ems/lib/log"
	"be.ems/restagent/config"
	"github.com/robfig/cron/v3"
	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/disk"
	"github.com/shirou/gopsutil/v3/load"
	"github.com/shirou/gopsutil/v3/mem"
	"github.com/shirou/gopsutil/v3/net"
)

type MonitorService struct{}

type IMonitorService interface {
	Run()
}

func NewIMonitorService() IMonitorService {
	return &MonitorService{}
}

func (m *MonitorService) Run() {
	// monitorStatus, _ := dborm.XormGetConfig("SystemMonitor", "MonitorStatus")
	// if monitorStatus["value"] == "disable" {
	// 	return
	// }
	var itemModel MonitorBase
	itemModel.CreatedAt = time.Now()
	itemModel.UpdatedAt = time.Now()

	totalPercent, _ := cpu.Percent(3*time.Second, false)
	if len(totalPercent) == 1 {
		itemModel.Cpu = totalPercent[0]
	}
	cpuCount, _ := cpu.Counts(false)

	loadInfo, _ := load.Avg()
	itemModel.CpuLoad1 = loadInfo.Load1
	itemModel.CpuLoad5 = loadInfo.Load5
	itemModel.CpuLoad15 = loadInfo.Load15
	itemModel.LoadUsage = loadInfo.Load1 / (float64(cpuCount*2) * 0.75) * 100

	memoryInfo, _ := mem.VirtualMemory()
	itemModel.Memory = memoryInfo.UsedPercent

	var dataSize int
	conf := config.GetYamlConfig()
	result, err := dborm.DbClient.XEngine.QueryString("SELECT SUM(data_length) AS data_size FROM information_schema.tables WHERE TABLE_SCHEMA = ?;", conf.Database.Name)
	if err != nil {
		dataSize = 0
	} else {
		v, _ := strconv.Atoi(result[0]["data_size"])
		dataSize = v
	}
	itemModel.DbSize = uint(dataSize)

	_, errx := dborm.DbClient.XEngine.Table("monitor_base").Insert(itemModel)
	if errx != nil {
		log.Errorf("Insert basic monitoring data failed, err: %v", err)
	}

	go loadDiskIO()
	go loadNetIO()

	// 删除保留的记录
	// monitorStoreDays, _ := dborm.XormGetConfig("SystemMonitor", "MonitorStoreDays")
	// if monitorStoreDays["value"] != "" {
	// 	return
	// }
	// storeDays, err := strconv.Atoi(MonitorStoreDays.Value)
	// if err != nil {
	// 	timeForDelete := time.Now().AddDate(0, 0, -storeDays)
	// 	DelMonitorBase(timeForDelete)
	// 	DelMonitorIO(timeForDelete)
	// 	DelMonitorNet(timeForDelete)
	// }

}

func loadDiskIO() {
	ioStat, _ := disk.IOCounters()

	time.Sleep(60 * time.Second)

	ioStat2, _ := disk.IOCounters()
	var ioList []MonitorIO
	for _, io2 := range ioStat2 {
		for _, io1 := range ioStat {
			if io2.Name == io1.Name {
				var itemIO MonitorIO
				itemIO.CreatedAt = time.Now()
				itemIO.UpdatedAt = time.Now()

				itemIO.Name = io1.Name
				if io2.ReadBytes != 0 && io1.ReadBytes != 0 && io2.ReadBytes > io1.ReadBytes {
					itemIO.Read = uint64(float64(io2.ReadBytes-io1.ReadBytes) / 60)
				}
				if io2.WriteBytes != 0 && io1.WriteBytes != 0 && io2.WriteBytes > io1.WriteBytes {
					itemIO.Write = uint64(float64(io2.WriteBytes-io1.WriteBytes) / 60)
				}

				if io2.ReadCount != 0 && io1.ReadCount != 0 && io2.ReadCount > io1.ReadCount {
					itemIO.Count = uint64(float64(io2.ReadCount-io1.ReadCount) / 60)
				}
				writeCount := uint64(0)
				if io2.WriteCount != 0 && io1.WriteCount != 0 && io2.WriteCount > io1.WriteCount {
					writeCount = uint64(float64(io2.WriteCount-io1.WriteCount) / 60)
				}
				if writeCount > itemIO.Count {
					itemIO.Count = writeCount
				}

				if io2.ReadTime != 0 && io1.ReadTime != 0 && io2.ReadTime > io1.ReadTime {
					itemIO.Time = uint64(float64(io2.ReadTime-io1.ReadTime) / 60)
				}
				writeTime := uint64(0)
				if io2.WriteTime != 0 && io1.WriteTime != 0 && io2.WriteTime > io1.WriteTime {
					writeTime = uint64(float64(io2.WriteTime-io1.WriteTime) / 60)
				}
				if writeTime > itemIO.Time {
					itemIO.Time = writeTime
				}
				ioList = append(ioList, itemIO)
				break
			}
		}
	}
	_, err := dborm.DbClient.XEngine.Table("monitor_io").Insert(ioList)
	if err != nil {
		log.Errorf("Insert io monitoring data failed, err: %v", err)
	}
}

func loadNetIO() {
	netStat, _ := net.IOCounters(true)
	netStatAll, _ := net.IOCounters(false)

	time.Sleep(60 * time.Second)

	netStat2, _ := net.IOCounters(true)
	var netList []MonitorNetwork
	for _, net2 := range netStat2 {
		for _, net1 := range netStat {
			if net2.Name == net1.Name {
				var itemNet MonitorNetwork
				itemNet.CreatedAt = time.Now()
				itemNet.UpdatedAt = time.Now()

				itemNet.Name = net1.Name

				if net2.BytesSent != 0 && net1.BytesSent != 0 && net2.BytesSent > net1.BytesSent {
					itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / 60
				}
				if net2.BytesRecv != 0 && net1.BytesRecv != 0 && net2.BytesRecv > net1.BytesRecv {
					itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / 60
				}
				netList = append(netList, itemNet)
				break
			}
		}
	}
	netStatAll2, _ := net.IOCounters(false)
	for _, net2 := range netStatAll2 {
		for _, net1 := range netStatAll {
			if net2.Name == net1.Name {
				var itemNet MonitorNetwork
				itemNet.Name = net1.Name
				if net2.BytesSent != 0 && net1.BytesSent != 0 && net2.BytesSent > net1.BytesSent {
					itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / 60
				}

				if net2.BytesRecv != 0 && net1.BytesRecv != 0 && net2.BytesRecv > net1.BytesRecv {
					itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / 60
				}
				netList = append(netList, itemNet)
				break
			}
		}
	}

	_, err := dborm.DbClient.XEngine.Table("monitor_network").Insert(netList)
	if err != nil {
		log.Errorf("Insert network monitoring data failed, err: %v", err)
	}
}

var c *cron.Cron
var monitorCronID int

func init() {
	c = cron.New()
	c.Start()
	monitorCronID = 0
}

// StartMonitor 开始监控任务 removeBefore删除上次任务，间隔interval分钟
func StartMonitor(removeBefore bool, interval string) error {
	if removeBefore {
		c.Remove(cron.EntryID(monitorCronID))
	}

	// 读取配置
	if interval == "" {
		v, err := dborm.XormGetConfig("SystemMonitor", "sampleTime")
		if err != nil {
			return err
		}
		data := make(map[string]any)
		err = json.Unmarshal([]byte(v["value_json"].(string)), &data)
		if err != nil {
			log.Error("json StartMonitor:%s", err.Error())
			return err
		}
		interval = data["sampleTime"].(string)
	}

	imservice := NewIMonitorService()
	monitorID, err := c.AddJob(fmt.Sprintf("@every %sm", interval), imservice)
	if err != nil {
		return err
	}
	imservice.Run()
	monitorCronID = int(monitorID)
	return nil
}
