-- Gestione attrezzaggio per Saomad-KAIROS - 2023/10/16

-- Intestazioni
require( 'EgtBase')
_ENV = EgtProtectGlobal()
EgtEnableDebug( false)

-- Tavola di passaggio valori
local STU = {}
STU.TUUID = ""
STU.TCPOS = ""
STU.HEAD = ""
STU.GROUP = ""
STU.POS = ""
STU.EXIT = 0
STU.INDEX = 0
STU.HEAD1 = ""
STU.HEAD2 = ""
STU.ISVALID = false
STU.ERR = 0
_G.STU = STU

local INVALIDPOS = ""
local POS = "Pos"

-- Geom Set
local GS = {}

-- Configurazione posizioni
local PositionTable={{Pos = "Pos1",  TcPos = "T101",  Head = "H1", Group = "G1"},
                     {Pos = "Pos2",  TcPos = "T102",  Head = "H1", Group = "G1"},
                     {Pos = "Pos3",  TcPos = "T103",  Head = "H1", Group = "G1"},
                     {Pos = "Pos4",  TcPos = "T104",  Head = "H1", Group = "G1"},
                     {Pos = "Pos5",  TcPos = "T105",  Head = "H1", Group = "G1"},
                     {Pos = "Pos6",  TcPos = "T106",  Head = "H1", Group = "G1"},
                     {Pos = "Pos7",  TcPos = "T107",  Head = "H1", Group = "G1"},
                     {Pos = "Pos8",  TcPos = "T108",  Head = "H1", Group = "G1"},
                     {Pos = "Pos9",  TcPos = "T109",  Head = "H1", Group = "G1"},
                     {Pos = "Pos10", TcPos = "T110", Head = "H1", Group = "G2"},
                     {Pos = "Pos11", TcPos = "T111", Head = "H1", Group = "G2"},
                     {Pos = "Pos12", TcPos = "T112", Head = "H1", Group = "G2"},
                     {Pos = "Pos13", TcPos = "T201", Head = "H1", Group = "G3"},
                     {Pos = "Pos14", TcPos = "T202", Head = "H1", Group = "G3"},
                     {Pos = "Pos15", TcPos = "T203", Head = "H1", Group = "G3"},
                     {Pos = "Pos16", TcPos = "T204", Head = "H1", Group = "G3"},
                     {Pos = "Pos17", TcPos = "T205", Head = "H1", Group = "G4"},
                     {Pos = "Pos18", TcPos = "T206", Head = "H1", Group = "G4"},
                     {Pos = "Pos19", TcPos = "T207", Head = "H1", Group = "G4"}}
if EgtGetHeadId( 'H3') then
   PositionTable[13].Head = 'H3'
end

local UsePositionHead = true

local function IsInGeomSet( ToolHead, PosHead)
   for GsIndex = 1, #GS do
     local bToolHead = false
     local bPosHead = false
      for HIndex = 1, #GS[GsIndex] do
         if GS[GsIndex][HIndex] == ToolHead then
            bToolHead = true
         elseif GS[GsIndex][HIndex] == PosHead then
            bPosHead = true
         end
         if bToolHead and bPosHead then
            return true
         end
      end
      if bToolHead and bPosHead then
         return true
      end
   end
   return false
end

function STU.IsCompatibleHeads()
   STU.ISVALID = false
   if IsInGeomSet(STU.HEAD1, STU.HEAD2) then
      STU.ISVALID = true
   else
      STU.ISVALID = false
   end
end

function STU.GetValidHeadExitForPos()
   -- se TUUID non valido restituisco errore
   local ToolName = EgtTdbGetToolFromUUID( STU.TUUID)
   if ToolName == nill then
      STU.ERR = 1
      return
   end
   EgtTdbSetCurrTool( ToolName)
   local CurrToolHead = EgtTdbGetCurrToolParam( MCH_TP.HEAD)
   STU.EXIT = EgtTdbGetCurrToolParam( MCH_TP.EXIT)
   -- recupero testa predefinita per la posizione corrente
   local CurrPosHead
   for i = 1, #PositionTable do
      if PositionTable[i].TcPos == STU.TCPOS then
         CurrPosHead = PositionTable[i].Head
         break
      end
   end
   -- verifico se la testa è quella della posizione predefinita
   if CurrPosHead == CurrToolHead then
      STU.HEAD = CurrToolHead
      STU.ERR = 0
      return
   -- verifico se la testa è compatibile con quella della posizione predefinita
   elseif IsInGeomSet( CurrToolHead, CurrPosHead) then
      if UsePositionHead then
         STU.HEAD = CurrPosHead
         STU.ERR = 0
         return
      else
         STU.HEAD = CurrToolHead
         STU.ERR = 0
         return
      end
   -- la testa non è compatibile con quella della posizione predefinita
   else
      STU.HEAD = INVALIDPOS
      STU.ERR = 0
      return
   end
end

function STU.IsValidTcPosFromHead()
   STU.ISVALID = false
   for i = 1, #PositionTable do
      if PositionTable[i].TcPos == STU.TCPOS then
         if PositionTable[i].Head == STU.HEAD then
            STU.ISVALID = true
            STU.ERR = 0
            return
         elseif IsInGeomSet(PositionTable[i].Head, STU.HEAD) then
            STU.ISVALID = true
            STU.ERR = 0
            return
         end
         STU.ISVALID = false
         STU.ERR = 0
         return
      end
   end
end

function STU.GetTcPosHeadGroupFromPos()
   for i = 1, #PositionTable do
      if PositionTable[i].Pos == (POS .. tostring(STU.INDEX)) then
         STU.TCPOS = PositionTable[i].TcPos
         STU.HEAD = PositionTable[i].Head
         STU.GROUP = PositionTable[i].Group
         STU.ERR = 0
         return
      end
   end
   STU.TCPOS = INVALIDPOS
   STU.HEAD = INVALIDPOS
   STU.GROUP = INVALIDPOS
   STU.ERR = 1
end

function STU.GetPosFromTcPos()
   for i = 1, #PositionTable do
      if PositionTable[i].TcPos == STU.TCPOS then
         STU.POS = PositionTable[i].Pos
         STU.ERR = 0
         return
      end
   end
   STU.POS = INVALIDPOS
   STU.ERR = 1
end

function STU.GetGroupFromTcPos()
   for i = 1, #PositionTable do
      if PositionTable[i].TcPos == STU.TCPOS then
         STU.GROUP = PositionTable[i].Group
         STU.ERR = 0
         return
      end
   end
   STU.GROUP = INVALIDPOS
   STU.ERR = 1
end

function STU.GetHeadFromPos()
   for i = 1, #PositionTable do
      if PositionTable[i].Pos == (POS .. tostring(STU.INDEX)) then
         STU.HEAD = PositionTable[i].Head
         STU.ERR = 0
         return
      end
   end
   STU.HEAD = INVALIDPOS
   STU.ERR = 1
end

function STU.GetTcPosFromPos()
   for i = 1, #PositionTable do
      if PositionTable[i].Pos == (POS .. tostring(STU.INDEX)) then
         STU.TCPOS = PositionTable[i].TcPos
         STU.ERR = 0
         return
      end
   end
   STU.TCPOS = INVALIDPOS
   STU.ERR = 1
end
