/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.triz.util;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author Admin
 */
public class Outils {

    //region Hashage code ..
    private static String key = "trizmobiletriz";

    public static String decrypt(String encrypted) throws Exception {
        byte[] keyb = key.getBytes("UTF-8");
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(keyb);
        SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");
        Cipher dcipher = Cipher.getInstance("AES");
        dcipher.init(Cipher.DECRYPT_MODE, skey);

        byte[] clearbyte = dcipher.doFinal(Base64.getDecoder().decode(encrypted));
        return new String(clearbyte);
    }

    public static String encrypt(String encrypted) throws Exception {
        byte[] keyb = key.getBytes("UTF-8");
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(keyb);
        SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");
        Cipher dcipher = Cipher.getInstance("AES");
        dcipher.init(Cipher.ENCRYPT_MODE, skey);

        byte[] clearbyte = dcipher.doFinal(encrypted.getBytes("utf-8"));
        return Base64.getEncoder().encodeToString(clearbyte);
    }
    //endregion

    public static Double dateDifferenceWithToday(String date) {
        Double diff = 100.0;

        try {
            Date dateClient = new SimpleDateFormat("yyyy-MM-dd kk:mm").parse(date);
            Date dateServer = new SimpleDateFormat("yyyy-MM-dd kk:mm").parse(getCurrentDate() + " " + getCurrentTime());
            System.out.println("diff=== " + (dateServer.getTime() - dateClient.getTime()) + " now=" + getCurrentDate() + " " + getCurrentTime() + " / date= " + date);
            diff = (double) ((dateServer.getTime() - dateClient.getTime()) / (1000 * 60));
            System.out.println("diff=== " + diff + " min");
        } catch (ParseException ex) {
            Logger.getLogger(Outils.class.getName()).log(Level.SEVERE, null, ex);
        }

        return diff;
    }

    public static String getCurrentDate() {
        GregorianCalendar localGregorianCalendar = new GregorianCalendar();
        return new SimpleDateFormat("yyyy-MM-dd").format(localGregorianCalendar.getTime());

    }

    public static String getCurrentTime() {
        GregorianCalendar localGregorianCalendar = new GregorianCalendar();
        return new SimpleDateFormat("HH:mm:ss").format(localGregorianCalendar.getTime());
    }

    public static Date stringToDate(String date) {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDate.parse(date);
        } catch (ParseException ex) {
            return null;
        }
    }

    public static String dateToString(Date date) {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");

        return date == null ? "0000-00-00" : simpleDate.format(date);

    }

    public static String formatHeur(Date date) {
        if (date == null) {
            return "-";
        }
        SimpleDateFormat simpleDate = new SimpleDateFormat("HH:mm");
        return simpleDate.format(date);
    }
    
    public static String  replaceNULL(Expression ex,String replacement){
        try{
            return ex.compile();
        }catch(Exception e){
            return replacement;
        } 
    }
    
    public interface Expression{
        String compile();
    }
}
