let usernames = [
"UID-0",
"UID-1",
"UID-2",
"UID-3",
"UID-4",
"UID-5",
"UID-6",
"UID-7",
"UID-8",
"UID-9",
"UID-10",
"UID-11",
"UID-12",
"UID-13",
"UID-14",
"UID-15",
"UID-16",
"UID-17",
"UID-18",
"UID-19",
"UID-20",
"UID-21",
"UID-22",
"UID-23",
"UID-24",
"UID-25",
"UID-26",
"UID-27",
"UID-28",
"UID-29",
"UID-30",
"UID-31",
"UID-32",
"UID-33",
"UID-34",
"UID-35",
"UID-36",
"UID-37",
"UID-38",
"UID-39",
"UID-40",
"UID-41",
"UID-42",
"UID-43",
"UID-44",
"UID-45",
"UID-46",
"UID-47",
"UID-48",
"UID-49",
"UID-50",
"UID-51",
"UID-52",
"UID-53",
"UID-54",
"UID-55",
"UID-56",
"UID-57",
"UID-58",
"UID-59",
"UID-60",
"UID-61",
"UID-62",
"UID-63",
"UID-64",
"UID-65",
"UID-66",
"UID-67",
"UID-68",
"UID-69",
"UID-70",
"UID-71",
"UID-72",
"UID-73",
"UID-74",
"UID-75",
"UID-76",
"UID-77",
"UID-78",
"UID-79",
"UID-80",
"UID-81",
"UID-82",
"UID-83",
"UID-84",
"UID-85",
"UID-86",
"UID-87",
"UID-88",
"UID-89",
"UID-90",
"UID-91",
"UID-92",
"UID-93",
"UID-94",
"UID-95",
"UID-96",
"UID-97",
"UID-98",
"UID-99",
"UID-100",
"UID-101",
"UID-102",
"UID-103",
"UID-104",
"UID-105",
"UID-106",
"UID-107",
"UID-108",
"UID-109",
"UID-110",
"UID-111",
"UID-112",
"UID-113",
"UID-114",
"UID-115",
"UID-116",
"UID-117",
"UID-118",
"UID-119",
"UID-120",
"UID-121",
"UID-122",
"UID-123",
"UID-124",
"UID-125",
"UID-126",
"UID-127",
"UID-128",
"UID-129",
"UID-130",
"UID-131",
"UID-132",
"UID-133",
"UID-134",
"UID-135",
"UID-136",
"UID-137",
"UID-138",
"UID-139",
"UID-140",
"UID-141",
"UID-142",
"UID-143",
"UID-144",
"UID-145",
"UID-146",
"UID-147",
"UID-148",
"UID-149",
"UID-150",
"UID-151",
"UID-152",
"UID-153",
"UID-154",
"UID-155",
"UID-156",
"UID-157",
"UID-158",
"UID-159",
"UID-160",
"UID-161",
"UID-162",
"UID-163",
"UID-164",
"UID-165",
"UID-166",
"UID-167",
"UID-168",
"UID-169",
"UID-170",
"UID-171",
"UID-172",
"UID-173",
"UID-174",
"UID-175",
"UID-176",
"UID-177",
"UID-178",
"UID-179",
"UID-180",
"UID-181",
"UID-182",
"UID-183",
"UID-184",
"UID-185",
"UID-186",
"UID-187",
"UID-188",
"UID-189",
"UID-190",
"UID-191",
"UID-192",
"UID-193",
"UID-194",
"UID-195",
"UID-196",
"UID-197",
"UID-198",
"UID-199",
"UID-200",
"UID-201",
"UID-202",
"UID-203",
"UID-204",
"UID-205",
"UID-206",
"UID-207",
"UID-208",
"UID-209",
"UID-210",
"UID-211",
"UID-212",
"UID-213",
"UID-214",
"UID-215",
"UID-216",
"UID-217",
"UID-218",
"UID-219",
"UID-220",
"UID-221",
"UID-222",
"UID-223",
"UID-224",
"UID-225",
"UID-226",
"UID-227",
"UID-228",
"UID-229",
"UID-230",
"UID-231",
"UID-232",
"UID-233",
"UID-234",
"UID-235",
"UID-236",
"UID-237",
"UID-238",
"UID-239",
"UID-240",
"UID-241",
"UID-242",
"UID-243",
"UID-244",
"UID-245",
"UID-246",
"UID-247",
"UID-248",
"UID-249",
"UID-250",
"UID-251",
"UID-252",
"UID-253",
"UID-254",
"UID-255",
"UID-256",
"UID-257",
"UID-258",
"UID-259",
"UID-260",
"UID-261",
"UID-262",
"UID-263",
"UID-264",
"UID-265",
"UID-266",
"UID-267",
"UID-268",
"UID-269",
"UID-270",
"UID-271",
"UID-272",
"UID-273",
"UID-274",
"UID-275",
"UID-276",
"UID-277",
"UID-278",
"UID-279",
"UID-280",
"UID-281",
"UID-282",
"UID-283",
"UID-284",
"UID-285",
"UID-286",
"UID-287",
"UID-288",
"UID-289",
"UID-290",
"UID-291",
"UID-292",
"UID-293",
"UID-294",
"UID-295",
"UID-296",
"UID-297",
"UID-298",
"UID-299",
"UID-300",
"UID-301",
"UID-302",
"UID-303",
"UID-304",
"UID-305",
"UID-306",
"UID-307",
"UID-308",
"UID-309",
"UID-310",
"UID-311",
"UID-312",
"UID-313",
"UID-314",
"UID-315",
"UID-316",
"UID-317",
"UID-318",
"UID-319",
"UID-320",
"UID-321",
"UID-322",
"UID-323",
"UID-324",
"UID-325",
"UID-326",
"UID-327",
"UID-328",
"UID-329"
];


