-- 2023/11/13
-- Gestione attrezzaggio per Essetre-PF1500rl

-- 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 (con o senza TC2)
local PositionTable = {}
local bTc2Active = ( EgtGetFirstNameInGroup( EgtGetBaseId( 'Base'), 'TC2') ~= nil)
local SecondSaw = ( EgtGetHeadId( 'H16') ~= nil)
local SecondChain = ( EgtGetFirstNameInGroup( EgtGetBaseId( 'Base'), 'TC4') ~= nil)
local TcSpecialTools = ( EgtGetFirstNameInGroup( EgtGetBaseId( 'Base'), 'TC5') ~= nil)
local TcAggreBladeUnder  = ( EgtGetFirstNameInGroup( EgtGetBaseId( 'Base'), 'TCS') ~= nil)
local DrillingAggregate = ( EgtGetHeadId( 'H14') ~= nil)

local bFullOpt = bTc2Active and FirstSaw and SecondSaw and SecondChain and TcSpecialTools and TcAggreBladeUnder and DrillingAggregate  -- se ha tutti gli optional attivati
if bFullOpt then
   PositionTable = {{Pos = "Pos1",  TcPos = "T1",  Head = "H12", Group = "G1"},  -- TC 1
                    {Pos = "Pos2",  TcPos = "T2",  Head = "H11", Group = "G1"},
                    {Pos = "Pos3",  TcPos = "T3",  Head = "H11", Group = "G1"},
                    {Pos = "Pos4",  TcPos = "T4",  Head = "H11", Group = "G1"},
                    {Pos = "Pos5",  TcPos = "T5",  Head = "H11", Group = "G1"},
                    {Pos = "Pos6",  TcPos = "T6",  Head = "H11", Group = "G1"},
                    {Pos = "Pos7",  TcPos = "T7",  Head = "H11", Group = "G1"},
                    {Pos = "Pos8",  TcPos = "T8",  Head = "H11", Group = "G1"},
                    {Pos = "Pos9",  TcPos = "T9",  Head = "H11", Group = "G1"},
                    {Pos = "Pos10",  TcPos = "T10", Head = "H11", Group = "G1"},
                    {Pos = "Pos11",  TcPos = "T11", Head = "H11", Group = "G1"},
                    {Pos = "Pos12",  TcPos = "T12", Head = "H11", Group = "G1"},
                    {Pos = "Pos13",  TcPos = "T13", Head = "H11", Group = "G1"},
                    {Pos = "Pos14", TcPos = "T101", Head = "H13", Group = "G2"},  -- motosega
                    {Pos = "Pos15", TcPos = "T111", Head = "H13", Group = "G2"},  -- motosega 2
                    {Pos = "Pos16", TcPos = "T121", Head = "H14", Group = "G2"},  -- utensile speciale
                    {Pos = "Pos17", TcPos = "T21", Head = "H16", Group = "G3"},   -- TC 2
                    {Pos = "Pos18", TcPos = "T22", Head = "H11", Group = "G3"},
                    {Pos = "Pos19", TcPos = "T23", Head = "H11", Group = "G3"},
                    {Pos = "Pos20", TcPos = "T24", Head = "H11", Group = "G3"},
                    {Pos = "Pos21", TcPos = "T25", Head = "H11", Group = "G3"},
                    {Pos = "Pos22", TcPos = "T26", Head = "H11", Group = "G3"},
                    {Pos = "Pos23", TcPos = "T27", Head = "H11", Group = "G3"},
                    {Pos = "Pos24", TcPos = "T28", Head = "H11", Group = "G3"},
                    {Pos = "Pos25", TcPos = "T29", Head = "H11", Group = "G3"},
                    {Pos = "Pos26", TcPos = "T30", Head = "H11", Group = "G3"},
                    {Pos = "Pos27", TcPos = "T201", Head = "H22", Group = "G4"},  -- lama con aggregatop da sotto
                    {Pos = "Pos28", TcPos = "T220", Head = "H21", Group = "G4"},  -- TC margherita
                    {Pos = "Pos29", TcPos = "T221", Head = "H21", Group = "G4"},
                    {Pos = "Pos30", TcPos = "T222", Head = "H21", Group = "G4"},
                    {Pos = "Pos31", TcPos = "T223", Head = "H21", Group = "G4"},
                    {Pos = "Pos32", TcPos = "T224", Head = "H21", Group = "G4"},
                    {Pos = "Pos33", TcPos = "T225", Head = "H21", Group = "G4"},
                    {Pos = "Pos34", TcPos = "T226", Head = "H21", Group = "G4"},
                    {Pos = "Pos35", TcPos = "T227", Head = "H21", Group = "G4"},
                    {Pos = "Pos36", TcPos = "T228", Head = "H21", Group = "G4"},
                    {Pos = "Pos37", TcPos = "T229", Head = "H21", Group = "G4"},
                    {Pos = "Pos38", TcPos = "T230", Head = "H21", Group = "G4"},
                    {Pos = "Pos39", TcPos = "T301", Head = "H38", Group = "G5"}}
