class App{
	setCookie(name,value,expires,path){var _path=(!path)?'/':path;var _expires=new Date(expires*1000).toUTCString();document.cookie=name+'='+value+';expires='+_expires+';path='+_path+';samesite=Lax;';}
	getCookie(name){var cookie=decodeURIComponent(document.cookie).split(';');var cookieCount=cookie.length;var _name=name+'=';for (var i=0;i<cookieCount;i++) if (cookie[i].trim().indexOf(_name)==0) return cookie[i].trim().substring(_name.length);return undefined;}
	clip(text){navigator.clipboard.writeText(text);}
	thumbChange=(o,data=null,name='')=>{
		var xhr=new XMLHttpRequest();
		xhr.onload=()=>{o.style.backgroundImage='url('+this.wroot+xhr.responseText+')';}
		xhr.open('post',location);
		var imgData=new FormData();
		imgData.append('cmd','imageDrop');
		if (data===null) imgData.append('image',o.files[0]);
		else imgData.append('image',data,name);
		xhr.send(imgData);
	}
	thumbDrop=(o,e)=>{e.preventDefault();
		if (!e.dataTransfer.files.length){
			var xhr=new XMLHttpRequest();
			var url= /src="?([^"\s]+)"?\s*/.exec(e.dataTransfer.getData('text/html'))[1];
			xhr.open('get',url);
			var name=url.substr(url.lastIndexOf('/')+1);
			xhr.responseType='blob';
			xhr.onload=()=>{this.thumbChange(o,xhr.response,name);}
			xhr.send();
		}
		else{o.files=e.dataTransfer.files;this.thumbChange(o);}
	}
	thumbDragover=(o,e)=>{e.preventDefault();e.dataTransfer.dropEffect="link";}
	toggleTheme(){
		var themes=document.getElementsByClassName('themeSheet'),themeName=document.getElementById('themeSheet').content,cookie=this.getCookie('theme'),current=0;
		if (cookie!=undefined) themeName=cookie;
		if (themeName!=undefined)for(current=0;current<themes.length;current++)if(themes[current].title==themeName)break;
		if (current==themes.length) current=0;
		current++;if (current==themes.length) current=0;
		this.setCookie('theme',themes[current].title);
		for(var i=0;i<themes.length;i++)themes[i].setAttribute('disabled','');
		themes[current].removeAttribute('disabled');
		document.getElementById('themeSheet').content=themes[current].title;
	}
	toggleSidebar(menu){var w=menu.style.width;w=((!w)||(w=='0px')||(w=='0'))?'var(--sideroom)':0;menu.style.width=w;
//		this.setCookie('sidebar',w?1:0);
	}
	acclimateMenus(){
		let icon,container=document.getElementById('header_submenu'),iconsize=3*this.rem,width=innerWidth,list,css;
		if (container){
			var header=document.getElementById('header');
			for (let item of container.children){if (item.children.length>0) header.appendChild(item.children[0]);}
			container.remove();
			container=null;
			let icon=document.getElementById('submenu_icon');
			if (icon) icon.remove();
		}
		list=document.getElementsByClassName('logo');
		for(let item of list){if (item.parentElement.id=='header'){css=getComputedStyle(item);width-=item.offsetWidth;width-=parseInt(css.padding);}}
		list=document.getElementsByClassName('hamburger');
		for(let item of list){if (item.parentElement.id=='header'){css=getComputedStyle(item);width-=item.offsetWidth;width-=parseInt(css.padding);}}
		list=document.getElementsByClassName('headicon');
		for(let item of list){if (item.parentElement.id=='header'){css=getComputedStyle(item);width-=item.offsetWidth;width-=parseInt(css.padding);}}
		width-=iconsize;
		list=Array.from(document.getElementsByClassName('menu'));
		for (var i=0;i<list.length;i++){
			let item=list[i];
			if (item.parentElement.id=='header'){
				width-=item.offsetWidth+2;
				if (width<=0){
					if (!container){
						var menuBtn=document.createElement('img');
						menuBtn.className='headicon';
						menuBtn.id='submenu_icon';
						menuBtn.src=this.wroot+'/main/assets/icons/menu.png';
						menuBtn.onclick=()=>{
							if (container.style.display!='none') container.style.display='none';
							else container.style.display='inline-block';
						}
						document.getElementById('header').appendChild(menuBtn);
						container=document.createElement('ul');
						container.style.display='none';
						container.id='header_submenu';
					}
					var li=document.createElement('li');
					container.append(li);
					li.appendChild(item);
				}
			}
		}
		if (container) document.body.append(container);
	}
	resize=(e,args)=>{
		this.acclimateMenus();
	}
	constructor(args){
		this.wroot=args[0];
		this.rem=parseInt(getComputedStyle(document.documentElement).fontSize);
		this.acclimateMenus();
		addEventListener('resize',this.resize);
	}
}
