// Copyright (c) 2017 kroppy. All rights reserved.
// Use of this source code is governed by a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) license
// that can be found at https://creativecommons.org/licenses/by-nc-nd/4.0/

// **********       GROUPS FUNCTIONS        ***************

function SaveGroups() {
	chrome.runtime.sendMessage({command: "save_groups", groups: bggroups, windowId: CurrentWindowId});
}
function AppendGroups(Groups) {
	// var scroll = $("#group_list").scrollTop();
	for (var group in Groups) {
		AppendGroupToList(Groups[group].id, Groups[group].name, Groups[group].font);
	}
	setTimeout(function() {
		RearrangeGroupsButtons(0);
	}, 1000);
}

function RearrangeGroupsButtons(stack) {
	$(".group_button").each(function() {
		let groupId = (this.id).substr(1);
		if (bggroups[groupId]) {
			if ($("#group_list").children().eq(bggroups[groupId].index)[0] && $(this).index() > bggroups[groupId].index) {
				$(this).insertBefore($("#group_list").children().eq(bggroups[groupId].index)[0]);
			} else {
				if ($("#group_list").children().eq(bggroups[groupId].index)[0] && $(this).index() < bggroups[groupId].index) {
					$(this).insertAfter($("#group_list").children().eq(bggroups[groupId].index)[0]);
				}
			}
			if ($(this).index() != bggroups[groupId].index && stack < 10) {
				RearrangeGroupsButtons(stack+1);
			}
		}
	});
}
function RearrangeGroupsLists() {
	let scroll = $("#"+active_group).scrollTop();
	$(".group_button").each(function() {
		let groupId = (this.id).substr(1);
		if ($("#"+groupId)[0]) {
			$("#groups").append($("#"+groupId));
		}
	});
	$("#"+active_group).scrollTop(scroll);
}

function AppendGroupToList(groupId, group_name, font_color) {
	if ($("#"+groupId).length == 0 && $("#groups")[0]) {
		var grp = document.createElement("div"); grp.className = "group"; grp.id = groupId; $("#groups")[0].appendChild(grp);
		var gcf = document.createElement("div"); gcf.className = "children_folders"; gcf.id = "cf"+groupId; grp.appendChild(gcf);
		var gct = document.createElement("div"); gct.className = "children_tabs"; gct.id = "ch"+groupId; grp.appendChild(gct);
	}
	if ($("#_"+groupId).length == 0) {
		var gbn = document.createElement("div"); gbn.className = "group_button"; gbn.id = "_"+groupId; $("#group_list")[0].appendChild(gbn);
		var gte = document.createElement("span"); gte.className = "group_title"; gte.id = "_gte"+groupId; gte.textContent = group_name; if (font_color != "") {gte.style.color = "#"+font_color;} gbn.appendChild(gte);
		var gtd = document.createElement("div"); gtd.className = "group_drag_box"; gtd.draggable = true; gtd.id = "-"+groupId; gbn.appendChild(gtd);
	}
	if (groupId != active_group) {
		$("#"+groupId).hide();
	}
	RefreshGUI();
	
	if (opt.switch_with_scroll) {
		BindTabsSwitchingToMouseWheel();
	}
}

function GenerateNewGroupID(){
	var newID = "g_"+GenerateRandomID();
	if ($("#"+newID)[0]) {
		GenerateNewGroupID();
	} else {
		return newID;
	}
}

function AddNewGroup(p) {
	var newId = GenerateNewGroupID();
	bggroups[newId] = { id: newId, index: 0, active_tab: 0, active_tab_ttid: "", name: (p.name ? p.name : caption_noname_group), font:  (p.font ? p.font : "")  };
	AppendGroupToList(newId, bggroups[newId].name, bggroups[newId].font);
	UpdateBgGroupsOrder();
	return newId;
}

// function AppendTabsToGroup(p) {
// }

// remove group, delete tabs if close_tabs is true
function GroupRemove(groupId, close_tabs) {
	if (close_tabs) {
		CloseTabs($("#"+groupId).find(".tab").map(function() {return parseInt(this.id);}).toArray());
		$("#"+groupId+" .folder").each(function() {
			RemoveFolder(this.id);
		});
	} else {
		$("#cf"+groupId).children().each(function() {
			$("#cftab_list").append(this);
		});
		$("#ch"+groupId).children().each(function() {
			$("#chtab_list").append(this);
		});
		RefreshExpandStates();
		RefreshCounters();
	}
	delete bggroups[groupId];
	
	if ($("#"+groupId)[0].id == active_group) {
		if ($("#_"+groupId).prev(".group_button")[0]) {
			SetActiveGroup(($("#_"+groupId).prev(".group_button")[0].id).substr(1), true, true);
		} else {
			if ($("#_"+groupId).next(".group_button")[0]) {
				SetActiveGroup(($("#_"+groupId).next(".group_button")[0].id).substr(1), true, true);
			} else {
				SetActiveGroup("tab_list", true, true);
			}
		}
	}
	SaveGroups();
	$("#"+groupId).remove();
	$("#_"+groupId).remove();
	schedule_update_data++;
}

