#requires
require 'watir'
#email stuff
require 'net/smtp'

#used to handle javascript popups
require 'watir/contrib/enabled_popup'
require 'watir/WindowHelper'
#
# Method to handle javascript popups.
#
require 'thread'
#
require 'win32ole'
#
def startClicker( button , waitTime = 3)
  w = WinClicker.new
  longName = @@ie.dir.gsub("/" , "\\" )
  shortName = w.getShortFileName(longName)
  c = "start ruby #{shortName}\\watir\\clickJSDialog.rb #{button} #{waitTime} "   
  puts "Starting #{c}"
  w.winsystem(c)   
  w=nil
end




#includes
include Watir
@@ie = IE.new
@@ie.visible= true
#set the speed of watir
@@ie.speed = :fast
#backup 2 directories
Dir.chdir('..')
Dir.chdir('..')
#we're in windows, so replace the forwards with the backs
workingdir = Dir.getwd.to_s.gsub!('/','\\')
#Set constants for import
ORIGINAL_APP_ID = '155'
NEW_APP_ID = '200'
SCHEMA = 'MIP_FAT'
APEX_EXPORT_SCRIPTS_DIR =  workingdir +'\\Screens\\'
APPLICATION_SCRIPT = 'f155.sql'
CSS_SCRIPT = 'css.sql'
IMAGES_SCRIPT = 'f155_img.sql'
THEME_SCRIPT = 'f155_theme_101.sql'
ADID = '4550' #APEX admin identifier
WORKSPACE = 'MIP_FAT'
USER = 'ADMIN'
PASSWORD  = 'password'
DELETE_PREVIOUS_IMPORTS = false

def importpageone (p_file, p_file_type, p_charset)
  @@ie.link(:title, 'Application Builder').click
  @@ie.wait
  @@ie.button(:value, 'Import >').click
  @@ie.wait
  @@ie.file_field(:id, "P460_FILE").set(p_file)
  @@ie.select_list(:id, "P460_FILE_TYPE").select p_file_type
  @@ie.select_list(:id, "P460_FILE_CHARSET").select p_charset
  @@ie.button(:value, 'Next >').click
  @@ie.wait
  @@ie.button(:value, 'Next >').click
  @@ie.wait
end

begin 
  #Login
  @@ie.goto("http://loordv01/pls/apex/f?p=#{ADID}:1")
  @@ie.text_field(:id, "F#{ADID}_P1_COMPANY").set(WORKSPACE)
  @@ie.text_field(:id, "F#{ADID}_P1_USERNAME").set(USER)
  @@ie.text_field(:id, "F#{ADID}_P1_PASSWORD").set(PASSWORD)
  @@ie.button(:value, 'Login').click
  @@ie.wait
  #Import Application export file
  importpageone(APEX_EXPORT_SCRIPTS_DIR+APPLICATION_SCRIPT,
                    'Application, Page or Component Export',
                    'Western European Windows 1252')
  #parse page
  @@ie.select_list(:id, "P49_PARSE_AS" ).select(SCHEMA)
  @@ie.select_list(:id, "P49_BUILD_STATUS" ).select 'Run and Build Application'
  @@ie.radio(:id, "P49_NEW_FLOW_OPT_2" ).set
  @@ie.wait
  @@ie.text_field(:id, "P49_NEW_FLOW_ID").set(NEW_APP_ID)
  @@ie.button(:value, 'Install').click
  @@ie.wait
  #This button won't appear the first time the import occurs
  if @@ie.button(:value, 'Replace Existing Application '+NEW_APP_ID).exists?
    @@ie.button(:value, 'Replace Existing Application '+NEW_APP_ID).click
    @@ie.wait
  end
  #Install Supporting objects
  @@ie.radio(:id, "P2100_ACTION_0" ).set
  @@ie.button(:value, 'Next >').click
  @@ie.wait
  @@ie.button(:value, 'Install').click
  @@ie.wait
  #Install CSS

  #Need to delete the existing CSS file before installing a new one
  #have decided to just install the application for the moment.
  #back to install page start

  importpageone(APEX_EXPORT_SCRIPTS_DIR+CSS_SCRIPT,
                    'CSS Export',
                    'Western European Windows 1252')

  if DELETE_PREVIOUS_IMPORTS
    #er this code does not work from the Loordv001 box
    #it will run on JP's machine if this code is run from a
    #dos prompt - it's an issue with ruby trying to spawn a
    #new thead to look for the open dialogue box
    @@ie.link(:title, 'Manage Export Repository').click
    @@ie.wait
    @@ie.checkboxes.each { |c| c.set }
    #Click the annoying are you sure button that pops up
    startClicker("OK", 1)   
    @@ie.button(:value, 'Delete Checked').click
    @@ie.wait
  end
  @@ie.close

rescue StandardError => anyerror
#Hopefully we never get here but, we'll try and send an email if the
#script errors so we can fix it
#don't leave memory hungry instances of ie running if we get an error
@@ie.close

#build the email
from_addr = 'jamie.priest@advantica.com'
to_addr = 'jamie.priest@advanticagroup.com'
project = 'webMIP test build process'
emailtext = <<END_EMAIL
From: webMIP <#{from_addr}>
To: Jamie Priest <#{to_addr}>
Subject: #{project} automated test failure

An automated assertion failed for the project
#{project}

#{anyerror}

END_EMAIL
  Net::SMTP.start('LOMAIL01') do |smtp|
    smtp.sendmail emailtext, from_addr, to_addr
  end

end