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 (line.contains("#LASTFIELD#")) {
                    line = line.replace("#LASTFIELD#", String.valueOf(lastField + 1));
                }
                if (line.contains("#DATETIME#")) {
                    line = line.replace("#DATETIME#", DateUtils.getFormattedTodayDateTime("yyyy-MM-dd'T'hh:mm:ss"));
                }
                if (line.contains("#YYYYMMDD#")) {
                    line = line.replace("#YYYYMMDD#", DateUtils.getFormattedTodayDate("yyyyMMdd"));
                }
                if (line.contains("#YYMMDD#")) {
                    line = line.replace("#YYMMDD#", DateUtils.getFormattedTodayDate("yyMMdd"));
                }
                if (line.contains("#YYYY-MM-DD#")) {
                    line = line.replace("#YYYY-MM-DD#", DateUtils.getFormattedTodayDate("yyyy-MM-dd"));
                }
                if (line.contains("#REF#")) {
                    line = line.replace("#REF#", String.valueOf(data.getTrxReference()));
                }
                if (line.contains("#IBAN#")) {
                    line = line.replace("#IBAN#", String.valueOf(data.getIban()));
                }
                if (line.contains("#RANDOM#")) {
                    line = line.replace("#RANDOM#", String.valueOf(data.getRandom()));
                }
                if (line.contains("#AMOUNT#")) {
                    line = line.replace("#AMOUNT#", String.valueOf(data.getAmount()));
                }
                if (line.contains("#AMOUNT1#")) {
                    line = line.replace("#AMOUNT1#", String.valueOf(data.getAmount1()));
                }
                if (line.contains("#CREDITOR#")) {
                    line = line.replace("#CREDITOR#", String.valueOf(data.getCreditorAcc()));
                }
                if (line.contains("#DEBTOR#")) {
                    line = line.replace("#DEBTOR#", String.valueOf(data.getDebtorAcc()));
                }
                if (line.contains("#BANKCODE#")) {
                    line = line.replace("#BANKCODE#", String.valueOf(data.getBankCode()));
                }
                if (line.contains("#MESSAGEID#")) {
                    line = line.replace("#MESSAGEID#", String.valueOf(data.getMessageId()));
                }
                if (line.contains("#FEETYPE#")) {
                    line = line.replace("#FEETYPE#", String.valueOf(data.getFeeType()));
                }
                if (line.contains("#HDTYPE#")) {
                    line = line.replace("#HDTYPE#", String.valueOf(data.getHdType()));
                }

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

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


}
