package main

import (
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/radovskyb/watcher"
	"github.com/sirupsen/logrus"

	"lc/common/util"
)

func WatchDevConfig(args ...interface{}) interface{} {
	w := watcher.New()
	w.SetMaxEvents(20)
	w.FilterOps(watcher.Create, watcher.Write, watcher.Remove)
	go func() {
		for {
			select {
			case event := <-w.Event:
				if !event.IsDir() {
					ok, _ := regexp.MatchString(`[1-9].*json`, event.Name())
					if ok {
						strList := strings.Split(event.Name(), ".")
						code, _ := strconv.Atoi(strList[0])
						switch event.Op {
						case watcher.Create, watcher.Write:
							rtuArr, err := LoadDev(uint8(code))
							if err != nil {
								logrus.Errorf("加载RTU文件[%s]失败:%s", strconv.Itoa(code)+".json", err.Error())
							} else {
								GetDeviceMgr().UpdateDevices(uint8(code), rtuArr)
							}
						case watcher.Remove:
							GetDeviceMgr().UpdateDevices(uint8(code), nil)
						default:
						}
					}
				}
			case <-w.Closed:
				return
			}
		}
	}()

	if err := w.Add(util.GetPath(1)); err != nil {
		return 0
	}
	if err := w.Start(time.Second); err != nil {
		return 0
	}
	return 0
}

func WatchModelConfig(args ...interface{}) interface{} {
	w := watcher.New()
	w.SetMaxEvents(20)
	w.FilterOps(watcher.Create, watcher.Write, watcher.Remove)
	go func() {
		for {
			select {
			case event := <-w.Event:
				if !event.IsDir() {
					ok, _ := regexp.MatchString(`[1-9].*json`, event.Name())
					if ok {
						strList := strings.Split(event.Name(), ".")
						tid, _ := strconv.Atoi(strList[0])
						switch event.Op {
						case watcher.Create:
							GetDeviceMgr().UpdateModel(uint16(tid), 2)
						case watcher.Write:
							GetDeviceMgr().UpdateModel(uint16(tid), 1)
						case watcher.Remove:
							GetDeviceMgr().UpdateModel(uint16(tid), 0)
						default:
						}
					}
				}
			case err := <-w.Error:
				logrus.Errorf("WatchModelConfig:发生错误:%s", err.Error())
			case <-w.Closed:
				return
			}
		}
	}()
	if err := w.Add(util.GetPath(2)); err != nil {
		logrus.Errorf("WatchModelConfig:Add(conf)发生错误:%s", err.Error())
	}
	if err := w.Start(time.Second); err != nil {
		logrus.Errorf("WatchModelConfig:Start发生错误:%s", err.Error())
	}
	return 0
}

func WatchConfConfig(args ...interface{}) interface{} {
	w := watcher.New()
	w.SetMaxEvents(20)
	w.FilterOps(watcher.Create, watcher.Write)
	go func() {
		for {
			select {
			case event := <-w.Event:
				if !event.IsDir() {
					if event.Name() == "app.json" {
						if event.Op == watcher.Create || event.Op == watcher.Write {
							UpdateAppConfig()
						}
					}
					if event.Name() == "serial.json" {
						if event.Op == watcher.Create || event.Op == watcher.Write {
							UpdateSerialConfig()
						}
					}
				}
			case err := <-w.Error:
				logrus.Errorf("WatchConfConfig:发生错误:%s", err.Error())
			case <-w.Closed:
				return
			}
		}
	}()
	if err := w.Add(util.GetPath(0)); err != nil {
		logrus.Errorf("WatchConfConfig:Add(conf)发生错误:%s", err.Error())
	}
	if err := w.Start(time.Second); err != nil {
		logrus.Errorf("WatchConfConfig:Start发生错误:%s", err.Error())
	}
	return 0
}

func UpdateAppConfig() {
	if err := loadAppConfig(); err != nil {
		logrus.Errorf("UpdateAppConfig:重新加载app.json文件失败:%s", err.Error())
		return
	}
}

func UpdateSerialConfig() {
	if err := loadSerialConfig(); err != nil {
		logrus.Errorf("UpdateSerialConfig:重新加载serial.json文件失败:%s", err.Error())
		return
	}
	GetSerialMgr().AddSerialPorts(serialConfig.Serial)
}
