package ru.upos;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import static ru.upos.UposCommandFlags.*;

/**
 * Параметры операции, выполняемой на терминале
 */
public class Parameters {

    private static final String paramsFileName = "pinpad.ini";
    private static final String defaultSerialPort = "9";
    private static final String serialPortSectionName = "ComPort";
    private static final String defaultShowScreens = "0";
    private static final String showScreensSectionName = "ShowScreens";

    private byte serialPort;
    private UposOperation operation;
    private long amount;
    private long amountAdditional;
    private long flags;

    public Parameters() {
        serialPort = Byte.valueOf(defaultSerialPort); // default value
        amount = 0;
        amountAdditional = 0;
        flags = TlvResponse.getFlagValue(); // default value
    }

    public Parameters(Parameters parameters) {
        this.serialPort = parameters.serialPort;
        this.operation = parameters.operation;
        this.amount = parameters.amount;
        this.amountAdditional = parameters.amountAdditional;
        this.flags = parameters.flags;
    }

    public byte getSerialPort() {
        return serialPort;
    }

    public UposOperation getOperation() {
        return operation;
    }

    public long getAmount() {
        return amount;
    }

    public long getAmountAdditional() {
        return amountAdditional;
    }

    public long getFlags() {
        return flags;
    }

    private void parseIniFile() {
        byte showScreensFlag;
        Properties properties = new Properties();

        try {
            properties.load(new FileInputStream(new File(paramsFileName)));
            serialPort = Byte.valueOf(properties.getProperty(serialPortSectionName, defaultSerialPort));
            showScreensFlag = Byte.valueOf(properties.getProperty(showScreensSectionName, defaultShowScreens));
            if (showScreensFlag != 0) {
                flags |= RemoteUi.getFlagValue();
            }
        } catch (Exception e) {
            System.out.println("Parameters file " + paramsFileName + " is absent or has wrong format");
            System.out.println("Default settings will be used");
        }
    }

    public void parseParams(String[] args) throws Exception {
        operation = UposOperation.getUposOperation(args[0]);
        if (args.length != operation.getParametersNum()) {
            throw new Exception("Incorrect parameters num for operation " + operation.name());
        }
        switch (operation) {
        case Purchase:
        case Cash:
        case Refund:
            amount = Long.parseLong(args[1]);
            break;
        case PurchaseWithCashback:
            amount = Long.parseLong(args[1]);
            amountAdditional = Long.parseLong(args[2]);
            break;
        }
        parseIniFile();
    }

    public void logParameters() {
        System.out.println("COM port: " + serialPort);
        System.out.println("Operation: " + operation);
        System.out.println("Amount: " + amount);
        if (operation != null && operation.name().equals(UposOperation.PurchaseWithCashback.name())) {
            System.out.println("Amount additional: " + amountAdditional);
        }
        System.out.println(String.format("Flags: %08X", flags));
    }
}
