Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
"use strict";



if (window.handyman === undefined) {
	let hmn = {};
	window.handyman = {};
	
	hmn.build_ui_core = () => {
		hmn.ui = {};
		
		hmn.ui.frame = new OO.ui.PanelLayout({
			expanded: false,
    		framed: true
		});
		hmn.ui.innerFrame_a = new OO.ui.PanelLayout({
		    expanded: false,
		    padded: true
		});

		hmn.ui.frame.$element.append(
			hmn.ui.innerFrame_a.$element
		);
		
		hmn.ui_target.append(hmn.ui.frame.$element);
	};
	
	/* BEGIN RCP */
	
	hmn.build_rcp_ui = async () => {
		hmn.build_ui_core();
	};
	
	/* END OF RCP | BEGIN FILTER TESTER */
	
	hmn.build_filter_ui = async () => {
		hmn.ui = {};
		await window.avtools.load_libavtools_filters();
		let css = await window.avtools.load_json_file("User:Moonythedwarf/HandymanFilterInterface.css"); //totally JSON.
		let st = $("<style>");
		hmn.ui.style = st;
		st.text(css);
		hmn.ui_target.append(st);
		let f = JSON.parse(await window.avtools.load_json_file("User:Moonythedwarf/HandymanFilterInterface.json"));
		window.libcui.build_ui(f, hmn.ui, hmn.ui_target);
		//hmn.ui.submit.$element.addClass("fullwidth");
		hmn.ui.submit.on("click", hmn.filter_test_run_filters);
		
		/*
		await window.avtools.load_libavtools_filters();
		hmn.build_ui_core();
		
		hmn.ui.innerFrame_b = new OO.ui.PanelLayout({
		    expanded: false,
		    padded: true
		});
		
		hmn.ui.frame.$element.append(
			$("<hr>"),
			hmn.ui.innerFrame_b.$element
		);
		
		hmn.ui.filter_type_select = new OO.ui.DropdownInputWidget({
			options: [
				{ data: "username_filters", label: "Username" },
				{ data: "content_filters", label: "Content" },
				{ data: "summary_filters", label: "Summary" },
			]
		});
		
		hmn.ui.filter_text_input = new OO.ui.MultilineTextInputWidget({
			placeholder: "Text to filter",
		})
		
		let submit = new OO.ui.ButtonInputWidget({
        	label: "Check filters"
		});
		
		hmn.ui.fields = new OO.ui.FieldsetLayout({
        	label: "Handyman Filter Testing"
    	});
		
		hmn.ui.fields.addItems([
			new OO.ui.FieldLayout(hmn.ui.filter_text_input),
			new OO.ui.FieldLayout(hmn.ui.filter_type_select, {
				label: "Select filter type:",
				align: "top"
			}),
			new OO.ui.FieldLayout(submit)
		]);
		
		
		
		//TODO: pretty JSON output.
		hmn.ui.output_text = $("<p>");
		hmn.ui.output_text.css("white-space", "pre-wrap");
		
		hmn.ui.output_frame = new OO.ui.PanelLayout({
			expanded: false,
    		framed: true,
    		padded: true,
		});
		
		hmn.ui.output_frame.$element.append(hmn.ui.output_text);
		
		hmn.ui.innerFrame_a.$element.append(hmn.ui.fields.$element, submit.$element);
		hmn.ui.innerFrame_b.$element.append(hmn.ui.output_frame.$element);
		*/
	}
	
	hmn.filter_test_run_filters = () => {
		let avt = window.avtools;
		
		let results = avt.match_against_filters(avt[hmn.ui.filter_type_select.value], hmn.ui.filter_text_input.value);
		
		let i = $("<p>");
		hmn.ui.outputFrame.$element.text(JSON.stringify(results, null, 2));
	}
	
	/* END FILTER TESTER | BEGIN CONFIG UI */
	
	hmn.build_config_ui = async () => {
		hmn.ui = {};
		let f = JSON.parse(await window.avtools.load_json_file("User:Moonythedwarf/HandymanConfigInterface.json"));
		window.libcui.build_ui(f, hmn.ui, hmn.ui_target);
		hmn.ui.reload_avtools_config_btn.on("click", () => {
			window.avtools.wipe_cache();
			location.reload();
		});
		/*
		hmn.build_ui_core();
		hmn.ui.reload_avtools_config_btn = new OO.ui.ButtonWidget({
			label: "Reload libavtools configuration and data (Unsaved config changes will be lost!)",
			flags: "destructive",
		});
		hmn.ui.save_
		
		hmn.ui.innerFrame_a.$element.append(
			$("<hr>"),
			hmn.ui.reload_avtools_config_btn.$element
		);
		*/
	}
	
	/* END CONFIG UI */
	
	hmn.interfaces = [
		"Moonythedwarf/HandymanRCPInterface",
		"Moonythedwarf/HandymanFilterInterface",
		"Moonythedwarf/HandymanConfigInterface",
		"Moonythedwarf/HandymanPatrolFeed",
	];
	
	$(async () => {
		// libavtools takes a fairly significant amount of time to load fully, so only load it on pages effected by handyman.
		try {
		if (hmn.interfaces.includes(mw.config.get("wgTitle"))) {
			let startTime = performance.now();
			mw.notify("Handyman loading...");
			await Promise.all([
				mw.loader.getScript("/w/index.php?title=User:Moonythedwarf/libavtools.js&action=raw&ctype=text/javascript"),
				mw.loader.getScript("/w/index.php?title=User:Moonythedwarf/libcui.js&action=raw&ctype=text/javascript"),
				mw.loader.using(["oojs", "oojs-ui-core", "oojs-ui-widgets", "oojs-ui-toolbars"]),
			]);
			
			await window.avtools.load_libavtools_core();
			
			hmn.ui_target = $("#mw-content-text");
			
			switch (mw.config.get("wgTitle")) {
				case "Moonythedwarf/HandymanRCPInterface":
					await hmn.build_rcp_ui();
					break;
				case "Moonythedwarf/HandymanFilterInterface":
					await hmn.build_filter_ui();
					break;
				case "Moonythedwarf/HandymanConfigInterface":
					await hmn.build_config_ui();
					break;
				case "Moonythedwarf/HandymanPatrolFeed":
					await mw.loader.getScript("/w/index.php?title=User:Moonythedwarf/handyman-usernames.js&action=raw&ctype=text/javascript")
					window.handyman.uns.build_usernamefeed_ui();
					break;
				default:
					throw new Error("unimplemented interface");
			}
			let endTime = performance.now();
			mw.notify(`Loaded in ${endTime-startTime}ms`);
		}
		} catch (e) {
			mw.notify(e);
		}
	});
}