class AuthorityControl {
	constructor() {
		this.authorities = new DatasetControl({
			prefix:"authority",
			prefixName:"권한",
			keymapper:info => info.AUTH_ID,
			dataGetter:obj => obj.authorityList,
			formats: {
				REG_DT:datetimeFormat
			}
		});
		
		this.authorities.isBuiltIn = (info) => {
			let current = info || this.authorities.getCurrent();
			return current && current.AUTH_TYPE < 2;
		};
		
		this.authorities.isImplicit = (info) => {
			let current = info || this.authorities.getCurrent();
			return current && current.AUTH_TYPE === 1;
		};
		
		this.authorities.isAdmin = (info) => {
			let current = info || this.authorities.getCurrent();
			return current && current.AUTH_TYPE === 0;
		};
		
		this.users = new DatasetControl({
			prefix:"user",
			prefixName:"사용자",
			keymapper:info => info.AUTH_ID + "-" + info.USER_ID,
			dataGetter:obj => obj.userList,
			formats: {
				REG_DT:datetimeFormat
			},
			urls:{
				load:wctx.url("/authority/user/list.do")
			}
		});
		
		this.actions = new DatasetControl({
			prefix:"action",
			prefixName:"기능 그룹",
			keymapper:info => info.AUTH_ID + "-" + info.GRP_ID,
			dataGetter:obj => obj.actionList,
			formats: {
				REG_DT:datetimeFormat
			},
			urls:{
				load:wctx.url("/authority/action/list.do")
			}
		});
		
		this.linkedList = "users";
		
		this.authorities.onDatasetChange = obj => this.onAuthorityListChange(obj);
		this.authorities.onCurrentChange = item => {
			this.onCurrentAuthorityChange(item);
			let info = item ? item.data : null;
			this.getLinkedList(this.linkedList, info);
		};
		this.authorities.onSelectionChange = selected => this.onAuthoritySelect(selected);

		this.users.onDatasetChange = obj => this.onUserListChange(obj);
		this.users.onCurrentChange = item => this.onCurrentUserChange(item);
		this.users.onSelectionChange = selected => this.onUserSelect(selected);

		this.actions.onDatasetChange = obj => this.onActionListChange(obj);
		this.actions.onCurrentChange = item => this.onCurrentActionChange(item);
		this.actions.onSelectionChange = selected => this.onActionSelect(selected);
	}
		
	getLinkedList(linked, info) {
		if (!info)
			info = this.authorities.getCurrent();
		switch (this.linkedList = linked) {
			case "users":
				if (info && !this.authorities.isImplicit(info)) {
					if (this.users.query.authIDs == info.AUTH_ID) return;
					this.users.query.authIDs = info.AUTH_ID;
					this.users.load();
				} else {
					this.users.query.authIDs = info.AUTH_ID;
					this.users.dataset.clear();
				}
				break;
			case "actions":
				if (info && !this.authorities.isAdmin(info)) {
					if (this.actions.query.authIDs == info.AUTH_ID) return;
					this.actions.query.authIDs = info.AUTH_ID;
					this.actions.load();
				} else {
					this.actions.query.authIDs = info.AUTH_ID;
					this.actions.dataset.clear();
				}
				break;
			default: break;
		}
	}
	
	onAuthorityListChange(obj) {}
	
	onCurrentAuthorityChange(item) {}
	
	onAuthoritySelect(selected) {}
		
	removeAuthorities() {
		let authIDs = this.authorities.dataset.getKeys("selected");
		this.authorities.remove({authIDs:authIDs.join(",")});
	}
	
	onUserListChange(obj) {}
	
	onCurrentUserChange(item) {}
	
	onUserSelect(selected) {}
	
	onActionListChange(obj) {}
	
	onCurrentActionChange(item) {}
	
	onActionSelect(selected) {}
	
	async addUsers() {
		let userIDs = await new UserControl().selectUser(true);
		let authID = this.authorities.dataset.getCurrent().AUTH_ID;
		json.post({
			url:wctx.url("/authority/user/add.do"),
			data:{
				authID:authID,
				userIDs:userIDs.join(",")
			},
			success:resp => {
				if (resp.saved)
					this.users._load();
			}
		});
	}
	
	removeUsers() {
		let selected = this.users.dataset.getDataset("selected").map(info => info.USER_ID);
		let authID = this.authorities.dataset.getCurrent().AUTH_ID;
		json.post({
			url:wctx.url("/authority/user/remove.do"),
			data:{
				authID:authID,
				userIDs:selected.join(",")
			},
			success:resp => {
				if (resp.saved)
					this.users._load();
			}
		});
	}
	
	async addActions() {
		let actions = await new ActionGroupControl(false).selectActionGroup(true);
		let authID = this.authorities.dataset.getCurrent().AUTH_ID;
		
		json.post({
			url:wctx.url("/authority/action/add.do"),
			data:{
				authID:authID,
				groupIDs:actions.join(",")
			},
			success:resp => {
				if (resp.saved)
					this.actions._load();
			}
		});
	}
	
	removeActions() {
		let selected = this.actions.dataset.getDataset("selected").map(info => info.GRP_ID);
		let authID = this.authorities.dataset.getCurrent().AUTH_ID;
		json.post({
			url:wctx.url("/authority/action/remove.do"),
			data:{
				authID:authID,
				groupIDs:selected.join(",")
			},
			success:resp => {
				if (resp.saved)
					this.actions._load();
			}
		});
	}
}