function UpdateBgGroupsOrder() {
	$(".group_button").each(function() {
		if (bggroups[(this.id).substr(1)]) {
			bggroups[(this.id).substr(1)].index = $(this).index();
		}
	});
	SaveGroups();
}

function SetActiveGroup(groupId, switch_to_active_in_group, scroll_to_active) {
	log("function: SetActiveGroup");
	if ($("#"+groupId)[0]) {
		active_group = groupId;
		$(".group_button").removeClass("active_group");
		$("#_"+groupId).addClass("active_group");
		$(".tab, .group").hide();
		$("#"+groupId).show();
		$("#"+groupId+" .tab").show();
		RefreshGUI();
		$("#group_edit").hide();
		if (switch_to_active_in_group && $("#"+groupId+" .active_tab")[0]){
			chrome.tabs.update(parseInt($("#"+groupId+" .active_tab")[0].id), {active: true});
		}
		if (scroll_to_active && $("#"+groupId+" .active_tab")[0]){
			ScrollToTab($("#"+groupId+" .active_tab")[0].id);
		}
		if (groupId == "tab_list") {
			$("#button_remove_group, #button_edit_group").addClass("disabled");
		} else {
			$("#button_remove_group, #button_edit_group").removeClass("disabled");
		}
		chrome.runtime.sendMessage({command: "set_active_group", active_group: groupId, windowId: CurrentWindowId});
		RefreshExpandStates();
		RefreshCounters();
	}
}

function SetActiveTabInGroup(groupId, tabId) {
	if ($(".tab#"+tabId)[0] /* && $("#"+tabId).parents(".group")[0] */ && bggroups[groupId] != undefined) {
		// bggroups[$("#"+tabId).parents(".group")[0].id].active_tab = parseInt(tabId);
		// if ($("#"+tabId).parents(".group")[0].id != active_group) {
		if (groupId != active_group) {
			// SetActiveGroup($("#"+tabId).parents(".group")[0].id, false, true);
			SetActiveGroup(groupId, false, true);
		}
		if (bggroups[groupId]) {
			bggroups[groupId].active_tab = parseInt(tabId);
		}
		SaveGroups();
	}
}

// direction == true goes up, false goes down
// function ScrollGroupList(direction) {
	// if (direction) {
		// $("#group_list").scrollTop($("#group_list").scrollTop()-3);
	// }
	// if (!direction) {
		// $("#group_list").scrollTop($("#group_list").scrollTop()+3);
	// }
	// if (IOKeys.LMB) {
		// setTimeout(function() { ScrollGroupList(direction); },10);
	// }
// }

// function ScrollToGroup(groupId) {
	// if ($("#"+groupId).offset().top-$("#group_list").offset().top < 1) {
		// $("#group_list").scrollTop($("#group_list").scrollTop()+$("#"+groupId).offset().top-$("#group_list").offset().top-1);
	// } else {
		// if ($("#"+groupId).offset().top+$("#"+groupId).outerHeight()+1 > $("#group_list").offset().top+$("#group_list").innerHeight()) {
			// $("#group_list").scrollTop($("#group_list").scrollTop()+$("#"+groupId).offset().top-$("#group_list").offset().top-$("#group_list").innerHeight()+$("#"+groupId).outerHeight()-1);
		// }
	// }
// }

// Edit group popup
function ShowGroupEditWindow() {
	if (bggroups[menuItemId]) {
		$("#group_edit_name")[0].value = bggroups[menuItemId].name;
		$("#group_edit").css({"display": "block", "top": $("#toolbar_groups").offset().top + 8, "left": 22});
		$("#group_edit_font").css({"background-color": bggroups[menuItemId].font == "" ? "var(--button_icons, #808080)" : "#"+bggroups[menuItemId].font});
	}
}

