package config_manager_go

import (
	"loafle.com/overflow/agent_api/observer"
	"loafle.com/overflow/agent_api/observer/messages"
)

func agentStartHandler() {
	agentStart := make(chan interface{}, 0)
	observer.Add(messages.AGT_STARTING, agentStart)
	go func() {
		data := <-agentStart
		path := data.(string)
		// load global config
		_configManager.loadGlobalConfig(path + "/" + "global.yaml")
		
		// load all crawler configs
		if err := _configManager.loadCrawlerConfigAll(); err != nil {
			// error process
		}
		
		observer.Remove(messages.AGT_STARTING, agentStart)
		observer.Notify(messages.CFG_LOADED, _configManager)
	}()
}

func agentEndHandler() {
	agentEnd := make(chan interface{}, 0)
	observer.Add(messages.AGT_WILL_STOP, agentEnd)
	go func() {
		_ = <-agentEnd
		observer.Remove(messages.AGT_WILL_STOP, agentEnd)
		observer.Remove(messages.TASK_SENSOR_ADD,_configManager.addCh)
		observer.Remove(messages.TASK_SENSOR_REMOVE,_configManager.removeCh)
	}()
}

func addSensorHandler() {
	observer.Add(messages.TASK_SENSOR_ADD,_configManager.addCh)
	go _configManager.addConfig()
}

func removeSensorHandler() {
	observer.Add(messages.TASK_SENSOR_REMOVE,_configManager.removeCh)
	go _configManager.removeConfig()
}


