/*
 * MIT License
 *
 * Copyright (c) 2018-2019 Daniel Cortes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */


package danielcortes.xyz.models.ingreso;

import danielcortes.xyz.data.ConnectionHolder;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.caja.CajaDAO;
import danielcortes.xyz.models.caja.MysqlCajaDAO;
import danielcortes.xyz.models.tipo_ingreso.MysqlTipoIngresoDAO;
import danielcortes.xyz.models.tipo_ingreso.TipoIngreso;
import danielcortes.xyz.models.tipo_ingreso.TipoIngresoDAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public abstract class IngresoDAO {
    protected ConnectionHolder connectionHolder;

    public abstract List<Ingreso> findAll();
    public abstract List<Ingreso> findByCaja(Caja caja);
    public abstract List<Ingreso> findById(int id);
    public abstract List<Ingreso> findByTipoIngreso(TipoIngreso tipoIngreso);

    public abstract boolean insertIngreso(Ingreso ingreso);
    public abstract boolean updateIngreso(Ingreso ingreso);
    public abstract boolean deleteIngreso(Ingreso ingreso);

    public abstract int getTotalIngreso(Caja caja);

    List<Ingreso> ingresosFromResultSet(ResultSet rs) throws SQLException {
        ArrayList<Ingreso> ingresosList = new ArrayList<>();
        while(rs.next()){
            int tipoIngresoId = rs.getInt("tipo_ingreso_id");
            TipoIngresoDAO tipoEgresoDAO = new MysqlTipoIngresoDAO();
            TipoIngreso tipoIngreso = tipoEgresoDAO.findById(tipoIngresoId).get(0);

            int cajaId = rs.getInt("caja_id");
            CajaDAO cajaDAO = new MysqlCajaDAO();
            Caja caja = cajaDAO.findById(cajaId);

            Ingreso ingreso = new Ingreso();

            ingreso.setId(rs.getInt("id"));
            ingreso.setValor(rs.getInt("valor"));
            ingreso.setNroZInicial(rs.getString("nro_z_inicial"));
            ingreso.setNroZFinal(rs.getString("nro_z_final"));
            ingreso.setNroInicial(rs.getString("nro_inicial"));
            ingreso.setNroFinal(rs.getString("nro_final"));
            ingreso.setTipoIngreso(tipoIngreso);
            ingreso.setCaja(caja);

            ingresosList.add(ingreso);
        }
        return ingresosList;
    }
}