// when pressed OK in group popup
function GroupEditConfirm() {
	if (bggroups[menuItemId]) {
		$("#group_edit_name")[0].value = $("#group_edit_name")[0].value.replace(/[\f\n\r\v\t\<\>\+\-\(\)\.\,\;\:\~\/\|\?\@\!\"\'\£\$\%\&\^\#\=\*\[\]]?/gi, "");
		bggroups[menuItemId].name = $("#group_edit_name")[0].value;
		bggroups[menuItemId].font = RGBtoHex($("#group_edit_font").css("background-color"));
		$(".edit_dialog").hide(0);
		$(".group_title#_gte" +menuItemId).css({"color": "#"+bggroups[menuItemId].font});
		RefreshGUI();
		SaveGroups();
	}
}

// "Move to group" popup
// function ShowMoveToGroupWindow(x, y) {
	// $(".move_to_group_menu_entry").remove();
	// bggroups.forEach(function(group) {
		// if (vt.ActiveGroup != group.g) {
			// var	li = document.createElement("li");
				// li.id = "move_to_"+group.g;
				// li.className = "menu_item move_to_group_menu_entry";
				// li.innerHTML = group.n;
			// $("#move_to_group_menu")[0].appendChild(li);
		// }
	// });
	// if (x >= $(window).width()-$("#tabs_menu").outerWidth()) {
		// x = $(window).width()-$("#tabs_menu").outerWidth();
	// }
	// if (y >= $(window).height()-$("#move_to_group_menu").outerHeight()-20) {
		// y = $(window).height()-$("#move_to_group_menu").outerHeight();
	// }
	// $("#move_to_group_menu").css({"display": "block", "top": y-24, "left": x-20});
// }


// **********         GROUPS EVENTS         ***************

function RestoreStateOfGroupsToolbar() {
	chrome.runtime.sendMessage({command: "get_group_bar", windowId: CurrentWindowId}, function(response) {
		$("#toolbar_groups").css({"display": "inline-block"});
		if (response == true) {
			$("#toolbar_groups").removeClass("hidden");
			$("#toolbar_groups").css({"width": "19px", "border-right": "1px solid var(--group_list_borders)"});
		} else {
			$("#toolbar_groups").addClass("hidden");
			$("#toolbar_groups").css({"width": "0px", "border-right": "none"});
		}
	});
}

function SetGroupEvents() {
			
	// activate group
	$(document).on("click", ".group_button", function(event) {
		// menuGroupId = (this.id).substr(1);
		if (event.button == 0) {
			SetActiveGroup((this.id).substr(1), true, true);
		}
	});

	// show/hide groups toolbar
	$(document).on("mousedown", "#button_groups_toolbar_hide", function(event) {
		if (event.button == 0) {
			$("#toolbar_groups").toggleClass("hidden");
			if ($("#toolbar_groups").is(".hidden")) {
				$("#toolbar_groups").css({"width": "0px", "border-right": "none"});
				chrome.runtime.sendMessage({command: "set_group_bar", group_bar: false, windowId: CurrentWindowId});
			} else {
				$("#toolbar_groups").css({"width": "19px", "border-right": "1px solid var(--group_list_borders)"});
				chrome.runtime.sendMessage({command: "set_group_bar", group_bar: true, windowId: CurrentWindowId});
			}
			RefreshGUI();
		}
	});


	// edit group dialog box
	$(document).on("mousedown", "#group_edit_discard", function(event) {
		$(".edit_dialog").hide(0);
	});
	$("#group_edit_name").keyup(function(e) {
		if (e.keyCode == 13) {
			GroupEditConfirm();
		}
	});
	$(document).on("mousedown", "#group_edit_confirm", function(event) {
		GroupEditConfirm();
	});

	// show color picker
	$(document).on("mousedown", "#group_edit_font, #group_edit_background", function(event) {
		event.stopPropagation();
		PickColor = this.id;
		$("#color_picker")[0].value = "#"+RGBtoHex($(this).css("background-color"));
		$("#color_picker").focus();
		$("#color_picker").click();
	});
	
	$(document).on("input", "#color_picker", function(event) {
		$("#"+PickColor).css({"background-color": $("#color_picker")[0].value});
	});	


	// edit group
	$(document).on("dblclick", ".group_button:not(#_tab_list)", function(event) {
		if (event.button == 0) {
			menuItemId = (this.id).substr(1);
			ShowGroupEditWindow();
		}
	});



	// scroll groups
	// $(document).on("mousedown", "#scroll_group_up, #scroll_group_down", function(event) {
		// IOKeys.LMB = true;
		// ScrollGroupList($(this).is("#scroll_group_up"));
	// });
	// $(document).on("mouseleave", "#scroll_group_up, #scroll_group_down", function(event) {
		// IOKeys.LMB = false;
	// });

	// remove tabs from group button
	// $(document).on("mousedown", "#remove_tabs_from_group", function(event) {
		// if (event.button == 0 && vt.ActiveGroup.match("at|ut") == null) {
			// AppendTabsToGroup({tabsIds: $(".tab.selected_tab:visible").map(function() {return parseInt(this.id);}).toArray(), groupId: "ut", SwitchTabIfHasActive: true, insertAfter: true, moveTabs: true});
		// }
	// });


	// remove group
	// $(document).on("mousedown", ".group", function(event) {
		// if (event.button == 1 || IOKeys.Shift) {
			// if (IOKeys.Shift) {
				// GroupRemove($(this)[0].id, true);
			// } else {
				// GroupRemove($(this)[0].id, false);
			// }
		// }
	// });

}