package _VisualDVM.Passes.All;
import Common.Visual.Trees.DataTree;
import Common.Visual.Windows.Dialog.Dialog;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Server.ComponentsServerPass;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Utils;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Vector;
public class ResurrectComponentFromServer extends ComponentsServerPass<_VisualDVM.ComponentsServer.Component.Component> {
    Vector<RemoteFile> backups; //не забывать что файлы на СЕРВЕРЕ.
    RemoteFile remoteFile;
    File localFile;
    @Override
    protected boolean isSafe() {
        return false;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        remoteFile = null;
        localFile = null;
        if (Global.components.getUI().CheckCurrent(Log) && Global.mainModule.getPass(PassCode.GetComponentsBackupsFromServer).Do()) {
            //1. Получить список бекапов.
            target = Global.components.getUI().getCurrent();
            backups = (Vector<RemoteFile>) Global.mainModule.getPass(PassCode.GetComponentsBackupsFromServer).target;
            backups.sort((o1, o2) -> o2.name.compareTo(o1.name));
            Dialog d = new Dialog(null) {
                @Override
                public int getDefaultWidth() {
                    return 300;
                }
                @Override
                public int getDefaultHeight() {
                    return 400;
                }
                @Override
                public void CreateContent() {
                    DefaultMutableTreeNode root = new DefaultMutableTreeNode("(скрыть)");
                    for (RemoteFile file : backups) {
                        root.add(new DefaultMutableTreeNode(file));
                    }
                    DataTree tree = new DataTree(root) {
                        {
                            setRootVisible(false);
                            setCellRenderer(new DefaultTreeCellRenderer() {
                                @Override
                                public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                                    super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                                    Object o = ((DefaultMutableTreeNode) value).getUserObject();
                                    if (o instanceof RemoteFile) {
                                        RemoteFile file = (RemoteFile) o;
                                        setText(file.name);
                                    }
                                    return this;
                                }
                            });
                        }
                        @Override
                        public Current currentName() {
                            return Current.ComponentServerBackup;
                        }
                    };
                    content = tree;
                }
            };
            if (d.ShowDialog("Выбор версии для восстановления") && Global.mainModule.Check(Log, Current.ComponentServerBackup)) {
                remoteFile = Global.mainModule.getComponentServerBackup();
                return SendRequest(ServerCode.ReceiveFile, remoteFile.full_name);
            }
        }
        return false;
    }
    @Override
    protected void body() throws Exception {
        //1. Скачать файл.
        localFile = Utils.getTempFileName(remoteFile.name);
        request.server_response.Unpack(localFile);
        //------------>>
        Files.copy(localFile.toPath(), target.getNewFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
        target.Update();
    }
    @Override
    protected void performDone() throws Exception {
        target.InitialVersionCheck();
        if (target.CanBeUpdated())
            target.CheckIfNeedsUpdateOrPublish();
    }
    @Override
    protected void showDone() throws Exception {
        Global.components.refreshUpdatesStatus();
    }
}
