import time
import threading
from transport import *
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ES


class MetrologTester():
    USER = 'user'
    PASWORD = 'uchetmo'
    PGW_PORT_1 = 1001
    PGW_PORT_2 = 1002
    PGW_PORT_3 = 1003
    PGW_TEST_DATA = b'abcdefghijklmnopqrstuvwxyz'
    WEB_TIMEOUT = 0.5


    def __init__(self, ip):

        self.pgw_1 = PortgwTcp(ip, self.PGW_PORT_1)
        self.pgw_2 = PortgwTcp(ip, self.PGW_PORT_2)
        self.pgw_3 = PortgwTcp(ip, self.PGW_PORT_3)

        self.pgw_1_thread = threading.Thread(target=self.portgw_sender, args=(self.pgw_1, ), daemon=True)
        self.pgw_2_thread = threading.Thread(target=self.portgw_sender, args=(self.pgw_2, ), daemon=True)
        self.pgw_3_thread = threading.Thread(target=self.portgw_sender, args=(self.pgw_3, ), daemon=True)

        self.ip = ip
        self.web_thread = threading.Thread(target=self.test_page_click, daemon=True)

    def connect(self):
        self.driver.get('http://' + self.ip)

    def login(self) -> bool:  
        self.driver.find_element(By.ID, 'login').send_keys(self.USER)
        self.driver.find_element(By.ID, 'pass').send_keys(self.PASWORD)
        self.driver.find_element(By.CLASS_NAME, 'btn.btn-primary').click()
        time.sleep(1)
        return True if self.driver.title == "Состояние модема" else False

    def test_page_click(self):
        self.driver = webdriver.Firefox()
        self.connect()
        while True:
            if self.login() == False:
                return
            for i in range(1):
                time.sleep(self.WEB_TIMEOUT)
                nav = self.driver.find_element(By.XPATH, "//ul[@id='nav']/li[2]/a").click()
                time.sleep(self.WEB_TIMEOUT)
                nav = self.driver.find_element(By.XPATH, "//ul[@id='nav']/li[3]/a").click()
                time.sleep(self.WEB_TIMEOUT)
                nav = self.driver.find_element(By.XPATH, "//ul[@id='nav']/li[1]/a").click()
                time.sleep(self.WEB_TIMEOUT)
            nav = self.driver.find_element(By.ID, 'logout').click()
            time.sleep(self.WEB_TIMEOUT)
    
    def start_web_clicker(self):
        self.web_thread.start()
        
    def start_pgw_sender(self, args):
        if len(args) != 0:
            if args[0] == 1:
                self.pgw_1_thread.start()
            if args[1] == 1:
                self.pgw_2_thread.start()
            if args[2] == 1:
                self.pgw_3_thread.start()
        else:
            self.pgw_1_thread.start()
            self.pgw_2_thread.start()
            self.pgw_3_thread.start()

    def portgw_sender(self, pgw):
        while True:
            pgw.open()
            pgw.write(self.PGW_TEST_DATA)
            pgw.read(len(self.PGW_TEST_DATA))
            pgw.close()
            time.sleep(0.1)

        

    

def metrolog():
    robot = MetrologTester('192.168.30.154') 
    robot.start_pgw_sender([0, 1, 1])
    # robot.start_web_clicker()
    
    

if __name__ == '__main__':
    metrolog()
    while True:
        time.sleep(1)
    
    