#!/usr/bin/python3
import sys
import time
import os
import mimetypes
import subprocess
from email.message import EmailMessage

try:
    from dogtail import tree
except ImportError:
    subprocess.check_call(['git', 'clone',
                           'https://gitlab.com/dogtail/dogtail.git'])
    subprocess.check_call(['git', '-C', 'dogtail', 'checkout', 'cb39d4b0'])
    sys.path.insert(0, os.getcwd() + '/dogtail')
    from dogtail import tree
from dogtail.config import config

defaultCutoffCount = 10


def make_email(attach_file):
    m = EmailMessage()
    m['Subject'] = 'Test Message'
    m['From'] = 'user@localhost'
    m['To'] = 'user@localhost'
    mimetype = mimetypes.guess_type(attach_file)
    if mimetype[0] == 'text/plain':
        with open(attach_file) as f:
            m.add_attachment(f.read(), filename=os.path.basename(attach_file))
    else:
        if mimetype[0]:
            typesubtype = mimetype[0].split('/')
        else:
            typesubtype = 'application', 'octet-stream'
        with open(attach_file, 'rb') as f:
            m.add_attachment(f.read(), filename=os.path.basename(attach_file),
                             maintype=typesubtype[0], subtype=typesubtype[1])
    return m.as_bytes()


def get_app():
    config.searchCutoffCount = 50
    tree.root.application('Thunderbird|Icedove')
    time.sleep(5)
    # now get it again to make sure we have the main window,
    # not any splash screen
    tb = tree.root.application('Thunderbird|Icedove')
    config.searchCutoffCount = defaultCutoffCount
    return tb


def skip_autoconf(tb):
    try:
        welcome = tb.childNamed('Mail Account Setup'
                                '|Set Up .* Existing Email .*')
        time.sleep(3)
        try:
            welcome.child('Cancel', roleName='button').doActionNamed('press')
        except tree.SearchError:
            welcome.button('Cancel').doActionNamed('press')
    except tree.SearchError:
        pass


def skip_autoconf2(tb):
    try:
        # depending on TB/GTK version, it can be either 'frame' or 'dialog'
        wizard = tb.child(name='Account Wizard')
        time.sleep(3)
        wizard.button('Cancel').doActionNamed('press')
        # wait for the confirmation prompt to appear - cannot do just search,
        # because there might be 'Account Wizard' frame already (see above)
        time.sleep(3)
        # if previous dialog was 'frame', then we have two 'frame' widgets with
        # the same name now, choose the newer one
        confirm = tb.findChildren(
            lambda w: w.name == 'Account Wizard' and w.roleName == 'frame',
            recursive=False)[-1]
        confirm.button('Exit').doActionNamed('press')
    except tree.SearchError:
        pass


def open_attachment(tb, name):
    # message subject
    msg = tb.child(name='Test Message - .*', roleName='frame')
    # resize the window so that button is on screen even on 1024x768
    subprocess.call(["xdotool", "search", "Test Message", "windowsize", "900", "700"])
    time.sleep(1)
    try:
        # TB >= 128
        msg.child(name, roleName='button').click()
    except tree.SearchError:
        # TB < 128
        msg.button(name).click()
    confirm = tb.child(name='Opening ' + name, roleName='frame')
    time.sleep(3)
    confirm.child(name='Open with', roleName='radio button')\
        .doActionNamed('select')
    try:
        # TB >= 128
        confirm.child('OK', roleName='button').doActionNamed('press')
    except tree.SearchError:
        # TB < 128
        confirm.button('OK').doActionNamed('press')


def main():
    tb = get_app()
    skip_autoconf(tb)
    eml_path = sys.argv[1] + '.eml'
    with open(eml_path, 'wb') as eml_file:
        eml_file.write(make_email(sys.argv[1]))
    # should open in existing app instance
    subprocess.check_call(['thunderbird', eml_path])
    skip_autoconf2(tb)
    open_attachment(tb, os.path.basename(sys.argv[1]))


if __name__ == '__main__':
    main()
