var package_option = $("#package");
var leverage_option = $("#leverage");
var bot = $("#bot");
var bot_type = $("#bot_type");

var classes = $("#invest").prop("class");
$("#invest").prop("class", classes + " active");

$(document).ready(function () {
	$("#invest_now_btn").click(function (event) {
		event.preventDefault();

		$("#feedbackPrompt").hide();
		$("#feedbackPrompt").html("");

		$("#invest_now_btn").attr("disabled", "");

		$(".accountId_feedback").hide();
		var accountId = $("input#accountId").val();

		if (accountId == "") {
			$(".accountId_feedback").html("Account ID cannot be empty.");
			$(".accountId_feedback").show();
			$("input#accountId").focus();

			$("#invest_now_btn").removeAttr("disabled");
			return false;
		}

		$(".amount_feedback").hide();
		var amountUSD = $("input#amountUSD").val();

		if (amountUSD == "") {
			$(".amount_feedback").html("Amount cannot be empty.");
			$(".amount_feedback").show();
			$("input#amountUSD").focus();

			$("#invest_now_btn").removeAttr("disabled");
			return false;
		}

		$("input#accountId").prop("disabled", true);
		$("input#amountUSD").prop("disabled", true);
		package_option.prop("disabled", true);
		leverage_option.prop("disabled", true);

		var form_data = "accountId=" + accountId;

		$.ajax({
			url: "../admin-helpers/get_user_account",
			type: "POST",
			data: form_data,
		}).done(function (response) {
			if (isJson(response)) {
				var json_response = jQuery.parseJSON(response);

				var user_name = json_response.user_name;

				showPopUp(user_name);
			} else {
				$("#feedbackPrompt").html('<div class="alert alert-danger" role="alert">' + response + "</div>");
				$("#feedbackPrompt").show();

				$("input#accountId").prop("disabled", false);
				$("input#amountUSD").prop("disabled", false);
				package_option.prop("disabled", false);
				leverage_option.prop("disabled", false);

				$(".button_spinner").hide();
				$("#invest_now_btn").removeAttr("disabled");
			}
		});

		return false;
	});

	$("#create_bot_btn").click(function (event) {
		event.preventDefault();

		$("#bt_feedbackPrompt").hide();
		$("#bt_feedbackPrompt").html("");

		$("#create_bot_btn").attr("disabled", "");

		$(".bt_accountId_feedback").hide();
		var accountId = $("input#bt_accountId").val();

		if (accountId == "") {
			$(".bt_accountId_feedback").html("Account ID cannot be empty.");
			$(".bt_accountId_feedback").show();
			$("input#bt_accountId").focus();

			$("#create_bot_btn").removeAttr("disabled");
			return false;
		}

		$("input#bt_accountId").prop("disabled", true);
		bot.prop("disabled", true);
		bot_type.prop("disabled", true);

		var form_data = "accountId=" + accountId;

		$.ajax({
			url: "../admin-helpers/get_user_account",
			type: "POST",
			data: form_data,
		}).done(function (response) {
			if (isJson(response)) {
				var json_response = jQuery.parseJSON(response);

				var user_name = json_response.user_name;

				showBotPopUp(user_name);
			} else {
				$("#bt_feedbackPrompt").html('<div class="alert alert-danger" role="alert">' + response + "</div>");
				$("#bt_feedbackPrompt").show();

				$("input#bt_accountId").prop("disabled", false);
				bot.prop("disabled", false);
				bot_type.prop("disabled", false);

				$(".bt_button_spinner").hide();
				$("#create_bot_btn").removeAttr("disabled");
			}
		});

		return false;
	});

	function isJson(str) {
		try {
			JSON.parse(str);
		} catch (e) {
			return false;
		}
		return true;
	}

	$("#package").on("change", function () {
		if (this.value == "Monthly") {
			$("#leverage-field").show();
		} else {
			$("#leverage-field").hide();
		}
	});
});

