package priority

import (
	"os"
	"pbx-api-gin/pkg/utils"
	"strconv"

	"gopkg.in/ini.v1"
)

var ICPAnswer = 0
var OCCAnswer = 0

var SpecialVoice = 0

var PADStart = 0
var PADTMSStart = 0
var PADOccStart = 0

var InterruptedPad = ""
var filePath = "/etc/asterisk/priority.conf"

type TaskInfo struct {
	RunChannel   string `json:"runChannel"`
	LocalChan    string `json:"localChan"`
	RunType      string `json:"runType"`
	Priority     int    `json:"priority"`
	ConfbridgeID string `json:"confbridgeID"`
	Running      bool
}

type TaskPriority struct {
	Priority int `json:"priority"`
}

type Tasks struct {
	PA     TaskPriority `json:"pa"`
	C2C    TaskPriority `json:"c2c"`
	PADICP TaskPriority `json:"padicp"`
	PADTMS TaskPriority `json:"padtms"`
	PADOCC TaskPriority `json:"padocc"`
	CPA    TaskPriority `json:"cpa"`
	EMG    TaskPriority `json:"emg"`
	SPC    TaskPriority `json:"spc"`
	DCS    TaskPriority `json:"dcs"`
	STN    TaskPriority `json:"stn"`
	CHK    TaskPriority `json:"chk"`
	VOL    TaskPriority `json:"vol"`
}

var AllTasks Tasks

func GetPriority() {
	utils.LoggerDebug.Printf("Init system priority !")

	_, err := os.Stat(filePath)
	if err != nil {
		utils.LoggerDebug.Printf("GetPriority err :%+v", err)
		return
	}
	iniFile, err := ini.Load(filePath)
	if err != nil {
		utils.LoggerDebug.Printf("GetPriority err :%+v", err)
		return
	}

	AllTasks.CHK.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("CHK").Value())
	AllTasks.STN.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("STN").Value())
	AllTasks.DCS.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("DCS").Value())
	AllTasks.SPC.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("SPC").Value())
	AllTasks.EMG.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("EMG").Value())
	AllTasks.CPA.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("CPA").Value())
	AllTasks.PADOCC.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("PAD-OCC").Value())
	AllTasks.PADTMS.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("PAD-TMS").Value())
	AllTasks.PADICP.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("PAD-ICP").Value())
	AllTasks.C2C.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("CabCab").Value())
	AllTasks.PA.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("ManuPa").Value())
	AllTasks.VOL.Priority, _ = strconv.Atoi(iniFile.Section("general").Key("VOL").Value())

}

func GetPriorityByKey(key string) int {

	switch key {
	case "VOL":
		return AllTasks.VOL.Priority
	case "CHK":
		return AllTasks.CHK.Priority
	case "STN":
		return AllTasks.STN.Priority
	case "DCS":
		return AllTasks.DCS.Priority
	case "SPC":
		return AllTasks.SPC.Priority
	case "EMG":
		return AllTasks.EMG.Priority
	case "CPA":
		return AllTasks.CPA.Priority
	case "PAD-OCC":
		return AllTasks.PADOCC.Priority
	case "PAD-TMS":
		return AllTasks.PADTMS.Priority
	case "PAD-ICP":
		return AllTasks.PADICP.Priority
	case "CabCab":
		return AllTasks.C2C.Priority
	case "ManuPa":
		return AllTasks.PA.Priority
	default:
		return 0
	}
}

// check priority , if the running priority is lowwer than the to run priority
func CheckPriority(runType string) bool {

	//Check special voice can not interrupt
	if SpecialVoice == 1 {
		return false
	}

	//Get the to run priority number in the config file
	toRunpriority := GetPriorityByKey(runType)

	runingtaskName, taskRuning, ok := RegistryTask.HighestPriorityRunningTask()
	if ok {
		utils.LoggerDebug.Printf("CheckPriority  TorunType:%s 	SpecialVoice:%d 	toRunpriority:%d 	runingpriority:%d ", runType, SpecialVoice, toRunpriority, taskRuning.Priority)
		//if the running task priority is lowwer
		if toRunpriority < taskRuning.Priority && toRunpriority != 0 {
			return true

		} else if toRunpriority == taskRuning.Priority {

			switch runType {
			case "PAD-ICP":
				return true
			case "PAD-TMS":
				return true
			case "EMG":
				return true
			case "SPC":
				return true
			case "DCS":
				if taskRuning.RunType == "STN" {
					return false
				} else {
					return true
				}

			case "STN":
				if taskRuning.RunType == "DCS" {
					return false
				} else {
					return true
				}
			case "CHK":
				return true
			case "VOL":
				return true
			}
		}

		if runingtaskName == "C2C" {
			if runType != "PA" && runType != "PAD-ICP" && runType != "PAD-TMS" && runType != "C2C" {
				return true
			}
		} else if runType == "C2C" {
			if runingtaskName != "PA" && runingtaskName != "PAD-ICP" && runingtaskName != "PAD-TMS" && runingtaskName != "C2C" {
				return true
			}
		}

	} else {
		//No any runing task now
		return true
	}

	return false
}
