package com.casic.PgInterface.intelPlan.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.intelPlan.domain.PgLineHole;
import com.casic.PgInterface.intelPlan.domain.PgPileAcu;
import com.casic.PgInterface.intelPlan.domain.PgSection;
import com.casic.PgInterface.intelPlan.dto.PgPlanResult;
import com.casic.PgInterface.intelPlan.dto.PgPlanResultRe;
import com.casic.PgInterface.intelPlan.dto.PgPlanTask;
import org.springframework.stereotype.Service;

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

@Service
public class PgPileAcuManager extends HibernateEntityDao<PgPileAcu> {

    @Resource
    private PgSectionManager pgSectionManager;

    @Resource
    private PgLineHoleManager pgLineHoleManager;

    public PgPileAcu getPgPileAcuByCode(String acuCode){
        List<PgPileAcu> pgPileAcuList=this.findBy("acuCode",acuCode);

        if(pgPileAcuList==null) return null;
        else
            return pgPileAcuList.get(0);
    }

    public List<PgPileAcu> getPileAcuListByPileAcuStr(List<String> pileAcuList){
        List<PgPileAcu> pgPileAcuList=new ArrayList<>();

        try{
            for(String pileAcu:pileAcuList){
                PgPileAcu pgPileAcu=getPgPileAcuByCode(pileAcu);
                if(pgPileAcu==null){
                    pgPileAcuList=null;
                    break;
                }
                pgPileAcuList.add(pgPileAcu);
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            pgPileAcuList=null;
        }

        return pgPileAcuList;
    }

    public List<PgPlanResultRe> getPgPlanResultList(List<PgPlanTask> pgPlanTaskList){

        List<PgPlanResultRe> pgPlanResultReList=new ArrayList<>();

        for(PgPlanTask pgPlanTask:pgPlanTaskList){
            List<PgPlanResult> pgPlanResultList=getPgPlanResultByPlantask(pgPlanTask);
            PgPlanResultRe pgPlanResultRe=new PgPlanResultRe();
            pgPlanResultRe.setTaskKey(pgPlanTask.getTaskKey());
            pgPlanResultRe.setPgPlanResultList(pgPlanResultList);

            pgPlanResultReList.add(pgPlanResultRe);
        }

        return pgPlanResultReList;
    }

    /*
    获取规划结果
     */
    private List<PgPlanResult> getPgPlanResultByPlantask(PgPlanTask pgPlanTask){

        List<PgPlanResult> pgPlanResultList=new ArrayList<>();

        try{
            String lineName=pgPlanTask.getLineName();
            List<PgPileAcu> pgPileAcuList=getPileAcuListByPileAcuStr(pgPlanTask.getPileAcuList());
            if(pgPileAcuList==null) return null;

            Long raodId=pgPileAcuList.get(0).getRoadId();
            //获取pgPlanResultList
            PgSection pgSection=pgSectionManager.getPgSectionByRoadAndLine(lineName,raodId);
            pgPlanResultList=getPlanResult(lineName,pgPileAcuList,pgSection);
        }
        catch(Exception ex){
            ex.printStackTrace();
            pgPlanResultList=null;
        }

        return pgPlanResultList;
    }

    private List<PgPlanResult> getPlanResult(String lineName,List<PgPileAcu> pgPileAcuList,PgSection pgSection){

        List<PgPlanResult> pgPlanResultList=new ArrayList<>();
        int rowCount=pgSection.getSecRow().intValue();
        int columnCount=pgSection.getSecColumn().intValue();

        for(int row=0;row<rowCount;row++) {
            for(int column=0;column<columnCount;column++){
                if(canPlanLine(lineName,pgPileAcuList,row,column))
                {
                    PgPlanResult pgPlanResult=new PgPlanResult();
                    pgPlanResult.setLineName(lineName);
                    pgPlanResult.setSecColumn(column);
                    pgPlanResult.setSecRow(row);
                    pgPlanResult.setLength(getAcuListLength(pgPileAcuList));
                    pgPlanResultList.add(pgPlanResult);

                    return pgPlanResultList;
                }
            }
        }

        return null;
    }

    private boolean canPlanLine(String lineName,List<PgPileAcu> pgPileAcuList,int row,int column){
        try {
            for (PgPileAcu pgPileAcu : pgPileAcuList) {
                List<PgLineHole> pgLineHoleList = pgLineHoleManager.getPgLineHole(lineName, pgPileAcu.getId());
                if(pgLineHoleList!=null&&pgLineHoleList.size()!=0&&pgLineHoleManager.isExistLine(pgLineHoleList, row, column))
                return false;
            }
            return true;
        }
        catch(Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    public Long getAcuListLength(List<PgPileAcu> pgPileAcuList){

        long length=0;
        for(PgPileAcu pgPileAcu:pgPileAcuList){
            length+=pgPileAcu.getLength();
        }

        return length;
    }

}