#copyright Fredo6 - July 2022

module ::Traductor

class Scanning

@@macid = G6.machine_id(3)
@@new_check = defined?(LibFredo6.new_check)

#List of plugins
@@lst_plugin_dates = []
@@lst_plugin_dates += ['JointPushPull', 'RoundCorner', 'FredoScale', 'ToolsOnSurface', 'Curviloft', 'VisuHole', 'TopoShaper', 'Curvizard']
if @@new_check
 @@lst_plugin_dates += ['FredoCorner', 'FredoPortrait', 'FredoGhost', 'FredoSpline']
end 
@@hsh_new_plugins = {}
@@hsh_new_plugins['ThruPaint'] = [Time.local(2024, 6, 1), 30]
####@@hsh_new_plugins['Axyz'] = [Time.local(2025, 12, 31), 30]
['FredoBatch', 'ElevationProfile', 'FredoSketch', 'LayOps', 'MatSim', 'Pic2Shape'].each do |p|
 @@hsh_new_plugins[p] = [Time.local(2025, 9, 1), 30]
end
@@hsh_new_plugins['Animator'] = [Time.local(2025, 10, 1), 30]
####@@hsh_new_plugins['FredoSection'] = [Time.local(2026, 4, 1), 30]
@@hsh_new_plugins['FredoBend'] = [Time.local(2026, 7, 1), 30]

@@lst_plugin_dates.concat @@hsh_new_plugins.keys

@@lst_plugin_dates_low = @@lst_plugin_dates.collect { |a| a.downcase }

def self.plugin_list ; @@lst_plugin_dates ; end

def self.string_scan(s1, s2, n)
 retval = (Math.sqrt((n + 34) * (n + 36)) * (n / 1.86) + 479).round
 
 #puts "\nSTRING SCAN S1 = #{s1} - S2 = #{s2.inspect} - N = #{n} - retval = #{retval}"
 
 #Logic for testing
 if s2 == 'PL'
  plugin_name = s1
  #puts "VIA PL #{plugin_name}"
 elsif ['marshall'].include?(s1.downcase)
  return [retval, nil]
 elsif ['keymanager'].include?(s1.downcase)
  ###return [-1, nil] unless s2
  return [retval, nil] unless s2
  s2 = s2.downcase
  return [retval, nil] if s2 =~ /\Abody_lib6/i
  iplug = @@lst_plugin_dates_low.index { |a| s2.include?(a) }
  return [retval, nil] unless iplug
  plugin_name = @@lst_plugin_dates[iplug]
  #puts " ----------->> #{s1} s2 = #{s2} - Plug = #{plugin_name}"
 else
  return [-1, nil] unless s2
  s2 = s2.downcase
  hsh_plug = Hash.new(-1)
  hsh_plug['faceselector;body_fredotools__facetriangulator'] = 0
  hsh_plug['faceselector;jointpushpulltool'] = 'JointPushPull'
  hsh_plug['selmode;roundcorner_tool'] = 'RoundCorner'
  hsh_plug['parametermanager;fredocornertool'] = 'FredoCorner'
  hsh_plug['standardselectiontool;fredoscaleselection'] = 'FredoScale'
  hsh_plug['edgepicker;curvilofttool'] = 'Curviloft'
  hsh_plug['edgepicker;curvizardtool'] = 'Curvizard'
  hsh_plug['edgepicker;toposhapertool'] = 'TopoShaper'
  hsh_plug['edgepicker;fredosplinetool'] = 'FredoSpline'
  hsh_plug['cameramanager;fredoportraittopdialog'] = 'FredoPortrait'
  hsh_plug['cameramanager;fredoportraittool'] = 'FredoPortrait'
  plugin_name = hsh_plug[s1.downcase + ';' + s2]
  case plugin_name
  when 0
   return [retval, nil]
  when -1
   iplug = @@lst_plugin_dates_low.index { |a| s2.include?(a) }
   plugin_name = (iplug) ? @@lst_plugin_dates[iplug] : nil
   return [0, plugin_name]
  end
  #puts "VIA S1S2 #{plugin_name}"
 end
 ##puts "PLUGIN NAME = #{plugin_name}"
 
 #Check if the machine id has been hacked
 return [0, nil] unless @@macid == G6.machine_id(3)
 
 #Check the date of starting
 t, = @@hsh_new_plugins[plugin_name]
 if t && (Time.now.to_f - t.to_f) < 0
  return [retval, nil]
 end
 
 #Checking the trial period and license
 stamp_result = nil
 #puts "\n----CALLING TEM MENU CODE #{plugin_name}"
 hmod = Traductor::Plugin.hsh_plugins[plugin_name].get_module
 hmod::MYPLUGIN.get_menucodes { |stamp| stamp_result = stamp }
 tnow = Time.now.to_f.round
 stamp_ref = tnow**1.0477 + tnow**1.0478
 #puts "    STAMP MENU CODE = #{stamp_result} == #{stamp_ref} - Now = #{Time.now.to_f}"
 
 #Plugin non licensed or expired
 if !stamp_result || (stamp_result.round - stamp_ref.round).abs > 5
  #puts "PAS BON LICENSED"
  UI.start_timer(0) { Sketchup.active_model.select_tool(nil) }
  UI.start_timer(1) do
   Traductor::ChromiumWdlg.close_from_key(plugin_name)
   Traductor::Wdlg.close_from_key(plugin_name)
  end 
  return [0, plugin_name]
 end 

 #Return the secret formula
 [retval, plugin_name]
end

end #class Scanning

end #module Traductor

