#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/input/seat.h"
#include "sway/tree/workspace.h"
#include "sway/tree/arrange.h"
#include "sway/output.h"
#include "list.h"
#include "log.h"
#include "stringop.h"

static struct workspace_config *workspace_config_find_or_create(char *ws_name) {
	struct workspace_config *wsc = workspace_find_config(ws_name);
	if (wsc) {
		return wsc;
	}
	wsc = calloc(1, sizeof(struct workspace_config));
	if (!wsc) {
		return NULL;
	}
	wsc->workspace = strdup(ws_name);
	wsc->outputs = create_list();
	wsc->gaps_inner = INT_MIN;
	wsc->gaps_outer.top = INT_MIN;
	wsc->gaps_outer.right = INT_MIN;
	wsc->gaps_outer.bottom = INT_MIN;
	wsc->gaps_outer.left = INT_MIN;
	layout_default_modifiers_init(&wsc->layout_default_modifiers);
	list_add(config->workspace_configs, wsc);
	return wsc;
}

void free_workspace_config(struct workspace_config *wsc) {
	free(wsc->workspace);
	list_free_items_and_destroy(wsc->outputs);
	free(wsc);
}

int parse_into_modifiers(int argc, char **argv,
		struct sway_scroller_modifiers *modifiers) {
	if (modifiers == NULL) {
		return 0;
	}
	int success = 0;
	for (int i = 0; i < argc; ++i) {
		if (strcasecmp(argv[i], "h") == 0) {
			modifiers->mode = L_HORIZ;
			modifiers->mode_set = true;
			++success;
		} else if (strcasecmp(argv[i], "v") == 0) {
			modifiers->mode = L_VERT;
			modifiers->mode_set = true;
			++success;
		}

		if (strcasecmp(argv[i], "after") == 0) {
			modifiers->insert = INSERT_AFTER;
			modifiers->insert_set = true;
			++success;
		} else if (strcasecmp(argv[i], "before") == 0) {
			modifiers->insert = INSERT_BEFORE;
			modifiers->insert_set = true;
			++success;
		} else if (strcasecmp(argv[i], "end") == 0) {
			modifiers->insert = INSERT_END;
			modifiers->insert_set = true;
			++success;
		} else if (strcasecmp(argv[i], "beginning") == 0 || strcasecmp(argv[i], "beg") == 0) {
			modifiers->insert = INSERT_BEGINNING;
			modifiers->insert_set = true;
			++success;
		}

        if (strcasecmp(argv[i], "focus") == 0) {
			modifiers->focus = true;
			modifiers->focus_set = true;
			++success;
		} else if (strcasecmp(argv[i], "nofocus") == 0) {
			modifiers->focus = false;
			modifiers->focus_set = true;
			++success;
		}

        if (strcasecmp(argv[i], "center_horiz") == 0) {
			modifiers->center_horizontal = true;
			modifiers->center_horizontal_set = true;
			++success;
		} else if (strcasecmp(argv[i], "nocenter_horiz") == 0) {
			modifiers->center_horizontal = false;
			modifiers->center_horizontal_set = true;
			++success;
		}
        if (strcasecmp(argv[i], "center_vert") == 0) {
			modifiers->center_vertical = true;
			modifiers->center_vertical_set = true;
			++success;
		} else if (strcasecmp(argv[i], "nocenter_vert") == 0) {
			modifiers->center_vertical = false;
			modifiers->center_vertical_set = true;
			++success;
		}

        if (strcasecmp(argv[i], "reorder_auto") == 0) {
			modifiers->reorder = REORDER_AUTO;
			modifiers->reorder_set = true;
			++success;
		} else if (strcasecmp(argv[i], "noreorder_auto") == 0) {
			modifiers->reorder = REORDER_LAZY;
			modifiers->reorder_set = true;
			++success;
		}
	}
	if (success > 0) {
		// Modifiers had some fields set
		modifiers->set = true;
	}
	return success;
}