elseif bTc2Active and SecondSaw and SecondChain and TcSpecialTools and TcAggreBladeUnder and DrillingAggregate then
   PositionTable = {{Pos = "Pos1",  TcPos = "T1",  Head = "H11", Group = "G1"},  -- TC 1
                    {Pos = "Pos2",  TcPos = "T2",  Head = "H11", Group = "G1"},
                    {Pos = "Pos3",  TcPos = "T3",  Head = "H11", Group = "G1"},
                    {Pos = "Pos4",  TcPos = "T4",  Head = "H11", Group = "G1"},
                    {Pos = "Pos5",  TcPos = "T5",  Head = "H11", Group = "G1"},
                    {Pos = "Pos6",  TcPos = "T6",  Head = "H11", Group = "G1"},
                    {Pos = "Pos7",  TcPos = "T7",  Head = "H11", Group = "G1"},
                    {Pos = "Pos8",  TcPos = "T8",  Head = "H11", Group = "G1"},
                    {Pos = "Pos9",  TcPos = "T9",  Head = "H11", Group = "G1"},
                    {Pos = "Pos10",  TcPos = "T10", Head = "H11", Group = "G1"},
                    {Pos = "Pos11",  TcPos = "T11", Head = "H11", Group = "G1"},
                    {Pos = "Pos12",  TcPos = "T12", Head = "H11", Group = "G1"},
                    {Pos = "Pos13",  TcPos = "T13", Head = "H11", Group = "G1"},
                    {Pos = "Pos14", TcPos = "T101", Head = "H13", Group = "G2"},  -- motosega
                    {Pos = "Pos15", TcPos = "T111", Head = "H13", Group = "G2"},  -- motosega 2
                    {Pos = "Pos16", TcPos = "T121", Head = "H14", Group = "G2"},  -- utensile speciale
                    {Pos = "Pos17", TcPos = "T21", Head = "H16", Group = "G3"},   -- TC 2
                    {Pos = "Pos18", TcPos = "T22", Head = "H11", Group = "G3"},
                    {Pos = "Pos19", TcPos = "T23", Head = "H11", Group = "G3"},
                    {Pos = "Pos20", TcPos = "T24", Head = "H11", Group = "G3"},
                    {Pos = "Pos21", TcPos = "T25", Head = "H11", Group = "G3"},
                    {Pos = "Pos22", TcPos = "T26", Head = "H11", Group = "G3"},
                    {Pos = "Pos23", TcPos = "T27", Head = "H11", Group = "G3"},
                    {Pos = "Pos24", TcPos = "T28", Head = "H11", Group = "G3"},
                    {Pos = "Pos25", TcPos = "T29", Head = "H11", Group = "G3"},
                    {Pos = "Pos26", TcPos = "T30", Head = "H11", Group = "G3"},
                    {Pos = "Pos27", TcPos = "T201", Head = "H22", Group = "G4"},  -- lama con aggregatop da sotto
                    {Pos = "Pos28", TcPos = "T220", Head = "H21", Group = "G4"},  -- TC margherita
                    {Pos = "Pos29", TcPos = "T221", Head = "H21", Group = "G4"},
                    {Pos = "Pos30", TcPos = "T222", Head = "H21", Group = "G4"},
                    {Pos = "Pos31", TcPos = "T223", Head = "H21", Group = "G4"},
                    {Pos = "Pos32", TcPos = "T224", Head = "H21", Group = "G4"},
                    {Pos = "Pos33", TcPos = "T225", Head = "H21", Group = "G4"},
                    {Pos = "Pos34", TcPos = "T226", Head = "H21", Group = "G4"},
                    {Pos = "Pos35", TcPos = "T227", Head = "H21", Group = "G4"},
                    {Pos = "Pos36", TcPos = "T228", Head = "H21", Group = "G4"},
                    {Pos = "Pos37", TcPos = "T229", Head = "H21", Group = "G4"},
                    {Pos = "Pos38", TcPos = "T230", Head = "H21", Group = "G4"},
                    {Pos = "Pos39", TcPos = "T301", Head = "H38", Group = "G5"}}
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
