package com.liang.novel.plugin.ui;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.ActionLink;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.components.BorderLayoutPanel;
import com.liang.novel.plugin.actions.*;
import com.liang.novel.plugin.constants.FontConstants;
import com.liang.novel.plugin.pojo.Book;
import com.liang.novel.plugin.state.NovelState;


import javax.swing.*;
import java.awt.*;
import java.util.List;


public class ToolWindowUI {
    private JPanel contentPanel;

    private Project project;
    private NovelState novelState;
    private String fontType;
    private Integer fontSize;
    private List<Book> bookList;
    private Integer currentReadBookIndex;
    private Book book;

    private ReadUI readUI;

    public ToolWindowUI(Project project) {
        this.project = project;

        initContentPanel();
    }

    private void initContentPanel() {
        this.novelState = NovelState.getInstance();
        if (novelState.getUseCustomFont()) {
            this.fontType = novelState.getFontType();
            this.fontSize = novelState.getFontSize();
        } else {
            this.fontType = FontConstants.DEFAULT_FONT_TYPE;
            this.fontSize = FontConstants.DEFAULT_FONT_SIZE;
        }
        this.bookList = novelState.getBookList();
        this.currentReadBookIndex = novelState.getCurrentReadBookIndex();
        this.book = currentReadBookIndex != null ? bookList.get(currentReadBookIndex) : null;

        //边界布局面板
        BorderLayoutPanel borderLayoutPanel = new BorderLayoutPanel();
        borderLayoutPanel.add(getReadUIPanel());
        borderLayoutPanel.add(createActionToolbar().getComponent(), BorderLayout.WEST); //最左边放工具栏

        GridConstraints gridConstraints = new GridConstraints();
        gridConstraints.setFill(GridConstraints.FILL_BOTH); //上下左右全填满
        contentPanel.add(borderLayoutPanel, gridConstraints);
    }

    private Component getReadUIPanel() {
        if (bookList.isEmpty()) {
            ActionLink link = new ActionLink("请添加图书", e -> {
                ShowSettingsUtil.getInstance().showSettingsDialog(project, "lnovel");
            });
            return getCenteredComponent(link);
        } else if (currentReadBookIndex == null) {
            ActionLink link = new ActionLink("请选择图书", e -> {

            });
            return getCenteredComponent(link);
        } else {
            //初始化阅读面板
            readUI = new ReadUI(book, new Font(fontType, Font.PLAIN, fontSize), true);
            return readUI.getContentPanel();
        }
    }

    private Component getCenteredComponent(Component component) {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(component, new GridBagConstraints());
        panel.setBackground(JBColor.WHITE);
        return panel;
    }

    /**
     * 从ActionGroup创建工具栏
     */
    private ActionToolbar createActionToolbar() {
        DefaultActionGroup actionGroup = getActionGroup();
        ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("NovelToolBar", actionGroup, false);
        toolbar.setTargetComponent(contentPanel);
        return toolbar;
    }

    /**
     * 创建一个ActionGroup
     */
    private DefaultActionGroup getActionGroup() {
        DefaultActionGroup actionGroup = new DefaultActionGroup();
        if (bookList.isEmpty() || currentReadBookIndex == null || readUI == null) {
            actionGroup.add(new RefreshToolWindowUIAction(this));
        } else {
            actionGroup.add(new RefreshToolWindowUIAction(this));
            actionGroup.add(Separator.getInstance());
            actionGroup.add(new ToggleChapterAction(readUI));
            actionGroup.add(Separator.getInstance());
            actionGroup.add(new BookSelectAction(this));
            actionGroup.add(Separator.getInstance());
            actionGroup.add(new BookMarkAction(readUI));
            actionGroup.add(new BookMarkListAction(readUI));
            actionGroup.add(Separator.getInstance());
            actionGroup.add(new FontSizeIncreaseAction(readUI));
            actionGroup.add(new FontSizeDecreaseAction(readUI));
            actionGroup.add(Separator.getInstance());
            actionGroup.add(new NovelSettingAction());
        }
        return actionGroup;
    }

    public void updateReadUI() {
        contentPanel.remove(0);
        initContentPanel();
    }

    public JPanel getContentPanel() {
        return contentPanel;
    }

    public Book getCurrentReadBook() {
        return book;
    }

    public String getFontType() {
        return fontType;
    }

    public Integer getFontSize() {
        return fontSize;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPanel;
    }

}
