package _VisualDVM.Passes.All;
import Common.Utils.Utils_;
import Common.Visual.Windows.ReadOnlyMultilineTextForm;
import _VisualDVM.ComponentsServer.Component.Component;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.ComponentsServerPass;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Utils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.List;
import java.util.Vector;
public class ShowComponentChangesLog extends ComponentsServerPass<Component> {
    File res;
    @Override
    public String getIconPath() {
        return "/Common/icons/Log.png";
    }
    @Override
    protected boolean isSafe() {
        return false;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (Global.components.getUI().CheckCurrent(Log)) {
            target = Global.components.getUI().getCurrent();
            return SendRequest(ServerCode.GetComponentChangesLog, target.getComponentType().toString());
        }
        return false;
    }
    @Override
    protected void performPreparation() throws Exception {
        res = Utils.getTempFileName("component_changes");
    }
    @Override
    protected void body() throws Exception {
        Utils_.bytesToFile((byte[]) request.server_response.object, res);
    }
    @Override
    protected boolean validate() {
        return res.exists();
    }
    @Override
    protected void showDone() throws Exception {
        ReadOnlyMultilineTextForm ff = new ReadOnlyMultilineTextForm();
        List<String> lines = FileUtils.readLines(res);
        Vector<String> res = new Vector<>();
        for (int i = lines.size() - 1; i >= 0; i--)
            res.add(lines.get(i));
        ff.ShowDialog("Журнал изменений компонента " +
                        Utils_.Brackets(target.getComponentType().getDescription()),
                String.join("\n", res)
        );
    }
}
