#[macro_use] extern crate serenity;

use serenity::client::Client;
use serenity::prelude::*;
use serenity::framework::standard::StandardFramework;
use serenity::model::gateway::Ready;
use std::{process, env};

struct Handler;

impl EventHandler for Handler {
	fn ready(&self, ctx: Context, ready: Ready) {
			if let Some(shard) = ready.shard {
			println!("{} is connected on shard {}/{}!", ready.user.name, shard[0], shard[1]);
			println!("--------------------------------------------");

			use serenity::model::user::OnlineStatus;
			use serenity::model::gateway::Game;
			let game = Game::playing("with knives");
			let status = OnlineStatus::Online;
      
			ctx.set_presence(Some(game), status);
			}
	}
}

fn main() {
    // Login with a bot token from the environment
    let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("token"), Handler)
        .expect("Error creating client");
    client.with_framework(StandardFramework::new()
        .configure(|c| c
        .prefix("OwO ")
        .case_insensitivity(true))

        .cmd("init", init)
        .cmd("gayculator", gayculator)
        .command("ad", |c| {
            c
            .cmd(ad)
            .check(|_, msg, _, _| msg.guild_id == Some(serenity::model::id::GuildId(255386835964919810)))
        })
        .cmd("waffle", waffle)
        .cmd("sausage", sausage)
        .cmd("halt", halt)
        .cmd("help", help)
        .cmd("info", info)
        .cmd("list_srv", list_srv)
        .command("compare_bot", |c| {
            c
            .cmd(compare_bot)
            .known_as("compare bot")
        })
	);

    // start listening for events by starting a single shard
    if let Err(why) = client.start() {
        println!("An error occurred while running the client: {:?}", why);
    }
}

command!(init(_ctx, message) {
    let _ = message.channel_id.say("The Discordinator9000 is ready to kick yer butts!");
});

command!(halt(ctx, message) {
	if message.author.id == 254310746450690048 {
	use serenity::model::user::OnlineStatus;
		let status = OnlineStatus::Offline;
		ctx.set_presence(None, status);
		ctx.quit();
		
		use std::{thread, time};
		let one_s = time::Duration::new(2, 0);
		thread::sleep(one_s);
		
		process::exit(0);
	}
});

command!(list_srv(_ctx, message) {
	if message.author.id == 254310746450690048 {
		/*use serenity::CACHE;
		let guilds = CACHE.read().guilds;
		    for (index, guild) in guilds.into_iter().enumerate() {
		        println!("{}: {}", index, guild.);
		    } */
		use serenity::CACHE;
		let cache = CACHE.read();
		for (index, guild_lock) in cache.guilds.values().enumerate() {
		    let guild = guild_lock.read();
		    let _ = message.channel_id.say(format!("{}: {}", index, guild.name));
		}
	}
});

command!(gayculator(_ctx, message, args) {
	let number_32: i32 = args.trim().parse()
	.expect("ERR!");
	let mut result;
	if number_32 % 2 == 0 {
		result = "You are straight as heck!".to_string();
	} else {
		result = "You are hella gay!".to_string();
	}
	let _ = message.channel_id.send_message(|m| m
		.embed(|e| e
		.title("Gayness level:")
		.description(result)
		.color(0xff00f9))
	);
});

command!(waffle(_ctx, message) {
	let _ = message.channel_id.say("Dongle!");
});

command!(sausage(_ctx, message) {
	let _ = message.channel_id.send_message(|m| m
		.embed(|e| e
		.title("Dongle!")
		.image("https://cdn.discordapp.com/attachments/379673147764506624/431546724637736971/image.png"))
		);
});

command!(ad(_ctx, message) {
		let _ = message.channel_id.send_message(|m| m
			.embed(|e| e
			.title(":b:ottom text")
			***REMOVED***
			.thumbnail("https://i.imgur.com/8MU0gqD.png")
			.color(0x00f3ff))
		); 
});

command!(help(_ctx, message) {
	let _ = message.channel_id.send_message(|m| m
		.embed(|e| e
			.title("Availble commands:")
			.description("All commands are case-insensitive")
			.fields(vec![
				("OwO waffle", "Join the **waffle dongle** cult!", false),
				("OwO sausage", "Join the **sausage dongle** cult!", false),
				("OwO help", "Help the fellow humanz!", false),
				("OwO info", "Show information about me!", false),
				("OwO compare_bot", "Compare me to other robots!", false),
				("Admin commands:", "\u{200B}", true),
				("OwO halt", "shut me down", false),
				("OwO list_srv", "list my servers", false)
			])
			.color(0x000000)
			));
});

command!(info(_ctx, message) {
use serenity::CACHE;
let num = CACHE.read().guilds.len();
	let _ = message.channel_id.send_message(|m| m
		.embed(|e| e
			.title("Discordinator9000's info:")
			.description(":gear: I can do stuff :cucumber:")
			.field("Author:", "#1337", false)
			.field("Server count:", num  , false)
			.field("Invite:", "[Invite link](https://discordapp.com/api/oauth2/authorize?client_id=470350233419907129&permissions=2048&scope=bot)", false )
			.footer(|f| f
			.text("Written in Rust using Serenity library"))
			.color(0xee657)
			));
});

command!(compare_bot(_ctx, message, args) {
let text: String = args.trim().parse()
	.expect("ERR!");
	if text.to_lowercase().contains("nib") {
		let _ = message.channel_id.say("I am superior to NibBot");
	} else if text.to_lowercase().contains("amit") {
		let _ = message.channel_id.say("Amiter is big dumb");
	} else if text.to_lowercase().contains("discordinator") {
		let _ = message.channel_id.say("Metaaaa, dude");
	} else {
		let _ = message.channel_id.say(format!("Me and {} are friends!", text));
	}
});


/*command!(embed_test(_ctx, message) {
	let _ = message.channel_id.send_message(|m| m
		.content("Hello, World!")
		.embed(|e| e
		.title("This is a title")
		.description("This is a description")
		.fields(vec![
			("This is the first field", "This is a field body", true),
			("This is the second field", "Both of these fields are inline", true),
		])
		.field("This is the third field", "This is not an inline field", false)
		.footer(|f| f
		.text("This is a footer"))));
}); */