static struct cmd_results *cmd_workspace_layout_default_mode(int argc, char **argv, int mode_location) {
	const char expected[] = "Expected 'workspace <name> layout_default_mode "
		"[<h|v> <after|before|end|beg> <focus|nofocus> <center_horiz|nocenter_horiz> <center_vert|nocenter_vert> <reorder_auto|noreorder_auto>]";
	if (mode_location == 0) {
		return cmd_results_new(CMD_INVALID, "%s", expected);
	}
	struct cmd_results *error;
	if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 3))) {
		return error;
	}
	int name_words = 0;
	for (int i = 0; i < argc; ++i) {
		if (strcasecmp(argv[i], "layout_default_mode") == 0) {
			name_words = i;
			break;
		}
	}
	char *ws_name = join_args(argv, name_words);
	struct workspace_config *wsc = workspace_config_find_or_create(ws_name);
	free(ws_name);
	if (!wsc) {
		return cmd_results_new(CMD_FAILURE,
				"Unable to allocate workspace output");
	}
	int success = parse_into_modifiers(argc, argv, &wsc->layout_default_modifiers);
	if (success > 0) {
		return cmd_results_new(CMD_SUCCESS, NULL);
	}
	return cmd_results_new(CMD_INVALID, "%s", expected);
}

static void prevent_invalid_outer_gaps(struct workspace_config *wsc) {
	if (wsc->gaps_outer.top != INT_MIN &&
			wsc->gaps_outer.top < -wsc->gaps_inner) {
		wsc->gaps_outer.top = -wsc->gaps_inner;
	}
	if (wsc->gaps_outer.right != INT_MIN &&
			wsc->gaps_outer.right < -wsc->gaps_inner) {
		wsc->gaps_outer.right = -wsc->gaps_inner;
	}
	if (wsc->gaps_outer.bottom != INT_MIN &&
			wsc->gaps_outer.bottom < -wsc->gaps_inner) {
		wsc->gaps_outer.bottom = -wsc->gaps_inner;
	}
	if (wsc->gaps_outer.left != INT_MIN &&
			wsc->gaps_outer.left < -wsc->gaps_inner) {
		wsc->gaps_outer.left = -wsc->gaps_inner;
	}
}

static struct cmd_results *cmd_workspace_gaps(int argc, char **argv,
		int gaps_location) {
	const char expected[] = "Expected 'workspace <name> gaps "
		"inner|outer|horizontal|vertical|top|right|bottom|left <px>'";
	if (gaps_location == 0) {
		return cmd_results_new(CMD_INVALID, "%s", expected);
	}
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "workspace", EXPECTED_EQUAL_TO,
					gaps_location + 3))) {
		return error;
	}
	char *ws_name = join_args(argv, argc - 3);
	struct workspace_config *wsc = workspace_config_find_or_create(ws_name);
	free(ws_name);
	if (!wsc) {
		return cmd_results_new(CMD_FAILURE,
				"Unable to allocate workspace output");
	}

	char *end;
	int amount = strtol(argv[gaps_location + 2], &end, 10);
	if (strlen(end)) {
		return cmd_results_new(CMD_FAILURE, "%s", expected);
	}

	bool valid = false;
	char *type = argv[gaps_location + 1];
	if (!strcasecmp(type, "inner")) {
		valid = true;
		wsc->gaps_inner = (amount >= 0) ? amount : 0;
	} else {
		if (!strcasecmp(type, "outer") || !strcasecmp(type, "vertical")
				|| !strcasecmp(type, "top")) {
			valid = true;
			wsc->gaps_outer.top = amount;
		}
		if (!strcasecmp(type, "outer") || !strcasecmp(type, "horizontal")
				|| !strcasecmp(type, "right")) {
			valid = true;
			wsc->gaps_outer.right = amount;
		}
		if (!strcasecmp(type, "outer") || !strcasecmp(type, "vertical")
				|| !strcasecmp(type, "bottom")) {
			valid = true;
			wsc->gaps_outer.bottom = amount;
		}
		if (!strcasecmp(type, "outer") || !strcasecmp(type, "horizontal")
				|| !strcasecmp(type, "left")) {
			valid = true;
			wsc->gaps_outer.left = amount;
		}
	}
	if (!valid) {
		return cmd_results_new(CMD_INVALID, "%s", expected);
	}

	// Prevent invalid gaps configurations.
	if (wsc->gaps_inner != INT_MIN && wsc->gaps_inner < 0) {
		wsc->gaps_inner = 0;
	}
	prevent_invalid_outer_gaps(wsc);

	return error;
}

