#
# JF North Angle Slider
#
# Adjust the North Angle using an HTML range slider.
#
# Author: Jim Foltz <jim.foltz@gmail.com>
#
# History:
#
#   * 2016-09-25.1 - Initial Release
#
#   * 2016-09-26.1
#     - Added Shadow Display Toggle button
#
unless defined?(JF::NorthAngleSlider)
   module JF
      module NorthAngleSlider
         menu = defined?(JF.menu) ? JF.menu : UI.menu('Plugins')
         menu.add_item('JF North Angle Slider') { show_north_angle_slider }

         def self.show_north_angle_slider
            if defined?(UI::HtmlDialog)
               @dlg = UI::HtmlDialog.new(
                  dialog_title: 'JF North Angle Slider',
                  preferences_key: 'JF\\NorthAngleSlider17',
                  scrollable: true,
                  resizable: true,
                  width: 200,
                  height: 200,
                  left: 100,
                  top: 100,
                  min_width: 50,
                  min_height: 50,
                  max_width: 1000,
                  max_height: 1000,
                  style: UI::HtmlDialog::STYLE_DIALOG
               ) # "JF North Angle Slider", false, 'JF\North_Angle_Slider', 500, 250)
            else
               @dlg = UI::WebDialog.new('JF North Angle Slider', false, 'JF\\NorthAngleSlider16', 500, 250)
            end

            html = File.dirname(__FILE__) + '/jf_north_angle_slider/dialog.html'
            @dlg.set_file(html)

            @dlg.add_action_callback('ready') do |_d, _a|
               si = Sketchup.active_model.shadow_info
               si['DisplayNorth'] = true
               UI.start_timer(0.2, false) do
                  scr = "update_ui('" + si['NorthAngle'].to_i.to_s + "', '" + si['DisplayShadows'].to_s + "')"
                  @dlg.execute_script(scr)
               end
            end

            @dlg.add_action_callback('pute') do |_d, a|
               puts a
            end

            @dlg.add_action_callback('blur') do |_d, _a|
               #@dlg.close
            end

            @dlg.add_action_callback('slider_updated') do |_d, a|
               Sketchup.active_model.shadow_info['NorthAngle'] = a.to_i
            end

            @dlg.add_action_callback('checkbox_updated') do |d, a|
               Sketchup.active_model.shadow_info['DisplayShadows'] = (a == 'true' || a == true)
            end

            if defined?(UI::HtmlDialog)
               @dlg.set_on_closed do
                  Sketchup.active_model.shadow_info['DisplayNorth'] = false
               end
            else
               @dlg.set_on_close do
                  Sketchup.active_model.shadow_info['DisplayNorth'] = false
               end
            end

            @dlg.show
         end
      end
   end
end
