import os
import pandas as pd
import pyautogui
import time
from datetime import datetime
import subprocess


def signIn(meeting_id, password):

    # Open's Zoom Application from the specified location
    os.startfile(os.getenv('APPDATA') + "\\Zoom\\bin\\Zoom.exe")
    time.sleep(3)

    # Click's join button
    joinbtn = pyautogui.locateCenterOnScreen("joinameeting.png")
    pyautogui.moveTo(joinbtn)
    pyautogui.click()
    time.sleep(1)

    # Type the meeting id
    meetingidbtn = pyautogui.locateCenterOnScreen("meetingid.png")
    pyautogui.moveTo(meetingidbtn)
    pyautogui.write(meeting_id)
    time.sleep(2)

    # To turn of video and audio
    mediaBtn = pyautogui.locateAllOnScreen("media.png")
    for btn in mediaBtn:
        pyautogui.moveTo(btn)
        pyautogui.click()
        time.sleep(1)

    # To join
    join = pyautogui.locateCenterOnScreen("join.png")
    pyautogui.moveTo(join)
    pyautogui.click()
    time.sleep(2)

    # Enter's passcode to join meeting
    passcode = pyautogui.locateCenterOnScreen("meetingPasscode.png")
    pyautogui.moveTo(passcode)
    pyautogui.write(password)
    time.sleep(1)

    # Click's on join button
    joinmeeting = pyautogui.locateCenterOnScreen("joinmeeting.png")
    pyautogui.moveTo(joinmeeting)
    pyautogui.click()
    time.sleep(5)

    # TODO maximize = pyautogui.locateCenterOnScreen('maximize.png')
    # pyautogui.moveTo(maximize)
    pyautogui.click(clicks=2)
    time.sleep(2)


def record(duration):
    os.chdir(os.environ["ProgramFiles"]+"//obs-studio//bin//64bit")
    obs = subprocess.Popen([r'obs64.exe', '--startrecording'])
    time.sleep(duration * 60)
    obs.terminate()

    # os.startfile("obs64.exe")


df = pd.read_excel('timings.xlsx')

while True:
    # To get current time
    now = datetime.now().strftime("%H:%M")
    if now in str(df['Timings']):

        mylist = df["Timings"]
        mylist = [i.strftime("%H:%M") for i in mylist]
        c = [i for i in range(len(mylist)) if mylist[i] == now]
        row = df.loc[c]
        meeting_id = str(row.iloc[0, 1])
        password = str(row.iloc[0, 2])
        duration = int(row.iloc[0, 3])

        time.sleep(5)
        signIn(meeting_id, password)
        print('signed in')
        record(duration)
        time.sleep(2)
        break
