import sys
from datetime import datetime
import subprocess
from batch.expire_maestro_manager import ExpireMaestroManager


# print('# args : {}'.format(len(sys.argv)))
# print('args : {}'.format(str(sys.argv)))
mode = 'mac'
arg_count = len(sys.argv)
if arg_count > 1:
    mode = str(sys.argv[1]).lower()


def print_items(list):
    for item in list:
        print(item)


def send_mail_30days_left(email, name, available_date):
    filename = 'send_mail_to_30days_left_maestro.php'
    path = ''
    if mode == 'mac':
        path = '/Applications/MAMP/htdocs/mouse_typing/src/web/php/'
    else:
        path = '/var/www/html/mouse_typing/src/web/php/'

    command_send_mail = []
    command_send_mail.append('/usr/bin/php')
    command_send_mail.append(path + filename)
    command_send_mail.append(email)
    command_send_mail.append(name)
    date = available_date.strftime('%Y%m%d')
    command_send_mail.append(date)
    # print(command_send_mail)

    # command_move_dir = ['cd', path]
    # print(command_move_dir)
    # subprocess.call(command_move_dir)
    # command_move_pwd = ['pwd']
    # subprocess.call(command_move_pwd)
    subprocess.call(command_send_mail)


def send_mail_7days_left(email, name, available_date):
    filename = 'send_mail_to_7days_left_maestro.php'
    path = ''
    if mode == 'mac':
        path = '/Applications/MAMP/htdocs/mouse_typing/src/web/php/'
    else:
        path = '/var/www/html/mouse_typing/src/web/php/'

    command_send_mail = []
    command_send_mail.append('/usr/bin/php')
    command_send_mail.append(path + filename)
    command_send_mail.append(email)
    command_send_mail.append(name)
    date = available_date.strftime('%Y%m%d')
    command_send_mail.append(date)
    # print(command_send_mail)

    # command_move_dir = ['cd', path]
    # print(command_move_dir)
    # subprocess.call(command_move_dir)
    # command_move_pwd = ['pwd']
    # subprocess.call(command_move_pwd)
    subprocess.call(command_send_mail)


# insert log and delete from DB
# print("30 days left");
expireMaestroMan = ExpireMaestroManager(mode)
maestro_list = expireMaestroMan.get_30days_left_maestro_list()
# print_items(maestro_list)
for maestro in maestro_list:
    send_mail_30days_left(maestro['Email'], maestro['Name'], maestro['AvailableActivateDateTime'])

# print("\n7 days left");
maestro_list = expireMaestroMan.get_7days_left_maestro_list()
# print_items(maestro_list)
for maestro in maestro_list:
    send_mail_7days_left(maestro['Email'], maestro['Name'], maestro['AvailableActivateDateTime'])
