package com.casic.PgInterface.intelPlan.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgRoad;
import com.casic.PgInterface.devTable.manager.PgRoadManager;
import com.casic.PgInterface.intelPlan.domain.PgLineHole;
import com.casic.PgInterface.intelPlan.domain.PgPileAcu;
import com.casic.PgInterface.intelPlan.dto.PgLineHoleDTO;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PgLineHoleManager extends HibernateEntityDao<PgLineHole> {

    @Resource
    private PgPileAcuManager pgPileAcuManager;

    @Resource
    private PgRoadManager pgRoadManager;

    public List<PgLineHole> getPgLineHole(String lineName,Long pileAcuId){
        Criteria criteria=createCriteria(PgLineHole.class);

        criteria.add(Restrictions.eq("lineName",lineName));
        criteria.add(Restrictions.eq("pileAcuId",pileAcuId));
        criteria.add(Restrictions.eq("active",true));

        List<PgLineHole> pgLineHoleList=criteria.list();

        if(pgLineHoleList==null||pgLineHoleList.size()==0)
            return null;
        else
            return pgLineHoleList;
    }

    public boolean isExistLine(List<PgLineHole> pgLineHoleList,int row,int column){
        for(PgLineHole pgLineHole:pgLineHoleList){
            if(pgLineHole.getSecRow()==row&&pgLineHole.getSecColumn()==column)
                return true;
        }
        return false;
    }

    public List<PgLineHoleDTO> getCurrentHoleByAcu(Long pileAcuId){
        Criteria criteria=createCriteria(PgLineHole.class);

        criteria.add(Restrictions.eq("pileAcuId",pileAcuId));
        criteria.add(Restrictions.eq("active",true));

        List<PgLineHole> pgLineHoleList=criteria.list();
        List<PgLineHoleDTO> pgLineHoleDTOList=new ArrayList<>();
        if(pgLineHoleList==null||pgLineHoleList.size()==0)
            return null;
        else
            pgLineHoleDTOList=PgLineHoleDTO.ConvertToDTOList(pgLineHoleList,pgPileAcuManager,pgRoadManager);
        return pgLineHoleDTOList;
    }

    public void addPgLIneHole(List<PgPileAcu> pgPileAcuList,String lineName,String secRow,String secColumn){
        for(PgPileAcu pgPileAcu:pgPileAcuList){
            PgLineHole pgLineHole=new PgLineHole();
            pgLineHole.setLineName(lineName);
            pgLineHole.setPileAcuId(pgPileAcu.getId());
            pgLineHole.setSecColumn(Long.valueOf(secColumn));
            pgLineHole.setSecRow(Long.valueOf(secRow));
            pgLineHole.setActive(true);

            this.save(pgLineHole);
        }
    }

}