const Discord = require('discord.js');
const client = new Discord.Client();

require("dotenv").config()

var hugGifs =
        ["https://c.tenor.com/9e1aE_xBLCsAAAAC/anime-hug.gif",
         "https://c.tenor.com/Ct4bdr2ZGeAAAAAC/teria-wang-kishuku-gakkou-no-juliet.gif",
         "https://c.tenor.com/ztEJgrjFe54AAAAd/hug-anime.gif",
         "https://c.tenor.com/UhcyGsGpLNIAAAAd/hug-anime.gif",
         "https://c.tenor.com/fLxZt7jo1YEAAAAd/loli-dragon.gif",
         "https://c.tenor.com/qF7mO4nnL0sAAAAd/abra%C3%A7o-hug.gif"]


client.once('ready', () => {
    console.log('Running');
    let msg = []
    msg.push(new Discord.MessageAttachment('https://cdn.discordapp.com/attachments/770762334305386556/925041308677074944/mp4.gif'))
    msg.push(new Discord.MessageAttachment('https://cdn.discordapp.com/attachments/770762334305386556/925041308677074944/mp4.gif'))
    
    client.channels.cache.get('770748282191740943').send(msg)
});

client.on('message', gotMessage);


function gotMessage(message){

    // console.log(message)

    if(message.content.includes('https://media.discordapp.net')) {
        if(message.content.includes('webm') || message.content.includes('mov') ||
        message.content.includes('mp4')) {
        let linkArr = message.content.split('https://media.discordapp.net');
        message.channel.send('https://cdn.discordapp.com' +linkArr[1])
        }
    }

    let content = message.content.trim()

    var msgContentSplit = content.split(/[ ]+/);

    if(message.reference != null){
        move(message, msgContentSplit)
    }

    if (msgContentSplit[0] == "henlo") {
        henlo(message)
    }
    else if (msgContentSplit[0] == "how ye") {
        mood(message)
    }
    else if (msgContentSplit[0] == "tylko jedno") {
        message.channel.send("Koksu pięć gram odlecieć sam");
    }
    if (msgContentSplit[0] == "hug") {
        hug(message)
    }
}

// Responses
function henlo(message){
    var emojis = ["🥰", "🐄", "🐮", "❤️", "👋", "🤠", "😊"];
    let randomNum = RNG(emojis.length);
    message.channel.send("Henlooo " + message.author.username + "   " + emojis[randomNum]);
}

function hug(message){
    var mentionedUsers = message.mentions.users.values()
    var mentionedUsersSize = message.mentions.users.size
    var mentionedRoles = message.mentions.roles.values()
    var mentionedRolesSize = message.mentions.roles.size

    msg = message.content.toLowerCase()

    let randomNum = RNG(hugGifs.length)
    title = message.author.username + " hugs"
    if (mentionedUsersSize > 0 || mentionedRolesSize > 0){
        let allMentions = ""
        for (let i = 0; i < mentionedUsersSize; i++) {
            let user = mentionedUsers.next()
            allMentions = allMentions + ", <@" + user.value.id + ">"
        }
        for (let i = 0; i < mentionedRolesSize; i++) {
            let role = mentionedRoles.next()
            allMentions = allMentions + ", <@&" + role.value.id + ">"
        }
        allMentions = allMentions.slice(2)
        embed = createEmbed(title, hugGifs[randomNum], allMentions)
        message.channel.send(embed)
    }
    else if (msg.includes("@here") || msg.includes("all")){
        embed = createEmbed(title, hugGifs[randomNum], "@here")
        message.channel.send(embed)
    }
    else if (msg.includes("@everyone")){
        embed = createEmbed(title, hugGifs[randomNum], "@everyone")
        message.channel.send(embed)
    }
    else {
        embed = new Discord.MessageEmbed().setImage(hugGifs[randomNum]).setColor(randomColor())
        message.channel.send(embed)
    }
}

function mood(message){
    var responses = ["Not bad, how yee?", "MOOdy", "A bit sad 😢", "Good, how yee?", "I'm fine, how yee?"];
    let randomNum = RNG(responses.length);
    message.channel.send(responses[randomNum]);
}

function move(message, splits){
    var replyChannelId;
    var mentionedChannelId;

    if(splits.length == 1 && message.mentions.channels.first() != undefined){

        message.react('🐮')

        var replyMessageId = message.reference.messageID;

        mentionedChannelId = splits[0].substring(2, splits[0].length - 1);
        replyChannelId = message.reference.channelID;

        var replyChannel = client.channels.cache.find(channel => channel.id === replyChannelId);

        replyChannel.messages.fetch(replyMessageId).then(msg => {

        newMessage = new Discord.Message()
        allAttachments = msg.attachments.values()
        
        att = []
        for (let i = 0; i < msg.attachments.size; i++){
            currAttachment = allAttachments.next().value
            att.push(new Discord.MessageAttachment(currAttachment.url))
        }
        console.log(att)
        coll = new Discord.Collection(att)
        console.log(coll)

        newMessage.attachments = coll
        // newMessage.attachments = new Discord.MessageAttachment()
        newMessage.embeds = msg.embeds
        newMessage.content = "Sent by <@" + msg.author + "> \nmooved <@" + message.author + ">\n"
        newMessage.content += msg.content

            client.channels.cache.get(mentionedChannelId).send(newMessage)

            msg.delete({timeout: 3000});
        });
        message.delete({timeout: 3000});
    }
}


function createEmbed(title, img, desc){
    embed = new Discord.MessageEmbed()
            .setTitle(title)
            .setImage(img)
            .setDescription(desc)
            .setColor(randomColor())
    return embed
}

function randomColor() {
    let color = '#';
    for (let i = 0; i < 6; i++){
       const random = Math.random();
       const bit = (random * 16) | 0;
       color += (bit).toString(16);
    };
    return color;
 };

function RNG(max){
    return Math.floor(Math.random() * max)
}



client.login(process.env.TOKEN)