class ColorMode {
	constructor(options={}) {
		if(!ColorMode._instance) {
			this.init(options);
			ColorMode._instance = this;
		}
		else
			return ColorMode._instance;
	}
	
	init(options={}) {
		if(options.themechangeid == undefined)
			options.themechangeid = 'themechange';
		
		var mode = ColorMode.getModeSetting();
		var theme = ColorMode.getThemeSetting();
		this.set(mode, theme);
			
		window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
			this.set(ColorMode.getModeSetting(), ColorMode.getThemeSetting());
		});
		
		var instance = this;
		$('#'+options.themechangeid).click(function(e) {
			var mode = (instance.get() == 'light') ? 'dark' : 'light';
			instance.set(mode, ColorMode.getThemeSetting());
		});
	}
	
	static getRealMode(cmode) {
		cmode = cmode.toLowerCase();
		if(cmode != 'light' && cmode != 'dark' && cmode != 'auto')
			cmode = 'auto';
		return (cmode == 'auto' ? ((window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) ? 'dark' : 'light') : cmode);
	}
	
	get() {
		return this.current_color_mode;
	}

	set(cmode='auto', theme="Default") {
		var mode = ColorMode.getRealMode(cmode);
		if(!theme)
			theme = "Default";
		
		if(mode == this.get() && theme == this.current_theme)
			return;
	
		let transparency = getCookieInt(COOKIE_CONTENT_TRANSPARENCY, 5);
		let pixels = getCookieInt(COOKIE_CONTENT_BLURFILTER, 5);
		this.setContentTransparencyAndBlur(transparency, pixels, mode);
		
		$('.control img, .header img').each(function() {
			var src = $(this).attr('src').replaceAll('/light/', '/'+mode+'/').replaceAll('/dark/', '/'+mode+'/');
			src = src.replaceAll('light.css', mode + '.css').replaceAll('dark.css', mode + '.css');
			$(this).attr('src', src)
		});
		
		$('.theme-bg, input:not([type="submit"])').each(function() {
			var bgimg = $(this).css('background-image');
			bgimg = bgimg.replaceAll('/light/', '/'+mode+'/').replaceAll('/dark/', '/'+mode+'/');
			if(bgimg != "" && bgimg != "none")
				$(this).css('background-image', bgimg);
		});
		
		if(typeof main_background_image != "undefined" && main_background_image != "")
			$('body').css('background', 'url(' + main_background_image + ') ' + main_background_prop);
		if(typeof main_logo_image != "undefined" && main_logo_image != "")
			$('.logo').css('background-image', 'url(' + main_logo_image + ')');
		if(typeof main_logo_big_image != "undefined" && main_logo_big_image != "")
			$('.biglogo').css('background-image', 'url(' + main_logo_big_image + ')');
		if(typeof main_background_size  != "undefined" && main_background_size  != "")
			$('body').css('background-size', main_background_size);
		
		$('#theme-mode-css').attr('href', 'lib/layouts/colors/' + mode + '/' + mode + '.css');
		$('#theme-default-css').attr('href', 'lib/layouts/colors/' + mode + '/themes/Default/default.css');
		
		var instance = this;
		var turl = AppModules.getRootURL() + 'lib/layouts/colors/' + mode + '/themes/' + theme + '/styles.css';
		$.ajax({url: turl, type:'HEAD',
			error: function() {
				$('#theme-styles-css').attr('href', 'lib/layouts/colors/' + mode + '/themes/Default/styles.css');
			},
			success: function() {
				$('#theme-styles-css').attr('href', 'lib/layouts/colors/' + mode + '/themes/' + theme + '/styles.css');
				instance.current_theme = theme;
			}
		});
		document.body.classList.toggle('dark-mode', mode === 'dark');
		
		this.current_color_mode = mode;
	}
	
	setContentTransparencyAndBlur(transparency, pixels, mode) {
		$('#theme-bg-css').attr('href', 'lib/php/styles.php?mode=' + mode + "&tr=" + transparency + "&bl=" + pixels);
	}
	
	static getModeSetting() {
		var mode = getCookie(COOKIE_THEME_COLOR_MODE, 'auto');
		if(mode != 'auto' && mode != 'light' && mode != 'dark')
			mode = 'auto';
		return mode;
	}
	
	static getThemeSetting() {
		return getCookie(COOKIE_THEME_THEME_NAME, 'Default');
	}
	
	static getMode() {
		if(!ColorMode._instance)
			new ColorMode();
		return ColorMode._instance.get();
	}
	
	static getTheme() {
		if(!ColorMode._instance)
			new ColorMode();
		return ColorMode._instance.current_theme;
	}
	
	static setTransparency(transparency) {
		if(!ColorMode._instance)
			new ColorMode();
		let pixels = getCookieInt(COOKIE_CONTENT_BLURFILTER, 5);
		return ColorMode._instance.setContentTransparencyAndBlur(transparency, pixels, ColorMode._instance.get());
	}
	
	static setFilterBlur(pixels) {
		if(!ColorMode._instance)
			new ColorMode();
		let transparency = getCookieInt(COOKIE_CONTENT_TRANSPARENCY, 5);
		return ColorMode._instance.setContentTransparencyAndBlur(transparency, pixels, ColorMode._instance.get());
	}
}
