package Visual_DVM_2021.Passes.All;
import Common.Visual.Windows.Dialog.Text.ReadOnlyMultilineTextForm;
import _VisualDVM.Utils;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Repository.Server.ServerExchangeUnit_2021;
import Common.Passes.PassException;
import Visual_DVM_2021.Passes.Server.TestingSystemPass;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.List;
import java.util.Vector;
public class ShowTestingServerFile extends TestingSystemPass<RemoteFile> {
    String title;
    File localFile;
    @Override
    protected boolean canStart(Object... args) throws Exception {
        title = (String) args[0];
        target = (RemoteFile) args[1];
        return true;
    }
    @Override
    protected void ServerAction() throws Exception {
        Command(new ServerExchangeUnit_2021(ServerCode.ReceiveFile, target.full_name));
        localFile = Utils.getTempFileName(target.name);
        if (response.object != null)
            response.Unpack(localFile);
        else throw new PassException("Файл не найден");
    }
    @Override
    protected boolean validate() {
        return localFile.exists();
    }
    @Override
    protected void showDone() throws Exception {
        ReadOnlyMultilineTextForm ff = new ReadOnlyMultilineTextForm();
        List<String> lines = FileUtils.readLines(localFile);
        Vector<String> res = new Vector<>();
        for (int i = lines.size() - 1; i >= 0; i--)
            res.add(lines.get(i));
        ff.ShowDialog(title,
                String.join("\n", res)
        );
    }
}
