package com.cidic.equipment.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.cidic.equipment.dao.VehicleInfoDao;
import com.cidic.equipment.model.User;
import com.cidic.equipment.model.VehicleColor;
import com.cidic.equipment.model.VehicleInfo;
import com.cidic.equipment.model.VehicleTexture;

@Repository
@Component
@Qualifier(value = "vehicleInfoDaoImpl")
public class VehicleInfoDaoImpl implements VehicleInfoDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public void createVehicleInfo(VehicleInfo vehicleInfo) {
		Session session = this.getSessionFactory().getCurrentSession();
		
		for (VehicleColor color : vehicleInfo.getVehicleColors()){
			color.setVehicleInfo(vehicleInfo);
		
		}
		
		for (VehicleTexture textures : vehicleInfo.getVehicleTextures()){
			textures.setVehicleInfo(vehicleInfo);
		}
		session.save(vehicleInfo);
	}

	@Override
	public void updateVehicleInfo(VehicleInfo vehicleInfo) {
		Session session = this.getSessionFactory().getCurrentSession();
		for (VehicleColor color : vehicleInfo.getVehicleColors()){
			color.setVehicleInfo(vehicleInfo);
		}
		
		for (VehicleTexture textures : vehicleInfo.getVehicleTextures()){
			textures.setVehicleInfo(vehicleInfo);
		}
		session.update(vehicleInfo);
	}

	@Override
	public void deleteVehicleInfo(int id) {
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = " delete from VehicleInfo t where t.id = ? ";
		Query query = session.createQuery(hql);
        query.setParameter(0, id); 
		query.executeUpdate();
	}

	@Override
	public List<VehicleInfo> getVehicleInfoByPage(int offset, int limit) {
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = " from VehicleInfo";
		final Query query = session.createQuery(hql);  
		query.setFirstResult(offset);    
	    query.setMaxResults(limit); 
        @SuppressWarnings("unchecked")
		final List<VehicleInfo> list = query.list(); 
		return list;
	}

	@Override
	public int getVehicleInfoCount() {
		Session session = this.getSessionFactory().getCurrentSession();
		final String hql = " select count(v) from VehicleInfo v"; 
        final Query query = session.createQuery(hql); 
        long count = (Long)query.uniqueResult();
        return (int)count;
	}

	@Override
	public Optional<VehicleInfo> getDataByVehicleInfoId(int id) {
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = " from VehicleInfo where Id = ?";
		Query query = session.createQuery(hql);
        query.setParameter(0, id); 
        @SuppressWarnings("unchecked")
		List<VehicleInfo> list = query.list();
        if (list.size() > 0){
        	Optional<VehicleInfo> vehicleInfo = Optional.ofNullable(list.get(0));
     		return vehicleInfo;
        }
        else{
        	return Optional.empty();
        }
	}

	@Override
	public void deleteVehicleColor(int vehicleId) {
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = " delete from VehicleColor t where t.vehicleInfo.id = ? ";
		Query query = session.createQuery(hql);
        query.setParameter(0, vehicleId); 
		query.executeUpdate();
	}

	@Override
	public void deleteVehicleTexture(int vehicleId) {
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = " delete from VehicleTexture t where t.vehicleInfo.id = ? ";
		Query query = session.createQuery(hql);
        query.setParameter(0, vehicleId); 
		query.executeUpdate();
	}

	@Override
	public List<VehicleInfo> getDataByBrandId(int id) {
		Session session = this.getSessionFactory().getCurrentSession();
		
        String hql  = "select v.id,v.categoryName,v.imageUrl1,v.imageUrl2,v.productCategory,v.componentInfo,c.name from VehicleInfo v, Category c where v.brandId = ? and v.categoryId = c.id ";
        Query query = session.createQuery(hql);
        query.setParameter(0, id); 
		List list = query.list();
		
		List<VehicleInfo> vList= new ArrayList<VehicleInfo>(10);
		VehicleInfo vehicleInfo = null;
        for(int i=0; i<list.size(); i++)
        {
        	vehicleInfo = new VehicleInfo();
            Object []o = (Object[])list.get(i);
            int vid = ((Number)o[0]).intValue();
            String categoryName = (String)o[1];
            String imageUrl1 = (String)o[2];
            String imageUrl2 = (String)o[3];
            String productCategory = (String)o[4];
            String componentInfo = (String)o[5];
            String name = (String)o[6];
            
            vehicleInfo.setId(vid);
            vehicleInfo.setCategoryName(categoryName);
            vehicleInfo.setImageUrl1(imageUrl1);
            vehicleInfo.setImageUrl2(imageUrl2);
            vehicleInfo.setProductCategory(productCategory);
            vehicleInfo.setComponentInfo(componentInfo);
            vehicleInfo.setCategoryName(name);
            vList.add(vehicleInfo);
        }
        return vList;
	}

	@Override
	public List<VehicleInfo> getDataByCategoryId(int id) {
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = " select new VehicleInfo(id,imageUrl1,imageUrl2,productCategory,componentInfo) from VehicleInfo where categoryId = ?";
		Query query = session.createQuery(hql);
        query.setParameter(0, id); 
        @SuppressWarnings("unchecked")
		List<VehicleInfo> list = query.list();
        return list;
	}

}

@@ -4202,23 +4202,8 @@
 d,v.
-categoryName,v.
 imag
@@ -4741,56 +4741,8 @@
 ();%0A
-            String categoryName = (String)o%5B1%5D;%0A
     
@@ -4778,17 +4778,17 @@
 tring)o%5B
-2
+1
 %5D;%0A     
@@ -4827,9 +4827,9 @@
 g)o%5B
-3
+2
 %5D;%0A 
@@ -4878,9 +4878,9 @@
 g)o%5B
-4
+3
 %5D;%0A 
@@ -4927,9 +4927,9 @@
 g)o%5B
-5
+4
 %5D;%0A 
@@ -4967,9 +4967,9 @@
 g)o%5B
-6
+5
 %5D;%0A 
@@ -5020,63 +5020,8 @@
 d);%0A
-            vehicleInfo.setCategoryName(categoryName);%0A
     

fix bugs