# -*- coding: utf-8 -*-
import os
import time
from datetime import datetime

import click
import pythoncom
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from win32com.client import Dispatch

from t_infinity import driver
from t_infinity.logger import logger


class LogisticsCreate:
    days_in_field = None
    call_number = None

    def __init__(self, serial_number, product_code, inbound_repair_order_number):
        self.instance = driver.instance
        self.instance.session_id = driver.session_id
        self.wait = WebDriverWait(self.instance, 5)
        if self.check_ro_number_is_free(inbound_repair_order_number):
            return
        history = self.check_history(serial_number, product_code)
        if history is 1:
            self.in_zulu(serial_number, product_code, inbound_repair_order_number)
        elif history is 2:
            self.not_in_zulu(serial_number, product_code, inbound_repair_order_number)
        elif history is 3:
            self._add_new(serial_number, product_code, inbound_repair_order_number)

        self.go_to_create()
        self.create_welcome_fill()
        self.job_site_details_fill()
        self.ship_site_details()
        self.job_item_details(serial_number, product_code, inbound_repair_order_number)
        self.job_details()
        self.complete()
        self.print(inbound_repair_order_number, self.call_number)