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 com.viettel.campaign.web.dto.ResultDTO;
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 org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
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) {
//        Page<CampaignCompleteCode>
//        try {
//
//        } catch (Exception e) {
//
//        }
        return null;
    }

    @Override
    public List<CampaignCompleteCode> findByName(String name, Pageable 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.addScalar("chanel", new LongType());

            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) {
        return campaignCompleteCodeRepository.findByCompleteNameContains(name, pageable);
    }

    @Override
    public int deleteds(List<Long> ids) {
        return 0;
    }

    @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
    @Transactional
    public void deleteById(Long id) {
        ResultDTO result = new ResultDTO();
        try {
            // update Status = 0 cho cái ID truyền vào
            CampaignCompleteCode entity = entityManager.find(CampaignCompleteCode.class, id);
            // set giá trị Status = 0 --> tương đương với inactive
            entity.setStatus(new Short("0"));
            entityManager.merge(entity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @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;
    }
}
