#! -*- coding=utf-8 -*-
import time
from machines.StateMachine import Machine
from appium4droid import webdriver
from appium4droid.common.exceptions import *
from appium4droid.support.ui import WebDriverWait
from random import choice


try:
    from util import reset_wifi, alert
except ImportError:
    reset_wifi = lambda: 1
    alert = lambda: 1


class MachineVPN(Machine):
    def __init__(self, driver, city):
        super(MachineVPN, self).__init__(self.enter_setting)
        self.driver = driver
        self.reconnect_times = 0
        self.city = city

    def enter_setting(self):
        dr = self.driver
        dr.press_keycode(3)  # back to Home
        time.sleep(1)
        dr.press_keycode(3)  # back to Home
        time.sleep(1)
        dr.find_element_by_name("设置").click()
        time.sleep(1)

        return self.enter_vpn

    def enter_vpn(self):
        dr = self.driver
        dr.find_element_by_name("其他连接方式").click()
        time.sleep(1)
        dr.find_element_by_name("VPN").click()
        time.sleep(1)
        return self.close_connection

    def close_connection(self):
        dr = self.driver
        if self.city == "广州":
            server_ip = ["183.47.6.115", "gzjsq51.2288.org", "gzjsq86.2288.org", "zhjsq77.2288.org", "zhjsq78.2288.org", "hyjsq81.2288.org",
                         "szjsq85.2288.org", "hzjsq80.2288.org", "dgjsq50.2288.org", "mzjsq98.2288.org"]
            try:
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_name("已连接")).click()
                time.sleep(1)
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_name("断开连接")).click()
                time.sleep(1)
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_id("com.android.settings:id/detail_arrow")).click()
                time.sleep(1)
                edts = WebDriverWait(dr, 15).until(lambda d: d.find_element_by_id("com.android.settings:id/server"))
                edts.click()
                dr.press_keycode(123)
                time.sleep(1)
                time.sleep(0.5)
                for i in range(30):
                    dr.press_keycode(67)
                edts.send_keys(choice(server_ip))
                time.sleep(1)
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_name("确定")).click()
                time.sleep(1)
                print("thats ok")
            except TimeoutException as e:
                print("in else")
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_id("com.android.settings:id/detail_arrow")).click()
                time.sleep(1)
                edts = WebDriverWait(dr, 15).until(lambda d: d.find_element_by_id("com.android.settings:id/server"))
                edts.click()
                dr.press_keycode(123)
                time.sleep(1)
                time.sleep(0.5)
                for i in range(30):
                    dr.press_keycode(67)
                edts.send_keys(choice(server_ip))
                time.sleep(1)
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_name("确定")).click()
                time.sleep(1)
        else:
            try:
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_name("已连接")).click()
                time.sleep(1)
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_name("断开连接")).click()
                time.sleep(1)
                WebDriverWait(dr, 15).until(lambda d: d.find_element_by_name("PPTP VPN")).click()
                time.sleep(1)
                print("thats ok")
            except TimeoutException as e:
                print("in else")
                dr.find_element_by_class_name("android.widget.RadioButton").click()
                time.sleep(1)
        return self.reconnect

    def reconnect(self):
        CONNECT_WAIT_TIME = 30

        dr = self.driver
        # time.sleep(2)
        # dr.find_element_by_name("连接").click()
        ## maybe should try and except here
        try:
            WebDriverWait(dr, CONNECT_WAIT_TIME).until(lambda d: d.find_element_by_name("已连接"))
        except TimeoutException:
            return self.onConnectTimeout

        self.reconnect_times = 0
        dr.press_keycode(3)
        return self.exit

    def onConnectTimeout(self):
        print("connect failed!!")
        self.reconnect_times = 1 + self.reconnect_times
        if self.reconnect_times > 3:
            alert()
            reset_wifi()
            self.reconnect_times = 0
        return self.close_connection

    # def Exit(self):
    #     self.running = False
    #     print("going to Exit")
    #     dr = self.driver
    #     dr.press_keycode(3)
    #     return None

    def ExceptinOccur(self):
        self.running = False
        print("ExceptinOccur!!!")
        self.driver.press_keycode(3)
        return None


if __name__ == "__main__":
    dr = webdriver.Remote()
    time.sleep(2)
    dr.press_keycode(3)

    MachineVPN(dr).run()
