// 控制器基类
class Controller {
    constructor(model, view, manager, acceptedTypeSet) {
        this.model = model;
        this.view = view;
        this.manager = manager;
        this.acceptedTypeSet = acceptedTypeSet;
    }

    // 关闭多任务视图
    closeMultitaskView() {
        this.manager.close();
    }

    // 窗口点击事件处理
    onWindowClicked(windowId) {}

    // 删除预览窗口
    removePreviewWindow(windowId) {}

    // 设置默认态
    setDefaultStatus() {
        this.view.setDefaultStatus();
    }

    // 设置 Hover 态
    setHoverStatus() {
        this.view.setHoverStatus();
    }

    //设置点击状态
    setClickStatus()
    {
        this.view.setClickStatus();
    }

    // 设置拖拽态
    setDragStatus() {}

    // 设置拖拽接收态
    setReceiveStatus() {}

    // 自己进入目标
    onEnterTarget(srcDesktopIndex, destDesktopIndex) {}

    // 自己离开目标
    onExitTarget() {}

    // 来源进入自己
    onSourceEntered(source) {}

    // 来源离开自己
    onSourceExited(source) {}

    // 自己被释放
    onDropSource(source, srcDesktopIndex, destDesktopIndex) {}

    // 接收来源对象
    acceptSource(source, srcDesktopIndex, destDesktopIndex) {}

    // 拒绝来源对象
    rejectSource(source) {
        source.returnOriginal(source.posX,source.posY);
    }

    // 是否合法来源
    validateSource(source) {
        return this.acceptedTypeSet.indexOf(source.windowType) > -1;
    }

    // 来源释放在自己区域
    onSourceDropped(source, srcDesktopIndex, destDesktopIndex) {
        if (this.validateSource(source)) {
            this.acceptSource(source, srcDesktopIndex, destDesktopIndex);
        }
        else
        {
            this.rejectSource(source);
        }
    }
}

class AppController extends Controller {
    constructor(model, view, manager, acceptedTypeSet) {
        super(model, view, manager, acceptedTypeSet);
    }

    // 窗口点击事件处理
    onWindowClicked(windowId) {
        this.model.activateAppWindow(windowId);
        this.manager.close();
    }

    // 删除预览窗口
    removePreviewWindow(windowId) {
        if (this.model.hasModalChildWindow(windowId))
        {
            this.onWindowClicked(windowId);
        }
        else
        {
            this.model.removeAppWindow(windowId);
        }
    }

    // 设置拖拽态
    setDragStatus() {
        this.view.setDragStatus();
    }

    // 自己进入目标
    onEnterTarget(srcDesktopIndex, destDesktopIndex) {
        this.view.onEnterDropArea();
    }

    // 自己离开目标
    onExitTarget() {
        this.view.onExitDropArea();
    }

    // 自己被释放
    onDropSource(source, srcDesktopIndex, destDesktopIndex) {
        this.model.moveAppWindowToDesktop(source.wid, srcDesktopIndex, destDesktopIndex);
    }
}

class DesktopController extends Controller {
    constructor(model, view, manager, acceptedTypeSet) {
        super(model, view, manager, acceptedTypeSet);
    }

    // 窗口点击事件处理
    onWindowClicked(desktopIndex) {
        this.manager.close();
    }

    // 删除预览窗口
    removePreviewWindow(desktopIndex) {
        this.model.removeDesktop(desktopIndex);
    }

    // 设置 Hover 态
    setHoverStatus() {
        this.view.setHoverStatus();
        this.model.setCurrentDesktop(this.view.wid);
    }

    // 设置拖拽态
    setDragStatus() {
        this.view.setDragStatus();
    }

    // 设置拖拽接收态
    setReceiveStatus() {
        this.view.setReceiveStatus();
    }

    // 自己进入目标
    onEnterTarget(srcDesktopIndex, destDesktopIndex) {
        // 进入动画靠 虚拟桌面预览区域的 ListView 自带提供，不需调用
        this.model.moveDesktop(srcDesktopIndex, destDesktopIndex);
    }

    // 来源进入自己
    onSourceEntered(source, srcDesktopIndex, destDesktopIndex) {
        this.setReceiveStatus();
        source.controller.onEnterTarget(srcDesktopIndex, destDesktopIndex);
    }

    // 来源离开自己
    onSourceExited(source) {
        source.controller.onExitTarget();
        this.setDefaultStatus();
    }

    // 接收来源对象
    acceptSource(source, srcDesktopIndex, destDesktopIndex) {
        source.controller.onDropSource(source, srcDesktopIndex, destDesktopIndex);
    }
}

class CloseButtonController extends Controller {
    constructor(model, view, manager, acceptedTypeSet) {
        super(model, view, manager, acceptedTypeSet);
    }
}

class CreationController extends Controller {
    constructor(model, view, manager, acceptedTypeSet) {
        super(model, view, manager, acceptedTypeSet);
    }

    // 窗口点击事件处理
    onWindowClicked(/*unused*/) {
        this.model.appendDesktop();
    }

    // 设置拖拽接收态
    setReceiveStatus() {
        this.view.setReceiveStatus();
    }

    // 来源进入自己
    onSourceEntered(source) {
        if (!validateSource(source)) {
            return;
        }
        this.setReceiveStatus();
        source.controller.onEnterTarget(srcDesktopIndex, destDesktopIndex);
    }

    // 来源离开自己
    onSourceExited(source) {
        if (!validateSource(source)) {
            return;
        }
        this.setDefaultStatus();
        source.controller.onExitTarget();
    }

    // 接收来源对象
    acceptSource(source, srcDesktopIndex, destDesktopIndex) {
        this.model.appendDesktop();
        source.controller.onDropSource(source, srcDesktopIndex, destDesktopIndex);
        // 设置新建的虚拟桌面为当前虚拟桌面。
    }
}