if (location.pathname.startsWith('/gem/')) {
	document.documentElement.classList.add('cordified');
	console.log("We're on the gemcord");
	async function repeatTask() {
		try {
			let posts = document.querySelectorAll('.post:not(.excluded)');
			posts.forEach((post,post_index) => {
				post.classList.add('excluded');
				let intro = post.querySelector('.intro');
				let label = intro.querySelector('label');
				let img = label.querySelector('img');
				let uidstr = img.getAttribute('alt');
				let exceptNum = intro.querySelectorAll(':not(.post_no)');

				exceptNum.forEach(x => {
					x.remove();
				})
				let n_thing = intro.querySelector('[id^="post_no_"]');
				n_thing.remove();
				let num = intro.querySelector('*');
				let uid = uidstr.split(' ')[1] || '';
				num.innerHTML=uid;
				num.classList.add('chatlog__author');
				let uidnum = parseInt(uid);
				if (isNaN(uidnum))
				{
					num.innerHTML='flagfag';
				}
				else
				{
					num.innerText=usernames[uidnum];
				}
				//adding tags from a css that i stole from discord chat exporter


				let body = post.querySelector('.body');
				let pfpdiv = document.createElement("div");
				let pfp = document.createElement("img");
				pfpdiv.classList.add('chatlog__message-aside');
				pfpdiv.appendChild(pfp);

				post.querySelector('.intro').remove();




				let primaryContainer = document.createElement("div");
				post.classList.add('chatlog__message-primary');
				post.appendChild(pfpdiv);
				post.appendChild(primaryContainer);



				let headerContainer = document.createElement("div");

				headerContainer.appendChild(num);
				headerContainer.classList.add('chatlog__header');
				num.classList.add('chatlog__author');
				primaryContainer.appendChild(headerContainer);
				primaryContainer.appendChild(body);
				primaryContainer.classList.add('chatlog__message-primary');



				post.classList.add('chatlog__message');
				body.classList.add('chatlog__content');
				body.classList.add('chatlog__markdown');
				num.classList.add('chatlog__author');


				console.log("running for post, ", post_index);
				let filesHeader = post.querySelector('.files');
				if(filesHeader){
					console.log("files header found");
					let files = filesHeader.querySelectorAll('.file');
					if (files.length > 0)
					{
						console.log("files found");
						files.forEach((f,index) => {
							//only use the first as an attachment and discard the rest
							//for simplicity
							if (index == 0) 
							{
								console.log("main file found");
								let thumbnail = f.querySelector('a img');
								if(thumbnail) {
									console.log("thumbnail found");
									let attachmentHeader = document.createElement("div");
									attachmentHeader.classList.add("chatlog__attachment");
									attachmentHeader.classList.add("chatlog__attachment-media");
									primaryContainer.appendChild(attachmentHeader);
									attachmentHeader.appendChild(thumbnail);
									thumbnail.classList.add('chatlog__attachment-media');
								}

							}
							else
							{
								console.log("file of index ", index, " discarded");
								f.remove();
							}
						});
					}

					filesHeader.remove();
				}



			});



		} catch (e) {
			console.error(e);
		}
		setTimeout(repeatTask, 4000); // schedule next run in 4 seconds
	}

	repeatTask();
}