package com.roshka.service;

import com.roshka.modelo.Beneficio;
import com.roshka.modelo.Birthday;
import com.roshka.modelo.Feriado;
import com.roshka.repositorio.BeneficioRepository;
import com.roshka.repositorio.BirthdayRepository;
import com.roshka.repositorio.FeriadoRepository;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Configuration
@EnableScheduling
public class Scheduler {
    @Autowired
    BirthdayRepository birthdayRepository;
    @Autowired
    FeriadoRepository feriadoRepository;
    @Autowired
    BeneficioRepository beneficioRepository;
    @PersistenceContext
    private EntityManager entityManager;

    // @Scheduled(cron = "0 0 8 * * MON-FRI")
   @Scheduled(cron = "0 * * * * *")
    public void cumples() {
        String url = "https://hooks.slack.com/services/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";
        // verificar que no sea feriado
        Feriado esFeriado = feriadoRepository.findByFecha(new Date());
        if (esFeriado != null){
            return;
        }

        int contador = 1;
        Date hoy = new Date();
        Date fechaAyer = new Date(hoy.getTime() - ((1000 * 60 * 60 * 24) * contador) );
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat f = new  SimpleDateFormat("EEEE");
        String diaAyer = f.format(fechaAyer);

        Boolean diaNoHabil = false;

        if ((diaAyer.equals("sábado")) || (diaAyer.equals("domingo"))){
            diaNoHabil = true;
        }

        Feriado fechaNoHabil = feriadoRepository.findByFecha(fechaAyer);
        if (fechaNoHabil != null) {
            diaNoHabil = true;
        }

        List<Birthday> cumplesDiaNoHabiles = null;
        while(diaNoHabil) {
            List<Birthday> cumplesAyer = birthdayRepository.findAllByFecha(fechaAyer);

            if (cumplesDiaNoHabiles == null) {
                cumplesDiaNoHabiles = new ArrayList<>(cumplesAyer);
            } else {
               cumplesDiaNoHabiles.addAll(cumplesAyer);
            }
            contador++;
            diaNoHabil = false;
            hoy = new Date();
            fechaAyer = new Date(hoy.getTime() - ((1000 * 60 * 60 * 24) * contador) );

            f = new  SimpleDateFormat("EEEE");
            diaAyer = f.format(fechaAyer);
            if ((diaAyer.equals("sábado")) || (diaAyer.equals("domingo"))){
                diaNoHabil = true;
            }

            fechaNoHabil = feriadoRepository.findByFecha(fechaAyer);
            if (fechaNoHabil != null) {
                diaNoHabil = true;
            }
        }
       for (Birthday cumple : cumplesDiaNoHabiles){
           String json = "{\n" +
                   "\"blocks\": [\n" +
                   "{\n" +
                   "\"type\": \"section\",\n" +
                   "\"text\": {\n" +
                   "\"type\": \"mrkdwn\",\n" +
                   "\"text\": \"Buenos días <!channel> \n En la fecha " + cumple.getFecha() + " estuvo de cumpleaños *" + cumple.getNombreCompleto() + "* <@" + cumple.getIdSlack() + ">. 🥳 ¡Que los cumplas muy feliz te desea Roshka! Que en la vida siempre encuentres razones para sonreír 🥳\",\n" +
                   "}" +
                   "}," +
                   "{" +
                   "\"type\": \"divider\"" +
                   "}," +
                   "{\n" +
                   "\"type\": \"image\",\n" +
                   "\"title\": {\n" +
                   "\"type\": \"plain_text\",\n" +
                   "\"text\": \"" + cumple.getNombreCompleto() + "\"\n" +
                   "},\n" +
                   "\"block_id\": \"image4\",\n" +
                   "\"image_url\": \"http://placekitten.com/500/500\",\n" +
                   "\"alt_text\": \"An incredibly cute kitten.\"\n" +
                   "}\n" +
                   "]\n" +
                   "}" +
                   "";
           String result = null;
           HttpPost httpPost = new HttpPost(url);
           httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));

           try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
               try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                   result = EntityUtils.toString(response.getEntity());
               }

           } catch (IOException | ParseException e) {
               e.printStackTrace();
           }

           System.out.println("FIN");

        }
        // Verificar el cumpleaños de quien es
        List<Birthday> cumples = birthdayRepository.findAllByFecha(new Date());
        System.out.println("PRUEBA");
        for (Birthday cumple : cumples) {
            String json = "{\n" +
                    "\"blocks\": [\n" +
                    "{\n" +
                    "\"type\": \"section\",\n" +
                    "\"text\": {\n" +
                    "\"type\": \"mrkdwn\",\n" +
                    "\"text\": \"Buenos días <!channel> \n Hoy está de cumpleaños *" + cumple.getNombreCompleto() + "* <@" + cumple.getIdSlack() + ">. 🥳 ¡Que los cumplas muy feliz te desea Roshka! Que en la vida siempre encuentres razones para sonreír 🥳\",\n" +
                    "}" +
                    "}," +
                    "{" +
                    "\"type\": \"divider\"" +
                    "}," +
                    "{\n" +
                    "\"type\": \"image\",\n" +
                    "\"title\": {\n" +
                    "\"type\": \"plain_text\",\n" +
                    "\"text\": \"" + cumple.getNombreCompleto() + "\"\n" +
                    "},\n" +
                    "\"block_id\": \"image4\",\n" +
                    "\"image_url\": \"http://placekitten.com/500/500\",\n" +
                    "\"alt_text\": \"An incredibly cute kitten.\"\n" +
                    "}\n" +
                    "]\n" +
                    "}" +
                    "";
            String result = null;
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));

            try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
                try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                    result = EntityUtils.toString(response.getEntity());
                }

            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }

            System.out.println("FIN");
        }
    }




    @Scheduled(cron = "0 0 8 * * MON-FRI")
//    @Scheduled(cron = "0 * * * * *")
    public void feriados(){

    }


        @Scheduled(cron = "0 0 1 * * FRI")
//    @Scheduled(cron = "0 * * * * *")
    public void beneficios() {
        String url = "https://hooks.slack.com/services/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";
        // Traer random
        Beneficio beneficio = getRandomBeneficio();
        if (beneficio != null) {
            String json = "{" +
                    "\"blocks\": [" +
                    "{" +
                    "\"type\": \"section\"," +
                    "\"text\": {" +
                    "\"type\": \"mrkdwn\"," +
                    "\"text\": \"Te recordamos uno de nuestros beneficios <!channel>\"" +
                    "}" +
                    "}," +
                    "{" +
                    "\"type\": \"header\"," +
                    "\"text\": {" +
                    "\"type\": \"plain_text\"," +
                    "\"text\": \"" + beneficio.getTitulo() + "\"," +
                    "\"emoji\": true" +
                    "}" +
                    "}," +
                    "{" +
                    "\"type\": \"section\"," +
                    "\"text\": {" +
                    "\"type\": \"mrkdwn\"," +
                    "\"text\": \"" + beneficio.getDescripcion() + "\"" +
                    "}" +
                    "}" +
                    "]" +
                    "}";
            String result = null;
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));

            try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
                try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
                    result = EntityUtils.toString(response.getEntity());
                }
            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }
        }
    }

    public Beneficio  getRandomBeneficio() {
        String query = "SELECT o.id FROM Beneficio o ORDER BY random()";
        Query q = entityManager.createQuery(query);
        q.setMaxResults(1);
        Optional<Beneficio> beneficio = beneficioRepository.findById((Long) q.getSingleResult());
        if (beneficio.isPresent()) {
            return beneficio.get();
        }
        return null;
    }

}
