package com.axis.innovators.box.browser;

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
 * @author tzdwindows 7
 */
public class WindowOperationHandler {
    private final WindowRegistry registry;
    private final Map<String, Consumer<String>> operations;
    private final Component attachedComponent;

    public static class Builder {
        private WindowRegistry registry = WindowRegistry.getInstance();
        private final Map<String, Consumer<String>> operations = new ConcurrentHashMap<>();
        private Component attachedComponent;

        public Builder attachTo(Component component) {
            this.attachedComponent = component;
            return this;
        }
        public Builder withDefaultOperations() {
            this.operations.put("open", target -> {
                registry.getWindow(target).setVisible(true);
            });
            this.operations.put("close", target -> {
                if (target != null) {
                    System.out.println("Close window: " + target);
                    registry.unregisterWindow(target);
                } else if (attachedComponent != null) {
                    Window window = SwingUtilities.getWindowAncestor(attachedComponent);
                    if (window instanceof BrowserWindow) {
                        ((BrowserWindow) window).closeWindow();
                    }
                }
            });
            return this;
        }

            public Builder onOperation(String operation, Consumer<String> handler) {
            this.operations.put(operation, handler);
            return this;
        }

        public WindowOperationHandler build() {
            return new WindowOperationHandler(this);
        }

        private void handleOpen(String targetWindow) {
            registry.createNewWindow(targetWindow, builder ->
                    builder.title("New Window")
            );
        }

        private void handleClose(String targetWindow) {
            if (targetWindow != null) {
                registry.unregisterWindow(targetWindow);
            } else {
                handleCurrentWindowClose();
            }
        }

        private void handleCurrentWindowClose() {
            if (attachedComponent != null) {
                BrowserWindow currentWindow = (BrowserWindow)
                        SwingUtilities.getWindowAncestor(attachedComponent);
                if (currentWindow != null) {
                    registry.unregisterWindow(currentWindow.getWindowId());
                }
            }
        }

    }

    private WindowOperationHandler(Builder builder) {
        this.registry = builder.registry;
        this.attachedComponent = builder.attachedComponent;
        this.operations = new ConcurrentHashMap<>(builder.operations);
    }

    public void handleOperation(WindowOperation operation) {
        Consumer<String> handler = operations.get(operation.getType());
        if (handler != null) {
            handler.accept(operation.getTargetWindow());
            operation.getCallback().success("操作成功: " + operation.getType());
        } else {
            operation.getCallback().failure(-1, "未定义的操作: " + operation.getType());
        }
    }
}