package com.fr.env;

import com.fr.design.RestartHelper;
import com.fr.design.dialog.link.MessageWithLink;
import com.fr.design.gui.ibutton.UIButton;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.itextarea.UITextArea;
import com.fr.design.i18n.Toolkit;
import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.mainframe.DesignerContext;
import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.general.CloudCenter;
import com.fr.general.GeneralContext;
import com.fr.general.IOUtils;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import com.fr.stable.StringUtils;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

/**
 * @author pengda
 * @version 10.0
 * Created on 2021-06-02
 */
public class SyncFailedPluginsDialog extends JDialog {
    private UILabel detailsLabel;
    private JScrollPane scrollPane;
    public SyncFailedPluginsDialog(Frame parent, JSONArray syncFailedPlugins) {
        super(parent, true);
        JPanel body = FRGUIPaneFactory.createBorderLayout_L_Pane();

        JPanel northPane = FRGUIPaneFactory.createBorderLayout_L_Pane();
        northPane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
        JPanel imagePanel = new JPanel();
        Icon icon = IOUtils.readIcon("com/fr/design/icon/versioncheck/warning.png");
        JLabel imageLabel = new JLabel();
        imageLabel.setIcon(icon);
        imagePanel.add(imageLabel);
        imagePanel.setPreferredSize(new Dimension(20, 20));

        JPanel messagePane = FRGUIPaneFactory.createBorderLayout_S_Pane();
        MessageWithLink messageWithLink = new MessageWithLink(Toolkit.i18nText("Fine-Design_Basic_Sync_Plugin_Fail_Suggestion"),Toolkit.i18nText("Fine-Design_Basic_Sync_Deal_Immediately"),
                CloudCenter.getInstance().acquireUrlByKind("help.installplugins", "https://help.fanruan.com/finereport/doc-view-2198.html"));
        messageWithLink.setPreferredSize(new Dimension(316, 20));

        messagePane.add(messageWithLink);
        messagePane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 0));

        northPane.add(imageLabel, BorderLayout.WEST);
        northPane.add(messagePane, BorderLayout.CENTER);

        JPanel centerPanel = FRGUIPaneFactory.createBorderLayout_S_Pane();
        detailsLabel = new UILabel(Toolkit.i18nText("Fine_Designer_Look_Detail"));
        detailsLabel.setIcon(UIManager.getIcon("OptionPane.narrow.down"));
        detailsLabel.addMouseListener(detailsLabelClickListener);
        JPanel detailsTitlePanel = FRGUIPaneFactory.createLeftFlowZeroGapBorderPane();
        detailsTitlePanel.add(detailsLabel);
        detailsTitlePanel.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));

        UITextArea detailsTextArea = new UITextArea();
        StringBuilder detailsText = new StringBuilder(StringUtils.EMPTY);
        for (int i = 0; i < syncFailedPlugins.size(); i++) {
            JSONObject plugin = syncFailedPlugins.getJSONObject(i);
            detailsText.append(plugin.getString("name")).append(",").append(Toolkit.i18nText("Fine-Design_Basic_Sync_Server_Version")).append(plugin.getString("version")).append("\n");
        }
        detailsTextArea.setText(detailsText.toString());
        scrollPane = new JScrollPane(detailsTextArea);
        centerPanel.add(detailsTitlePanel,BorderLayout.NORTH);
        centerPanel.add(scrollPane,BorderLayout.CENTER);

        JPanel southPane = FRGUIPaneFactory.createRightFlowInnerContainer_S_Pane();
        UIButton restartButton = new UIButton(Toolkit.i18nText("Fine-Design_Updater_Restart_Designer"));
        restartButton.addMouseListener(restartButtonClickListener);
        southPane.add(restartButton);

        body.add(northPane,BorderLayout.NORTH);
        body.add(centerPanel,BorderLayout.CENTER);
        body.add(southPane,BorderLayout.SOUTH);

        this.setTitle(Toolkit.i18nText("Fine-Design_Basic_Tool_Tips"));
        this.setResizable(false);
        this.add(body, BorderLayout.CENTER);
        this.setSize(new Dimension(GeneralContext.getLocale().equals(Locale.US) ? 400 : 380, 225));
        GUICoreUtils.centerWindow(this);
    }

    private MouseListener detailsLabelClickListener = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if(scrollPane.isVisible()){
                scrollPane.setVisible(false);
                detailsLabel.setIcon(UIManager.getIcon("OptionPane.narrow.right"));
            }else{
                scrollPane.setVisible(true);
                detailsLabel.setIcon(UIManager.getIcon("OptionPane.narrow.down"));
            }
        }
    };

    private MouseListener restartButtonClickListener = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            RestartHelper.restartForUpdate(DesignerContext.getDesignerFrame());
        }
    };
}