/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.controller.admin;

import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.admin.constance.ClosingDayCfg;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.core.constance.LoanByUserCfg;
import com.arrebol.apc.model.loan.Loan;
import com.arrebol.apc.model.views.AvailablesOwnersView;
import com.arrebol.apc.model.views.CurrentCustomerByLoanView;
import com.arrebol.apc.model.views.constance.AvailablesOwnersViewCfg;
import com.arrebol.apc.model.views.constance.CurrentCustomerByLoanViewCfg;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
public class ChangeOwnerController implements Serializable {

    /**
     * Find all users that have loans by office.
     *
     * @param officeId
     * @return
     * @throws Exception
     */
    public List<AvailablesOwnersView> findAllCurrentOwners(String officeId) throws Exception {
        logger.info("findAllCurrentOwners");

        List<AvailablesOwnersView> results = new ArrayList();
        try {
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(AvailablesOwnersViewCfg.PARAM_OFFICE_ID, officeId));

            results = genericEntityRepository.xmlQueryAPCEntities(AvailablesOwnersView.class, AvailablesOwnersViewCfg.QUERY_FIND_ALL_CURRENT_OWNERS, parameters);
        } catch (Exception e) {
            logger.error("findAllCurrentOwners", e);
        }
        return results;
    }

    /**
     * Find all users in office execluding user id (currentOwnerId).
     *
     * @param officeId
     * @param currentOwnerId
     * @return
     * @throws Exception
     */
    public List<AvailablesOwnersView> findAllNewOwners(String officeId, String currentOwnerId) throws Exception {
        logger.info("findAllNewOwners");

        List<AvailablesOwnersView> results = new ArrayList();
        try {
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(AvailablesOwnersViewCfg.PARAM_OFFICE_ID, officeId));
            parameters.add(new ModelParameter(AvailablesOwnersViewCfg.USER_ID, currentOwnerId));

            results = genericEntityRepository.xmlQueryAPCEntities(AvailablesOwnersView.class, AvailablesOwnersViewCfg.QUERY_FIND_ALL_NEW_OWNERS, parameters);
        } catch (Exception e) {
            logger.error("findAllNewOwners", e);
        }
        return results;
    }

    /**
     * Find all loans that current owner has in status PENDING, TO_DELIVERY,
     * PENDING_RENOVATION and APPROVED.
     *
     * @param officeId
     * @param currentOwnerId
     * @return
     * @throws Exception
     */
    public List<CurrentCustomerByLoanView> findAllLoansByCurrentOwner(String officeId, String currentOwnerId) throws Exception {
        logger.info("findAllLoansByCurrentOwner");

        List<CurrentCustomerByLoanView> results = new ArrayList();
        try {
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(CurrentCustomerByLoanViewCfg.PARAM_OFFICE_ID, officeId));
            parameters.add(new ModelParameter(CurrentCustomerByLoanViewCfg.USER_ID, currentOwnerId));

            results = genericEntityRepository.xmlQueryAPCEntities(CurrentCustomerByLoanView.class, CurrentCustomerByLoanViewCfg.QUERY_FIND_ALL_LOANS_BY_CURRENT_OWNER, parameters);
        } catch (Exception e) {
            logger.error("findAllLoansByCurrentOwner", e);
        }
        return results;
    }

    /**
     *
     * @param newOwner
     * @param loans
     * @return
     */
    public boolean changeLoansBetweenUsers(String newOwner, List<Loan> loans) {
        logger.info("changeLoansBetweenUsers");

        boolean success = false;

        try {
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(LoanByUserCfg.FIELD_USER, new User(newOwner)));
            parameters.add(new ModelParameter(LoanByUserCfg.PARAMS_LOAN, loans));

            success = genericEntityRepository.xmlUpdateOrDeleteAPCEntity(LoanByUserCfg.QUERY_CHANGE_LOANS_BETWEEN_USERS_IN_LOAN_IDS, parameters);
        } catch (Exception e) {
            logger.error("changeLoansBetweenUsers", e);
        }

        return success;
    }

    private static final long serialVersionUID = 2051770456409752434L;
    final Logger logger = LogManager.getLogger(getClass().getName());

    private final GenericEntityRepository genericEntityRepository;

    public ChangeOwnerController() {
        this.genericEntityRepository = new GenericEntityRepository();
    }
}
