const axios = require('axios');
const protonMail = "web-mail@5.0.20.3";
const app = require('../../app.json');
const net = require('net');

function sleep(ms) {
    return new Promise((res) => {
        setTimeout(res, ms);
    });
}

function checkGmail(user, type) {
    return new Promise((res) => {
        const client = net.createConnection(25, "gmail-smtp-in.l.google.com");
        let current = 0;

        client.on('data', (data) => {
            let message = data.toString().trim();

            if (message.startsWith("220 ")) {
                client.write("helo equestria.dev\n");
            } else if (message.startsWith("250 ") && current === 0) {
                current++;
                client.write("mail from: <invalid@invalid.equestria.dev>\n");
            } else if (message.startsWith("250 ") && current === 1) {
                current++;
                client.write(`rcpt to: <${user}>\n`);
            } else if (message.startsWith("250 ") && current === 2) {
                client.write("quit\n");
                res("gmail");
            } else if (message.startsWith("550-") && current === 2) {
                client.write("quit\n");
                res(type);
            }
        });

        client.on('end', () => {
            res(type);
        });
    });
}

(async () => {
    let user = process.argv[2];
    let type = "generic";

    try {
        let data = (await axios.get(`https://mail.proton.me/api/core/v4/keys?Email=${encodeURIComponent(user)}`, {
            headers: {
                Cookie: app.proton.cookie,
                'X-Pm-Appversion': protonMail,
                'X-Pm-Uid': app.proton.id
            }
        })).data;

        if (data['RecipientType'] < 2) type = "proton";
    } catch (e) {}

    if (type === "generic") {
        try {
            type = await checkGmail(user, type);
        } catch (e) {}
    }

    if (type === "generic") {
        let domain = user.split("@")[1].trim();

        if (domain === "outlook.dk"
         || domain === "skype.dk"
         || domain === "live.dk"
         || domain === "hotmail.dk"
         || domain === "outlook.dk"
         || domain === "skype.se"
         || domain === "live.se"
         || domain === "hotmail.se"
         || domain === "outlook.se"
         || domain === "outlook.fr"
         || domain === "skype.fr"
         || domain === "live.fr"
         || domain === "hotmail.fr"
         || domain === "outlook.it"
         || domain === "skype.it"
         || domain === "live.it"
         || domain === "hotmail.it"
         || domain === "outlook.de"
         || domain === "skype.de"
         || domain === "live.de"
         || domain === "hotmail.de"
         || domain === "outlook.com.au"
         || domain === "hotmail.com"
         || domain === "hotmail.co.uk"
         || domain === "hotmail.eu"
         || domain === "hotmail.co"
         || domain === "hotmail.net"
         || domain === "hotmail.org"
         || domain === "live.com"
         || domain === "live.co.uk"
         || domain === "line.net"
         || domain === "line.co"
         || domain === "line.org"
         || domain === "line.eu"
         || domain === "skype.com"
         || domain === "skype.co.uk"
         || domain === "skype.net"
         || domain === "skype.co"
         || domain === "skype.org"
         || domain === "skype.eu"
         || domain === "outlook.com"
         || domain === "outlook.org"
         || domain === "outlook.co"
         || domain === "outlook.eu") {
            type = "outlook";
        }
    }

    if (type === "generic") {
        let domain = user.split("@")[1].trim();

        if (domain === "yahoo.com"
            || domain === "yahoo.fr"
            || domain === "yahoo.co.uk") {
            type = "yahoo";
        }
    }

    let parts = user.split("@").map(i => i.trim());
    let icon = "https://img.icons8.com/fluency-systems-regular/64/ffffff/email.png";
    let title = "Email";

    if (type === "gmail") {
        icon = "https://upload.wikimedia.org/wikipedia/commons/7/7e/Gmail_icon_%282020%29.svg";
        title = "Gmail";
    }

    if (type === "outlook") {
        icon = "https://upload.wikimedia.org/wikipedia/commons/d/df/Microsoft_Office_Outlook_%282018%E2%80%93present%29.svg";
        title = "Outlook";
    }

    if (type === "yahoo") {
        icon = "https://upload.wikimedia.org/wikipedia/commons/e/e0/Yahoo%21_Mail_icon_%282013-2019%29.png";
        title = "Yahoo";
    }

    if (type === "proton") {
        icon = "https://proton.me/static/fc5b1e8b92dd09734ca7c9d91e89db93/proton-mail-badge.svg";
        title = "Proton Mail";
    }

    console.log(JSON.stringify({
        error: null,
        avatar: icon,
        name: title,
        description: user,
        link: "mailto:" + user,
        copy: [
            {
                title: "Copy email address",
                text: user
            },
            {
                title: "Copy email domain",
                text: parts[1]
            },
            {
                title: "Copy email user name",
                text: parts[0]
            },
            {
                title: "Copy mailto: link",
                text: `mailto:${user}`
            }
        ]
    }, null, 2));
})();