# copyright (c) 2012 BuildEdge

# the main setup point for the plugin.



# all our requires
Sketchup.require("#{File.dirname(__FILE__)}/buildedge_lib_includes")

#The string table to load - change for differenct languages
# en-US is the default.  Add more codes as we support more languages.
if(Sketchup.os_language=="en-US")
    Sketchup.require("#{BuildEdge.root_dir}/strings_en_us")
else
    Sketchup.require("#{BuildEdge.root_dir}/strings_en_us")
end

Sketchup.require("#{BuildEdge.root_dir}/init_dll")

Sketchup.require("#{BuildEdge.utils_dir}/debug_puts")
Sketchup.require("#{BuildEdge.utils_dir}/keyvaluepair")
Sketchup.require("#{BuildEdge.utils_dir}/wall_product")
Sketchup.require("#{BuildEdge.utils_dir}/settings")
Sketchup.require("#{BuildEdge.utils_dir}/messenger")
Sketchup.require("#{BuildEdge.utils_dir}/tol_math")
Sketchup.require("#{BuildEdge.utils_dir}/heel_calculator")

Sketchup.require("#{BuildEdge.logger_dir}/logger")
Sketchup.require("#{BuildEdge.logger_dir}/log_manager")

Sketchup.require("#{BuildEdge.json_dir}/lexer")

Sketchup.require("#{BuildEdge.units_dir}/angle")
Sketchup.require("#{BuildEdge.units_dir}/slope")
Sketchup.require("#{BuildEdge.units_dir}/unitmanager")

BuildEdgeSketchup::SetSketchUpVersion(Sketchup.version)

# We can now make our one and only Messenger and Controller
module BuildEdge
    # global operations manager
    @SUSmartOpInstance = SketchupSmartOperation.new
    @SUOperationStack = OperationStack.new
    @ModifiedEntityUpdater = ModifiedEntityUpdater.new
    @DeletedEntityUpdater = DeletedEntityUpdater.new

    class << self
        attr_accessor :SUSmartOpInstance
        attr_accessor :SUOperationStack
        attr_accessor :ModifiedEntityUpdater
        attr_accessor :DeletedEntityUpdater
    end

    path = nil
    begin
        path = BuildEdge::LogManager.get_next_log_path
    rescue => err
        UI.messagebox(BuildEdge::Strings["BuildEdge_Plan_could_not_be_loaded"] + "\n" + err.message, MB_MULTILINE)
        #bail completely
        raise err
    end
    # init logging
    be_plan_log_file = File.open(path, File::WRONLY | File::CREAT)
    Log = BuildEdgeLogger.new(be_plan_log_file)
    Log.info(LogManager.make_log_header(path))
    LogManager.roll_logs

    Messenger_Instance = BuildEdge::Messenger.new
    BuildEdge_BIM_Controller = BuildEdgeBimController.new

end

appObserver = BuildEdge::SketchupAppObserver.new
appObserver.setupId(Sketchup.active_model)

Sketchup.add_observer(appObserver)
Sketchup.active_model.selection.add_observer(BuildEdge::SketchUpSelectionObserver.new)
Sketchup.active_model.add_observer(BuildEdge::SketchUpModelObserver.new)
Sketchup.active_model.entities.add_observer(BuildEdge::SketchUpEntitiesObserver.new)

# setup menus and/or tool bar
menu = UI.menu("Plugins").add_submenu(BuildEdge::Strings["BuildEdge"])
toolbar = UI::Toolbar.new BuildEdge::Strings["BuildEdge"]

if(menu != nil && toolbar != nil)
     # The toolbar button
     cmd = UI::Command.new(BuildEdge::PLUGIN_NAME) {
        # Check and let users know
        BuildEdgeSketchup::Initialize(BuildEdge.root_dir)

        BuildEdge::BuildEdge_BIM_Controller.set_auth(BuildEdgeSketchup::IsAuthorized())

        if(BuildEdge::BuildEdge_BIM_Controller.is_authorized)
            BuildEdge::Log.info("Check Security PASS...")
            BuildEdge.run_buildedge_bim_controller
        else
            BuildEdge::Log.info("Check Security FAIL...")
            UI::messagebox(BuildEdge::Strings["BuildEdge_Plan_could_not_be_loaded"] + " " + BuildEdge::Strings["License_not_activated"])
        end
     }
     cmd.small_icon = "#{BuildEdge.images_dir}/buildedge24.png"
     cmd.large_icon = "#{BuildEdge.images_dir}/buildedge24.png"
     cmd.tooltip = BuildEdge::PLUGIN_NAME
     cmd.status_bar_text = BuildEdge::PLUGIN_NAME
     cmd.menu_text = BuildEdge::PLUGIN_NAME

     menu.add_item cmd
     toolbar = toolbar.add_item cmd
     toolbar.show

    if(BuildEdge::DEBUG)
        cmd = UI::Command.new("BuildEdgeDebug") {
            BuildEdge.show_debug_window
        }
        cmd.menu_text = "Debug"
        menu.add_item cmd
    end

else
    UI.messagebox BuildEdge::Strings["BuildEdge_Plan_could_not_be_loaded"]
end
