package com.viettel.campaign.repository.impl;

import com.viettel.campaign.model.CampaignCompleteCode;
import com.viettel.campaign.repository.CampaignCompleteCodeRepository;
import com.viettel.campaign.utils.HibernateUtil;
import com.viettel.campaign.utils.SQLBuilder;
import com.viettel.campaign.web.dto.CampaignCompleteCodeDTO;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.LongType;
import org.hibernate.type.ShortType;
import org.hibernate.type.StringType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class CampaignCompleteCodeRepositoryIpml implements CampaignCompleteCodeRepository {
    @Autowired
    EntityManager entityManager;
    CampaignCompleteCodeRepository campaignCompleteCodeRepository;

    private Logger log = LoggerFactory.getLogger(CampaignCompleteCodeRepositoryIpml.class);

    @Override
    public Page<CampaignCompleteCode> findAll(Pageable pageable) {
        return campaignCompleteCodeRepository.findAll(pageable);
    }

    @Override
    public List<CampaignCompleteCode> findByName(String name, Pageable pageable) {
//        return campaignCompleteCodeRepository.findByName(name , pageable);
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        List<CampaignCompleteCode> resultList = null;

        try {
            StringBuilder sql = new StringBuilder(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_STATUS_MNG, "search-campaign-status-by-params"));
            SQLQuery query = session.createSQLQuery(sql.toString());

            query.setParameter(":p_name", name);

            // add SCalar
            query.addScalar("campaignId", new LongType());
            query.addScalar("completeValue", new StringType());
            query.addScalar("completeName", new StringType());
            query.addScalar("completeType", new StringType());
            query.addScalar("chanel", new ShortType());
            query.addScalar("description", new StringType());
            query.addScalar("status", new ShortType());


            query.setResultTransformer(Transformers.aliasToBean(CampaignCompleteCodeDTO.class));

            resultList = query.list();
        } catch (Exception e) {
            session.close();
            log.error(e.getMessage(), e);
        }

        return resultList;
    }

    @Override
    public List<CampaignCompleteCode> findByCompleteNameContains(String Name, Pageable pageable) {
//        List<CampaignCompleteCodeDTO> lst = new ArrayList<>();
//        return lst;
        return null;
    }

    @Override
    public List<CampaignCompleteCode> findAll() {
        return campaignCompleteCodeRepository.findAll();
    }

    @Override
    public List<CampaignCompleteCode> findAll(Sort sort) {
        return campaignCompleteCodeRepository.findAll();
    }

    @Override
    public List<CampaignCompleteCode> findAllById(Iterable<Long> iterable) {
        return null;
    }

    @Override
    public long count() {
        return 0;
    }

    @Override
    public void deleteById(Long aLong) {

    }

    @Override
    public void delete(CampaignCompleteCode campaignCompleteCode) {

    }

    @Override
    public void deleteAll(Iterable<? extends CampaignCompleteCode> iterable) {

    }

    @Override
    public void deleteAll() {

    }

    @Override
    public <S extends CampaignCompleteCode> S save(S s) {
        return null;
    }

    @Override
    public <S extends CampaignCompleteCode> List<S> saveAll(Iterable<S> iterable) {
        return null;
    }

    @Override
    public Optional<CampaignCompleteCode> findById(Long aLong) {
        return Optional.empty();
    }

    @Override
    public boolean existsById(Long aLong) {
        return false;
    }

    @Override
    public void flush() {

    }

    @Override
    public <S extends CampaignCompleteCode> S saveAndFlush(S s) {
        return null;
    }

    @Override
    public void deleteInBatch(Iterable<CampaignCompleteCode> iterable) {

    }

    @Override
    public void deleteAllInBatch() {

    }

    @Override
    public CampaignCompleteCode getOne(Long aLong) {
        return null;
    }

    @Override
    public <S extends CampaignCompleteCode> Optional<S> findOne(Example<S> example) {
        return Optional.empty();
    }

    @Override
    public <S extends CampaignCompleteCode> List<S> findAll(Example<S> example) {
        return null;
    }

    @Override
    public <S extends CampaignCompleteCode> List<S> findAll(Example<S> example, Sort sort) {
        return null;
    }

    @Override
    public <S extends CampaignCompleteCode> Page<S> findAll(Example<S> example, Pageable pageable) {
        return null;
    }

    @Override
    public <S extends CampaignCompleteCode> long count(Example<S> example) {
        return 0;
    }

    @Override
    public <S extends CampaignCompleteCode> boolean exists(Example<S> example) {
        return false;
    }
}
