package org.jpos.tcpay.db.service;

import org.jpos.tcpay.ApplicationProperties;
import org.jpos.tcpay.acquirer.AcquirerNames;
import org.jpos.tcpay.acquirer.LengthMode;
import org.jpos.tcpay.db.entity.*;
import org.jpos.util.AppLogger;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

public class TransactionAuxUtils {

    public final EntityManagerFactory emf;
    private final AppLogger logger = new AppLogger();

    public  TransactionAuxUtils() {
        logger.log(new ApplicationProperties().getPersistenceProperties());
        emf = Persistence.createEntityManagerFactory("jpos-tcpay-local", new ApplicationProperties().getPersistenceProperties());
    }
    // Get AcquirerTerminal by POSTerminal


    public PosTerminal getPosTerminalByTerminalId(String posTerminalId) {
        EntityManager entityManager = emf.createEntityManager();
        try {
            TypedQuery<PosTerminal> terminalQuery = entityManager.createQuery("Select post from PosTerminal post where post.terminalid= :terminalId", PosTerminal.class);
            terminalQuery.setParameter("terminalId", posTerminalId);

            return terminalQuery.getSingleResult();
        } catch (Exception e) {
            logger.log("No reference for POS Terminal " + posTerminalId);
            return null;
        } finally {
            entityManager.close();
        }
    }

    public AcquirerConnection getAcquirerConnectionByAcquirer(Acquirer acquirer) {
        EntityManager entityManager = emf.createEntityManager();
        try {
            TypedQuery<AcquirerConnection> query = entityManager.createQuery("Select conn from AcquirerConnection conn where conn.acquirer= :acquirer", AcquirerConnection.class);
            query.setParameter("acquirer", acquirer);

            return query.getSingleResult();
        } catch (Exception e) {
            logger.log("No reference for Acquirer Connection " + acquirer);
            return null;
        } finally {
            entityManager.close();
        }
    }

/*
    public AcquirerTerminal getAcquirerTerminalByTerminalId(String posTerminalId) {
        return getDummmyAcquirerTerminal();
    }

    private AcquirerTerminal  getDummmyAcquirerTerminal() {
        AcquirerTerminal acquirerTerminal = new AcquirerTerminal();

        acquirerTerminal.setTerminalId("12345678").setAcquirerMerchant(getDummyAcquirerMerchant())
                .setAcquirer(getDummyAcquirer());

        return acquirerTerminal;
    }


    private AcquirerConnection getDummmyAcquirerConnection(Acquirer acquirer) {
        AcquirerConnection acquirerConnection = new AcquirerConnection();
        acquirerConnection.setAcquirer(getDummyAcquirer())
                .setIpAddress("testssl.tnsi.com.au").setPortNumber(45257).setSsl(true)
                .setKeyStorePath(null).setKeyStorePassword(null)
                .setTrustStorePath("cfg/fiserv/truststore.jks").setTrustStorePassword("password")
                .setTpdu("6000412000").setLengthMode(LengthMode.HEX);


        return acquirerConnection;
    }

    private Acquirer getDummyAcquirer() {
        Acquirer acq = new Acquirer();
        acq.setName(AcquirerNames.FISERV.getName()).setAcquirerInstitutionCode("").setNii("41");

        return acq;
    }

    private AcquirerMerchant getDummyAcquirerMerchant() {
        AcquirerMerchant acquirerMerchant = new AcquirerMerchant();
        acquirerMerchant.setAcquirer(getDummyAcquirer()).setMerchantId("123456781234567")
                .setMcc("5799").setAddress(getDummyAddress());

        return acquirerMerchant;
    }

    private Address getDummyAddress() {
        Address address = new Address();
        address.setLine1("Line1").setLine2("Line2").setLine3("Line3").setLine4("Line4")
                .setCity("Bangalore").setState("Karnataka").setCountry("India").setPostalCode("560100");

        return address;
    }*/
}