struct cmd_results *cmd_workspace(int argc, char **argv) {
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1))) {
		return error;
	}

	if (strcasecmp(argv[0], "swap") == 0) {
		struct sway_workspace *workspace = config->handler_context.workspace;
		if (!workspace) {
			return cmd_results_new(CMD_INVALID,
					"This command only works when called from a workspace");
		}
		if (argc < 2) {
			return cmd_results_new(CMD_INVALID,
					"Expected 'workspace swap [number] <[num:]name> [name_only]'");
		}
		struct sway_workspace *swap_ws;
		int arg = 2;
		if (strcasecmp(argv[1], "number") == 0) {
			if (argc < 3 || !isdigit(argv[2][0]) ||
				!(swap_ws = workspace_by_number(argv[2]))) {
				return cmd_results_new(CMD_INVALID,	"Invalid workspace number");
			} 
			++arg;
		} else {
			if (!(swap_ws = workspace_by_name(argv[1]))) {
				return cmd_results_new(CMD_INVALID,
						"Invalid workspace name '%s'", argv[1]);
			}
		}
		bool name_only;
		if (argc > arg && strcasecmp(argv[arg], "name_only") == 0) {
			name_only = true;
		} else {
			name_only = false;
		}
		workspace_swap(workspace, swap_ws, name_only);
		return cmd_results_new(CMD_SUCCESS, NULL);
	}

	if (strcasecmp(argv[0], "split") == 0) {
		const char *expected = "Expected 'workspace split [h|v|reset] [<fraction>] [<gap>]'";
		struct sway_workspace *workspace = config->handler_context.workspace;
		if (!workspace) {
			return cmd_results_new(CMD_INVALID,
					"This command only works when called from a workspace");
		}
		enum sway_workspace_split split = WORKSPACE_SPLIT_VERTICAL;
		double fraction = 0.5;
		int gap = 0;
		if (argc > 1) {
			if (strcasecmp(argv[1], "h") == 0) {
				split = WORKSPACE_SPLIT_HORIZONTAL;
			} else if (strcasecmp(argv[1], "v") == 0) {
				split = WORKSPACE_SPLIT_VERTICAL;
			} else if (strcasecmp(argv[1], "reset") == 0) {
				workspace_split_reset(workspace);
				return cmd_results_new(CMD_SUCCESS, NULL);
			} else {
				return cmd_results_new(CMD_INVALID, "%s", expected);
			}
			if (argc > 2) {
				char *end;
				fraction = strtod(argv[2], &end);
				if (*end) {
					return cmd_results_new(CMD_INVALID, "%s", expected);
				}
				fraction = MIN(MAX(0.1, fraction), 0.9);
				if (argc > 3) {
					gap = strtol(argv[3], &end, 10);
					if (*end) {
						return cmd_results_new(CMD_INVALID, "%s", expected);
					}
					if (gap < 0) {
						gap = 0;
					}
				}
			}
		}
		workspace_split(workspace, split, fraction, gap);
		return cmd_results_new(CMD_SUCCESS, NULL);
	}

	int output_location = -1;
	int gaps_location = -1;
	int mode_location = -1;

	for (int i = 0; i < argc; ++i) {
		if (strcasecmp(argv[i], "output") == 0) {
			output_location = i;
			break;
		}
	}
	for (int i = 0; i < argc; ++i) {
		if (strcasecmp(argv[i], "gaps") == 0) {
			gaps_location = i;
			break;
		}
	}
	for (int i = 0; i < argc; ++i) {
		if (strcasecmp(argv[i], "layout_default_mode") == 0) {
			mode_location = i;
			break;
		}
	}
	for (int i = 0; i < argc; ++i) {
		if (strcasecmp(argv[i], "exec") == 0) {
			return cmd_results_new(CMD_INVALID,
				"'workspace <name> exec <command>' has been deprecated. See "
				"man 5 scroll for details");
		}
	}
	if (output_location == 0) {
		return cmd_results_new(CMD_INVALID,
			"Expected 'workspace <name> output <output>'");
	} else if (output_location > 0) {
		if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST,
						output_location + 2))) {
			return error;
		}
		char *ws_name = join_args(argv, output_location);
		struct workspace_config *wsc = workspace_config_find_or_create(ws_name);
		free(ws_name);
		if (!wsc) {
			return cmd_results_new(CMD_FAILURE,
					"Unable to allocate workspace output");
		}
		for (int i = output_location + 1; i < argc; ++i) {
			list_add(wsc->outputs, strdup(argv[i]));
		}
	} else if (gaps_location >= 0) {
		if ((error = cmd_workspace_gaps(argc, argv, gaps_location))) {
			return error;
		}
	} else if (mode_location >= 0) {
		if ((error = cmd_workspace_layout_default_mode(argc, argv, mode_location))) {
			return error;
		}
	} else {
		if (config->reading || !config->active) {
			return cmd_results_new(CMD_DEFER, NULL);
		} else if (!root->outputs->length) {
			return cmd_results_new(CMD_INVALID,
					"Can't run this command while there's no outputs connected.");
		}

		if (root->fullscreen_global) {
			return cmd_results_new(CMD_FAILURE,
				"Can't switch workspaces while fullscreen global");
		}

		bool auto_back_and_forth = true;
		while (strcasecmp(argv[0], "--no-auto-back-and-forth") == 0) {
			auto_back_and_forth = false;
			if ((error = checkarg(--argc, "workspace", EXPECTED_AT_LEAST, 1))) {
				return error;
			}
			++argv;
		}

		struct sway_seat *seat = config->handler_context.seat;

		struct sway_workspace *ws = NULL;
		if (strcasecmp(argv[0], "number") == 0) {
			if (argc < 2) {
				return cmd_results_new(CMD_INVALID,
						"Expected workspace number");
			}
			if (!isdigit(argv[1][0])) {
				return cmd_results_new(CMD_INVALID,
						"Invalid workspace number '%s'", argv[1]);
			}
			if (!(ws = workspace_by_number(argv[1]))) {
				char *name = join_args(argv + 1, argc - 1);
				ws = workspace_create(NULL, name);
				free(name);
			}
			if (ws && auto_back_and_forth) {
				ws = workspace_auto_back_and_forth(ws);
			}
		} else if (strcasecmp(argv[0], "next") == 0 ||
				strcasecmp(argv[0], "prev") == 0 ||
				strcasecmp(argv[0], "next_on_output") == 0 ||
				strcasecmp(argv[0], "prev_on_output") == 0 ||
				strcasecmp(argv[0], "current") == 0) {
			ws = workspace_by_name(argv[0]);
		} else if (strcasecmp(argv[0], "back_and_forth") == 0) {
			if (!seat->prev_workspace_name) {
				return cmd_results_new(CMD_INVALID,
						"There is no previous workspace");
			}
			if (!(ws = workspace_by_name(argv[0]))) {
				ws = workspace_create(NULL, seat->prev_workspace_name);
			}
		} else {
			char *name = join_args(argv, argc);
			if (!(ws = workspace_by_name(name))) {
				ws = workspace_create(NULL, name);
			}
			free(name);
			if (ws && auto_back_and_forth) {
				ws = workspace_auto_back_and_forth(ws);
			}
		}
		if (!ws) {
			return cmd_results_new(CMD_FAILURE, "No workspace to switch to");
		}
		workspace_switch(ws);
		seat_consider_warp_to_focus(seat);
	}
	return cmd_results_new(CMD_SUCCESS, NULL);
}
