from saflow.utils import array_topoplot, create_pval_mask, get_SAflow_bids
from saflow import RESULTS_PATH, FREQS_NAMES, BIDS_PATH, IMG_DIR
from mlneurotools.stats import compute_pval
from str2bool import str2bool
import argparse
import mne
import itertools
import pickle
import numpy as np

parser = argparse.ArgumentParser()
parser.add_argument(
    "-n",
    "--classif_name",
    default="VTC_PSD_SVM_group-level_multifeat_averaged_normalized_1000perm_5050-split_run-allruns",
    type=str,
    help="Name of the folder that contains classification results",
)
parser.add_argument(
    "-a",
    "--alpha",
    default=0.05,
    type=float,
    help="Desired alpha threshold",
)
args = parser.parse_args()

if __name__ == "__main__":
    classif_name = args.classif_name
    alpha = args.alpha
    savepath = RESULTS_PATH + classif_name + "/"

    # Load the data
    allfreqs_acc = []
    allfreqs_pval = []
    allmasks = []

    if "singlefeat" in classif_name:
        for FREQ in FREQS_NAMES:
            allchans_acc = []
            allchans_pval = []
            allchans_accperms = []
            for CHAN in range(270):
                savename = "freq_{}_chan_{}.pkl".format(FREQ, CHAN)
                with open(savepath + savename, "rb") as f:
                    result = pickle.load(f)
                allchans_acc.append(result["acc_score"])
                allchans_pval.append(result["acc_pvalue"])
                allchans_accperms.append(result["acc_pscores"])

            # Correction for multiple comparisons
            freq_perms = list(itertools.chain.from_iterable(allchans_accperms))
            corrected_pval = []
            for acc in allchans_acc:
                corrected_pval.append(compute_pval(acc, freq_perms))
            pval_mask = create_pval_mask(np.array(corrected_pval), alpha=alpha)

            allfreqs_acc.append(np.array(allchans_acc).squeeze())
            allfreqs_pval.append(np.array(allchans_pval).squeeze())
            allmasks.append(pval_mask)

        toplot_pval = allfreqs_pval
        figpath_pval = IMG_DIR + classif_name + "_pval" + str(alpha)[2:] + ".png"

        toplot_acc = allfreqs_acc
        figpath_acc = IMG_DIR + classif_name + "_acc" + str(alpha)[2:] + ".png"

        titles = FREQS_NAMES

    elif "multifeat" in classif_name:
        features_importances_list = []
        pvals_list = []
        permscores_list = []
        for FREQ in FREQS_NAMES:
            savename = "freq_{}.pkl".format(FREQ)
            with open(savepath + savename, "rb") as f:
                results = pickle.load(f)
            features_importances_list.append(results["feature_importances"].squeeze())
            pvals_list.append(results["acc_pvalue"])
            permscores_list.append(results["acc_pscores"])
        # Correction for multiple comparisons
        # freq_perms = list(itertools.chain.from_iterable(allchans_accperms))
        toplot_featimp = features_importances_list
        figpath_featimp = IMG_DIR + classif_name + "_features_importances" + ".png"

        freq_titles = []
        for idx_pval, pval in enumerate(pvals_list):
            if pval < alpha:
                freq_titles.append(FREQS_NAMES[idx_pval] + " *")
            else:
                freq_titles.append(FREQS_NAMES[idx_pval])

        titles = freq_titles

    _, data_fname = get_SAflow_bids(BIDS_PATH, subj="04", run="2", stage="-epo")
    epochs = mne.read_epochs(data_fname)
    ch_xy = epochs.pick_types(
        meg=True, ref_meg=False
    ).info  # Find the channel's position

    if "singlefeat" in classif_name:
        # Setup the min and max value of the color scale
        vmax_pval = np.max(np.max(np.asarray(toplot_pval)))
        vmin_pval = np.min(np.min(np.asarray(toplot_pval)))
        vmax_acc = np.max(np.max(np.asarray(toplot_acc)))
        vmin_acc = np.min(np.min(np.asarray(toplot_acc)))

        array_topoplot(
            toplot_pval,
            ch_xy,
            showtitle=True,
            titles=titles,
            savefig=True,
            figpath=figpath_pval,
            vmin=0,
            vmax=0.1,
            with_mask=True,
            masks=allmasks,
            cmap="viridis",
        )

        array_topoplot(
            toplot_acc,
            ch_xy,
            showtitle=True,
            titles=titles,
            savefig=True,
            figpath=figpath_acc,
            vmin=vmin_acc,
            vmax=vmax_acc,
            with_mask=True,
            masks=allmasks,
            cmap="plasma",
        )
    else:
        if "_LR_" in classif_name:
            vmin = 0
        elif "_RF_" in classif_name:
            vmin = 0
        vmin = 0
        vmax_featimp = np.max(np.max(np.asarray(toplot_featimp)))
        array_topoplot(
            toplot_featimp,
            ch_xy,
            showtitle=True,
            titles=titles,
            savefig=True,
            figpath=figpath_featimp,
            vmin=vmin,
            vmax=vmax_featimp,
            with_mask=False,
            masks=None,
            cmap="Reds",
        )


import yaml,smtplib,os
import email
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
import time
from google_images_download import google_images_download

config = yaml.load(open('config.yaml'), Loader=yaml.FullLoader)

def GetTargets():
    receivers = []
    phonenumber = [config['Phone_Number']]
    carrier = ['@vzwpix.com','@pm.sprint.com', '@tmomail.net', '@mms.att.net']
    for c in carrier:
        for num in phonenumber:
            receivers.append(num+c)

    return receivers

def GetAttachments():
    response = google_images_download.googleimagesdownload()

    arguments = {"keywords": config["Attachments"],
                 "output_directory": "attachments/",
                 "no_directory": True,
                 "format": "jpg",
                 "chromedriver": config["Chrome_Driver"],
                 "limit":config["Number_Of_MSGs"],
                 "size": "medium"}
    try:
        response.download(arguments)

    # Handling File NotFound Error
    except FileNotFoundError as Error:
        print(Error)

def SendSMS():

    receiver = GetTargets()

    message = "Hey"

    try:
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        server.ehlo()
        server.login(config["Sender"], config["Sender_Pass"])
        server.sendmail(config["Sender"], receiver, message)
        print("Successfully sent email")

    except smtplib.SMTPException:
        print("Error: unable to send email")

def SendMMS(attachment):

    receiver = GetTargets()

	###List of attachments###

    outer = MIMEMultipart()
    outer['Subject'] = "Hey"

	###Add the attachments to the message###
    # for file in arr:
    try:
        with open('attachments/'+attachment, 'rb') as fp:
            msg2 = MIMEBase('application', "octet-stream")
            msg2.set_payload(fp.read())
        encoders.encode_base64(msg2)
        msg2.add_header('Content-Disposition', 'attachments', filename=os.path.basename(attachment))
        outer.attach(msg2)
    except:
        print("Unable to open one of the attachments.")
        raise

    composed = outer.as_string()

    server = smtplib.SMTP('smtp.gmail.com:587')
    server.ehlo()
    server.starttls()
    server.login(config["Sender"], config["Sender_Pass"])
    server.sendmail(config["Sender"],receiver,composed)
    server.quit()
    print("IMAGE SENT")
    time.sleep(20)

if __name__ == "__main__":

    GetAttachments()

    arr = os.listdir("attachments")
    for attachment in arr:
        SendMMS(attachment)