'use strict';
const {ipcRenderer} = require('electron');
const BaseSection = require(__dirname + '/base-section.js');
const ConfigUtil = require(__dirname + '/../../utils/config-util.js');
class NetworkSection extends BaseSection {
	constructor(props) {
		super();
		this.props = props;
	}
	template() {
		return `
            
                Proxy
                
					
						Connect servers through a proxy
						
					 
					
				 
             
		`;
	}
	init() {
		this.props.$root.innerHTML = this.template();
		this.$proxyPAC = document.querySelector('#proxy-pac-option .setting-input-value');
		this.$proxyRules = document.querySelector('#proxy-rules-option .setting-input-value');
		this.$proxyBypass = document.querySelector('#proxy-bypass-option .setting-input-value');
		this.$proxySaveAction = document.getElementById('proxy-save-action');
		this.$settingBlock = this.props.$root.querySelector('.setting-block');
		this.initProxyOption();
		this.$proxyPAC.value = ConfigUtil.getConfigItem('proxyPAC', '');
		this.$proxyRules.value = ConfigUtil.getConfigItem('proxyRules', '');
		this.$proxyBypass.value = ConfigUtil.getConfigItem('proxyBypass', '');
		this.$proxySaveAction.addEventListener('click', () => {
			ConfigUtil.setConfigItem('proxyPAC', this.$proxyPAC.value);
			ConfigUtil.setConfigItem('proxyRules', this.$proxyRules.value);
			ConfigUtil.setConfigItem('proxyBypass', this.$proxyBypass.value);
			ipcRenderer.send('forward-message', 'reload-proxy', true);
		});
	}
	initProxyOption() {
		const proxyEnabled = ConfigUtil.getConfigItem('useProxy', false);
		this.toggleProxySettings(proxyEnabled);
		this.updateProxyOption();
	}
	toggleProxySettings(option) {
		if (option) {
			this.$settingBlock.classList.remove('hidden');
		} else {
			this.$settingBlock.classList.add('hidden');
		}
	}
	updateProxyOption() {
		this.generateSettingOption({
			$element: document.querySelector('#use-proxy-option .setting-control'),
			value: ConfigUtil.getConfigItem('useProxy', false),
			clickHandler: () => {
				const newValue = !ConfigUtil.getConfigItem('useProxy');
				ConfigUtil.setConfigItem('useProxy', newValue);
				this.toggleProxySettings(newValue);
				if (newValue === false) {
					// Reload proxy if the proxy is turned off
					ipcRenderer.send('forward-message', 'reload-proxy', false);
				}
				this.updateProxyOption();
			}
		});
	}
}
module.exports = NetworkSection;