function showPopUp(user_name) {
	var package = $("#package option:selected").val();
	var accountId = $("input#accountId").val();

	$("#inv_message").html("Are you sure you want to create <b>" + package + "</b> trading pack" + " for <b>" + user_name + "</b> with account ID <b>" + accountId + "</b>?");

	$("#modalCreateInvestment").modal({
		show: true,
		backdrop: "static",
		keyboard: false,
	});

	$("#modalCreateInvestment").on("hidden.bs.modal", function () {
		$("input#accountId").prop("disabled", false);
		$("input#amountUSD").prop("disabled", false);
		package_option.prop("disabled", false);
		leverage_option.prop("disabled", false);

		$(".button_spinner").hide();
		$("#invest_now_btn").removeAttr("disabled");
	});
}

function showBotPopUp(user_name) {
	var sl_bot = $("#bot option:selected").val();
	var sl_bot_type = $("#bot_type option:selected").val();
	var accountId = $("input#bt_accountId").val();

	$("#bot_message").html("Are you sure you want to create <b>" + sl_bot_type + " (" + sl_bot + ")" + "</b> " + " for <b>" + user_name + "</b> with account ID <b>" + accountId + "</b>?");

	$("#modalCreateBot").modal({
		show: true,
		backdrop: "static",
		keyboard: false,
	});

	$("#modalCreateBot").on("hidden.bs.modal", function () {
		$("input#bt_accountId").prop("disabled", false);
		bot.prop("disabled", false);
		bot_type.prop("disabled", false);

		$(".bt_button_spinner").hide();
		$("#create_bot_btn").removeAttr("disabled");
	});
}

$("#create_investment_btn").click(function (event) {
	$(".create_spinner").show();
	$("#create_errorPrompt").hide();

	$("#create_investment_btn").prop("disabled", true);

	var accountId = $("input#accountId").val();
	var amountUSD = $("input#amountUSD").val();
	var package = $("#package option:selected").val();
	var leverage = $("#leverage option:selected").val();

	var form_data = "package=" + package + "&amountUSD=" + amountUSD + "&accountId=" + accountId + "&leverage=" + leverage;

	$.ajax({
		url: "../admin-helpers/create_trade",
		type: "POST",
		data: form_data,
	}).done(function (response) {
		$("#create_investment_btn").prop("disabled", false);
		$(".create_spinner").hide();

		if (response == "success") {
			$("#create_errorPrompt").hide();
			$("#feedbackPrompt").html('<div class="alert alert-success" role="alert">Trade successfully created.</div>');
			$("#feedbackPrompt").show();

			$("#modalCreateInvestment").modal("toggle");
		} else {
			$("#create_errorPrompt").show();
			$("#create_errorPrompt").html('<div class="alert alert-danger" role="alert">' + response + "</div>");
		}
	});
});

$("#cfm_create_bot_btn").click(function (event) {
	$(".cfm_create_spinner").show();
	$("#cfm_create_errorPrompt").hide();

	$("#cfm_create_bot_btn").prop("disabled", true);

	var accountId = $("input#bt_accountId").val();
	var sl_bot = $("#bot option:selected").val();
	var sl_bot_type = $("#bot_type option:selected").val();

	var form_data = "bot=" + sl_bot + "&botType=" + sl_bot_type + "&accountId=" + accountId;

	$.ajax({
		url: "../admin-helpers/create_bot",
		type: "POST",
		data: form_data,
	}).done(function (response) {
		$("#cfm_create_bot_btn").prop("disabled", false);
		$(".cfm_create_spinner").hide();

		if (response == "success") {
			$("#cfm_create_errorPrompt").hide();
			$("#bt_feedbackPrompt").html('<div class="alert alert-success" role="alert">Bot successfully created.</div>');
			$("#bt_feedbackPrompt").show();

			$("#modalCreateBot").modal("toggle");
		} else {
			$("#cfm_create_errorPrompt").show();
			$("#cfm_create_errorPrompt").html('<div class="alert alert-danger" role="alert">' + response + "</div>");
		}
	});
});
