###############################################################################
# Manage App Development Screen Scripts 
#
# License:
#   Written by David McComas, licensed under the copyleft GNU General
#   Public License (GPL).
# 
###############################################################################

require 'cosmos'
require 'cosmos/script'
Cosmos.catch_fatal_exception do
  require 'cosmos/tools/cmd_sender/cmd_sender'
  require 'cosmos/tools/tlm_viewer/screen'
  require 'cosmos/tools/tlm_viewer/tlm_viewer'
end
require 'osk_global'
require 'osk_system'
require 'create_app_screen'
require 'app_template'

################################################################################
## Screen Commands
################################################################################

def mng_app_dev(screen, cmd)

   if (cmd == "CREATE_APP")
      #spawn("java -jar #{Osk::CREATE_APP_DIR}/CreateApp.jar", :chdir => "#{Osk::CREATE_APP_DIR}")
      build_create_app_screen
      
      display("CFS_KIT #{File.basename(Osk::CREATE_APP_SCR_FILE,'.txt')}")
   elsif (cmd == "EDIT_CMAKE")
      Cosmos.run_process("ruby lib/OskTxtFileViewer -f '#{Osk::CFS_CMAKE_DIR}/#{Osk::CPU1_TARGET_FILE}'")
   elsif (cmd == "EDIT_STARTUP")
      Cosmos.run_process("ruby lib/OskTxtFileViewer -f '#{Osk::CFS_CMAKE_DIR}/#{Osk::CPU1_STARTUP_FILE}'")
   elsif (cmd == "BUILD_CFS")
      spawn("xfce4-terminal --default-working-directory=""#{Osk::OSK_CFS_DIR}"" --execute make install""")
      spawn("xfce4-terminal --default-working-directory=""#{Osk::OSK_CFS_DIR}"" --execute ./cmake.sh""")
   elsif (cmd == "STOP_CFS_SERVER")
   
      Osk::System.stop_cfs
      Osk::System.stop_cmd_tlm_server
      
   elsif (cmd == "START_CFS_SERVER") 
   
      spawn("ruby #{Osk::COSMOS_CMD_TLM_SRV}")            
      wait 4
      Osk::System.start_cfs

   else
      prompt("Error in screen definition file. Undefined command sent to app_create()")
   end
   
end # mng_app_dev()

def build_create_app_screen


   t = Time.new 
   time_stamp = "_#{t.year}_#{t.month}_#{t.day}_#{t.hour}#{t.min}#{t.sec}"
   
   title_to_dir = {}
   
   begin

      create_app_scr_file = File.join(Osk::SCR_DIR,Osk::CREATE_APP_SCR_FILE)
   
      json_config_file = File.join(Osk::TOOLS_DIR,'create-app',Osk::CREATE_APP_JSON_FILE)

      config = read_config_file(json_config_file)  

      version = config[JSON_VERSION]

      template_combo = ""
      dirs = get_default_dirs(config)
      Dir.foreach(dirs["TEMPLATE"]) do |item|

         # There are no constraints on template directory and file contents 
         # other than if a directory has a CREATE_APP_TEMPLATE_FILE then it
         # will be processed and it must follow the template rules
         next if item == '.' or item == '..'
         template_dir  = File.join(dirs["TEMPLATE"],item)
         if File.directory?(template_dir)

            template_file  = File.join(template_dir,Osk::CREATE_APP_TEMPLATE_FILE)
            if File.exist?(template_file)
               title = AppTemplate.get_title(template_dir)
               if title != ""
                  template_combo << "'#{title}' "
                  title_to_dir[title] = template_dir
               end
            end

         end # if directory
      end # Directory loop 

      create_app_set_template_dir(title_to_dir)

   create_app_scr_txt = "
   ###############################################################################
   # Create App Screen
   #
   # Notes:
   #   1. Do not edit this file because it is automatically generated and your
   #      changes will not be saved.
   #   2. File created by mng_app_dev_screen.rb on #{time_stamp}
   #
   # License:
   #   Written by David McComas, licensed under the copyleft GNU General Public
   #   License (GPL). 
   #
   ###############################################################################

   SCREEN AUTO AUTO 0.5
   GLOBAL_SETTING BUTTON BACKCOLOR 112 199 204
  
   TITLE 'Create App Version #{version}'
     SETTING BACKCOLOR 254 156 255
     SETTING TEXTCOLOR black

   VERTICAL

   MATRIXBYCOLUMNS 4
     LABEL \"App Template\"
     NAMED_WIDGET template COMBOBOX #{template_combo}
     BUTTON 'Template Info' 'require \"#{Cosmos::USERPATH}/config/targets/CFS_KIT/lib/create_app_screen.rb\"; create_app_display_template_info(self)'
     BUTTON 'Create App' 'require \"#{Cosmos::USERPATH}/config/targets/CFS_KIT/lib/create_app_screen.rb\"; create_app_execute(self)'
     SETTING BACKCOLOR 0 200 0
   END # Matrix

   HORIZONTALLINE

   LABEL \"Generate application or library 'hello world' code from a template. Additional artifacts may be generated. See <template info> for details.\"
   LABEL \"Using template directory #{dirs["TEMPLATE"]}.\"
   SETTING TEXTCOLOR black
   LABEL \"  \"
   NAMED_WIDGET Line1  LABEL \"  1. Select a template from the drop down menu. Click <Template Info> to get a description of the template.\"  
   SETTING TEXTCOLOR 0 0 153
   NAMED_WIDGET Line2  LABEL \"  2. Modify the cFS and COSMOS target directories below if necessary\"
   SETTING TEXTCOLOR 0 0 153
   NAMED_WIDGET Line3  LABEL \"  3. Click <Create App> to generate the code\"
   SETTING TEXTCOLOR 0 0 153
   LABEL \"  \"
  
   HORIZONTALLINE
  
   MATRIXBYCOLUMNS 3
     LABEL 'cFS Target Directory'
     SETTING HEIGHT 20
     BUTTON 'Show Default' 'require \"#{Cosmos::USERPATH}/config/targets/CFS_KIT/lib/create_app_screen.rb\"; create_app_manage_dir(self,\"SHOW_DEFAULT_CFS\")'
     BUTTON 'Browse'       'require \"#{Cosmos::USERPATH}/config/targets/CFS_KIT/lib/create_app_screen.rb\"; create_app_manage_dir(self,\"BROWSE_CFS\")'
   END # Matrix
   NAMED_WIDGET cfs_target_dir TEXTFIELD 256 ""
     SETTING HEIGHT 20

   MATRIXBYCOLUMNS 3
     LABEL 'COSMOS Target Directory'
     SETTING HEIGHT 20
     BUTTON 'Show Default' 'require \"#{Cosmos::USERPATH}/config/targets/CFS_KIT/lib/create_app_screen.rb\"; create_app_manage_dir(self,\"SHOW_DEFAULT_COSMOS\")'
     BUTTON 'Browse'       'require \"#{Cosmos::USERPATH}/config/targets/CFS_KIT/lib/create_app_screen.rb\"; create_app_manage_dir(self,\"BROWSE_COSMOS\")'
   END # Matrix
   NAMED_WIDGET cosmos_target_dir TEXTFIELD 256
     SETTING HEIGHT 20

   END # Vertical
   "
      # Always overwrite the screen file
      File.open(create_app_scr_file,"w") { |f| f.write (create_app_scr_txt) }


   rescue Exception => e
      puts e.message
      puts e.backtrace.inspect  
   end

end # build_create_app_screen()

