-- tipAnywhere 
-- 
-- by Defender
SpecializationUtil.registerSpecialization("tipAnywhere", "tipAnywhere", g_modsDirectory.."/zzz_tipAnywhere/tipAnywhere.lua");
--table.insert(VehicleTypeUtil.vehicleTypes.trailer.specializations, SpecializationUtil.getSpecialization("tipAnywhere"));
for k,v in pairs(VehicleTypeUtil.vehicleTypes) do  
  if v~=nil then
    for a=1, table.maxn(v.specializations) do
      local s = v.specializations[a];
      if s ~= nil then
        if s == SpecializationUtil.getSpecialization("trailer") then
          print("tipAnywhere pridan do "..tostring(v.name));
          table.insert(v.specializations, SpecializationUtil.getSpecialization("tipAnywhere"));
        end;
      end;
    end;
  end;
end;

print("tipAnywhere specialization registred");


tipAnywhereListener = {};

function tipAnywhereListener:loadMap(name)   
--  language:loadTexts(getUserProfileAppPath().."mods/zzz_tipAnywhere/modDesc.xml");
  if g_currentMission.tipAnywhereTriggers == nil then
    print("tipAnywhere loading ...");
    g_currentMission.tipAnywhereTriggers = {};
    local siloSaveDir = g_currentMission.missionInfo.savegameDirectory;
    local siloSaveXML = siloSaveDir .. "/tipAnywhere.xml";
    if g_currentMission.missionStats.playTime == 0 then
      local existDir0 = io.open (siloSaveXML, "r");
      if existDir0 ~= nil then
        local siloFile = io.open (siloSaveXML, "w");
        if siloFile ~= nil then
          siloFile:write('<?xml version="1.0" encoding="utf-8" standalone="no" ?>\n');
          siloFile:write('<tipAnywhere>\n');      
          siloFile:write("</tipAnywhere>");
          siloFile:close();
        end;
      end;
    else
      local existDir = io.open (siloSaveXML, "r");
      if existDir ~= nil then
        self.xmlFile = loadXMLFile("TAConfig", siloSaveXML);
        local keyI = 0;
        while true do
          local key = string.format("tipAnywhere.tipAnywhereTrigger(%d)", keyI);
          local fillType = getXMLString(self.xmlFile, key.."#fillType");
          if fillType == nil then
            break;
          end;      
          local x, y, z = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#position"));
          local rx, ry, rz = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#rotation"));
          local fillLevel = getXMLFloat( self.xmlFile, key.."#fillLevel");      
          local fname = getUserProfileAppPath().."mods/zzz_tipAnywhere/hromada/hromada.i3d";
          local hromada = loadI3DFile(fname);
          setTranslation(hromada, x, y, z);
          setRotation(hromada, rx, ry, rz);         
          link(getRootNode(), hromada);
          for i=1, table.maxn(g_currentMission.tipAnywhereTriggers) do
            local tr = g_currentMission.tipAnywhereTriggers[i];
            if tr~=nil then
              if tr.triggerId ==  Utils.indexToObject(hromada,"0|1") then
                print("tr.triggerId="..tostring(tr.triggerId)); 
                tr:setFillLevel(fillLevel, Fillable.fillTypeNameToInt[fillType]);
              end;
            end;
          end;
          keyI = keyI + 1;
        end;     
      end;
    end;        
  end;
end;

function tipAnywhereListener:deleteMap()
  if g_currentMission.tipAnywhereTriggers ~= nil then
    for i=1, table.maxn(g_currentMission.tipAnywhereTriggers) do
      local t = g_currentMission.tipAnywhereTriggers[i];
      if t ~= nil then
        delete(t.hromadaId);
        g_currentMission.tipAnywhereTriggers[i] = nil;
      end;
    end;
    g_currentMission.tipAnywhereTriggers = nil;  
  end;
end;

function tipAnywhereListener:mouseEvent(posX, posY, isDown, isUp, button)
end;

function tipAnywhereListener:keyEvent(unicode, sym, modifier, isDown)
end;

function tipAnywhereListener:update(dt)
end;

function tipAnywhereListener:draw()
  if g_currentMission.tipAnywhereTriggers ~= nil then
    for i=1, table.maxn(g_currentMission.tipAnywhereTriggers) do
      local tr = g_currentMission.tipAnywhereTriggers[i];
      if tr~=nil then
        tr:draw();
      end;
    end;
  end;
end;


addModEventListener(tipAnywhereListener);