package cz.moneta.test.dsl.payment_engine.model;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.data.payment.PaymentData;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileForInputPreparation {
    protected Integer lastField;

    public File createTemporaryFileForInput(String templateFile, String newFileName) throws IOException {
        //create temporary file for uploading
        Path originalPath = Path.of(templateFile);
        Path copied = Paths.get(newFileName);
        return Files.copy(originalPath, copied).toFile();
    }

    public File prepareFileForInput(Harness harness, String templateFile, String newFileName, PaymentData data) throws IOException {

        //get document sample from resource
        BufferedReader reader = PaymentEngineUtils.getFileByFilePath(templateFile);
        String line = reader.readLine();

        //create temporary file for uploading
        File file = PaymentEngineUtils.createFile(newFileName);
        harness.log("File name: " + file.getName());
        harness.log("File path: " + file.getPath());

        //delete file in case of exception
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            while (line != null) {
                if (lastField != null) {
                    line = line.replace("#LASTFIELD#", String.valueOf(lastField + 1));
                }
                line = line.replace("#DATETIME#", DateUtils.getFormattedTodayDateTime("yyyy-MM-dd'T'hh:mm:ss"));
                line = line.replace("#YYYYMMDD#", DateUtils.getFormattedTodayDate("yyyyMMdd"));
                line = line.replace("#YYMMDD#", DateUtils.getFormattedTodayDate("yyMMdd"));
                line = line.replace("#YYYY-MM-DD#", DateUtils.getFormattedTodayDate("yyyy-MM-dd"));
                line = line.replace("#REF#", String.valueOf(data.getTrxReference()));
                line = line.replace("#IBAN#", String.valueOf(data.getIban()));
                line = line.replace("#RANDOM#", String.valueOf(data.getRandom()));
                line = line.replace("#AMOUNT#", String.valueOf(data.getAmount()));
                line = line.replace("#AMOUNT1#", String.valueOf(data.getAmount1()));
                line = line.replace("#CREDITOR#", String.valueOf(data.getCreditorAcc()));
                line = line.replace("#DEBTOR#", String.valueOf(data.getDebtorAcc()));
                line = line.replace("#BANKCODE#", String.valueOf(data.getBankCode()));
                line = line.replace("#MESSAGEID#", String.valueOf(data.getMessageId()));
                line = line.replace("#FEETYPE#", String.valueOf(data.getFeeType()));
                line = line.replace("#HDTYPE#", String.valueOf(data.getHdType()));
                line = line.replace("#UETR#", String.valueOf(data.getUetr()));
                line = line.replace("#CREATE_DATE#", DateUtils.getFormattedTodayDateTime("yyyy-MM-dd'T'hh:mm:ss") + "+00:00");
                line = line.replace("#CREATE_DATETIME#", DateUtils.getFormattedTodayDateTime("yyyy-MM-dd'T'hh:mm:ss") + "+01:00");

                writer.write(line);
                writer.newLine();
                line = reader.readLine();
                writer.flush();
            }
            writer.close();

        } catch (Exception e) {
            file.delete();
            throw new RuntimeException(e);
        }
        return file;
    }


}
