
#Copyright © 2024 – WildGate Consultancy Solutions Limited – all Rights Reserved
#Proof of concept to constantly capture image from mapserver
#
#13 Mar 24 - based on CaptureViaGarrisonTestV414Feb24-Test.py
#
#
#
#
#
#
#
#
#
#
#
#
#

#and adds link to map and opens map
#
# to run with streamlit  - streamlit run path-to-file.py
# then  open on localhost:8501

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

from selenium.webdriver.chrome.service import Service as ChromeService  # added 25/1/24
from webdriver_manager.chrome import ChromeDriverManager # added 25/1/24

from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder

import streamlit as st # added 30 Jan 24

import time
from time import sleep
import cv2

#url = "https://www.google.com/"
#url = "https://freeboard.io/"                                     
#url = "http://209.38.235.136:8501"
#url = "http://134.209.236.202:1880/tak-map"
#url1 = "http://134.209.236.202:1880/Secondmap"
#url = "https://live.garrison-ultra.com/login/"
#url= "https://002158.garrison-ultra.com/"
#target = "http://134.209.236.202:1880/tak-map"
url = "http://wildgate.org/LeafletFollower/"


options = Options()
#options.headless = True  # remed out 25/1/24

options.add_argument("--headless")  # added 25/1/24
options.add_argument("window-size=1920,1080") # remed out 25/1/24
options.add_argument('--no-sandbox')  # added 25/1/24
options.add_argument('--disable-dev-shm-usage')  # added 25/1/24
options.add_experimental_option("useAutomationExtension", False) #added 6 Feb 24 - removes "Chrome is being controlled ..."
options.add_experimental_option("excludeSwitches",["enable-automation"])  #added 6 Feb 24 - removes "Chrome is being controlled ..."

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)  # added 25/1/24

#open the webpage first , do this only once to keep open, and delay 12 secs to alow full page
#to form 
print('got to here')
driver.get(url)
print('and got to here')
#driver1.get(url1)
sleep(4)

#From main window select logon windon , entery key and submit
print('got to here 2')
curwin = driver.current_window_handle
print (curwin)
list = driver.window_handles
print (list)

#driver.switch_to.default_content()

#driver.switch_to.window(list[1])
driver.switch_to.window(curwin)
driver.maximize_window()  # remed 8/2/24
#driver.set_window_size(1920, 1080) # added 8 Feb 24

#logon = driver.find_element(By.CLASS_NAME, "field._1c4e2w1")

#print(logon)
print('got to here')

# for testing only
#attrs=[]
#for attr in logon.get_property('attributes'):
    #attrs.append([attr['name'], attr['value']]) # original
#    attrs.append([attr['name']])
#print(attrs)

#add logon detail 
#logon.click()  # this and below works , then need to click button
#logon.send_keys("kzVgqNQkQvuHYaup303POKjWuvfj-E8vlkQoKvjkbVSkH5tPH5ZFXJe0MTQVFz0A") # updated Feb 24

#find and click submit
#submit = driver.find_element(By.CLASS_NAME, "button._1c4e2w1")
#submit.click()

#driver.switch_to.default_content() ###     move back up             ###
print('waiting for map')
sleep(8)
###          maximize window

driver.save_screenshot('test1.png')

try:
    action = ActionBuilder(driver)    # added 8 Feb 24
    action.pointer_action.move_to_location(700, 540)  # added 8 Feb 24
    action.pointer_action.click()  # added 8 Feb 24
    action.perform()  # added 8 Feb 24
    print('moved mouse to click Advanced')
except:
    print(' not able to click advanced')

driver.save_screenshot('test2.png')


try:
    
    action = ActionBuilder(driver)    # added 8 Feb 24
    action.pointer_action.move_to_location(700, 721)  # added 8 Feb 24
    action.pointer_action.click()  # added 8 Feb 24
    action.perform()  # added 8 Feb 24
    print('moved and clicked to proceeed to wildgate ')
except:
    print(' unable to proceed')
    
    
driver.save_screenshot('test3.png')

#action = ActionBuilder(driver)    # added 8 Feb 24
#action.pointer_action.move_to_location(1769, 930)  # added 8 Feb 24
#action.pointer_action.click()  # added 8 Feb 24
#action.perform()  # added 8 Feb 24
#sleep(2)

###    then move and click sysnc on and acknowldge      ###
#action = ActionBuilder(driver)    # added 8 Feb 24
#action.pointer_action.move_to_location(1770, 930)  # added 8 Feb 24
#action.pointer_action.click()  # added 8 Feb 24
#action.perform()  # added 8 Feb 24
#print('activate sync')
#sleep(1)

#action = ActionBuilder(driver)  # added 8 Feb 24
#action.pointer_action.move_to_location(1340, 640)  # added 8 Feb 24
#action.pointer_action.click()  # added 8 Feb 24
#action.perform()  # added 8 Feb 24
#print('ack sync')
#sleep(1)


#driver.switch_to.default_content() ###     move back up             ###

###  main loop ##
#then continually capture image
st.set_page_config(layout="wide")
placeholder = st.empty()  # added 30 Jan 24

while(True):
    driver.save_screenshot('map1.png')
    frame = cv2.imread('map1.png')
    # stream if needed
    with placeholder.container():
        st.image(frame, caption='Test Highside Map server')
        sleep(0.5)




        
