<?php

namespace frontend\extensions;

use Yii;

class Utils {

    public static function getFestivi($anno = "") {
        //PRENDO IN INPUT L'ANNO DA ELABORARE. 
        //SE VUOTO PRENDO L'ANNO IN CORSO
        $anno_input = $anno != '' ? $anno : date("Y");

        //INIZIALIZZIAMO LE FESTIVITA' CON LA LORO ETICHETTA.
        $feste = Array(
            "01-01" => "Capodanno",
            "06-01" => "Epifania",
            "25-04" => "Liberazione",
            "01-05" => "Festa Lavoratori",
            "02-06" => "Festa della Repubblica",
            "15-08" => "Ferragosto",
            "01-11" => "Tutti Santi",
            "08-12" => "Immacolata",
            "25-12" => "Natale",
            "26-12" => "St. Stefano"
        );

        $giorni = array('Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato');

        //ARRAY FINALE CON LE DATE DEI FESTIVI
        $festivita = array();

        $start = strtotime("$anno_input-01-01");
        $stop = strtotime("$anno_input-12-31");

        $day = $start;
        while ($day <= $stop) {
            list($ita_date, $confronto_festivo, $anno, $giorno_settimana) = explode('__', date('d-m-Y__d-m__Y__w', $day));
            //SE GIORNO FESTIVO LO INSERISCO NELLA NOSTRA LISTA.

            if (self::inFestivita($confronto_festivo, $feste)) {
                $festivita[] = array(
                    "day" => date("d", $day),
                    "month" => date("m", $day),
                );
                $etichette[$giorni[$giorno_settimana] . ": " . $ita_date] = $feste[$confronto_festivo];
            } else {
                //CONTROLLO CHE NON SIA PASQUETTA.
                $march21 = date("$anno-03-21");
                $gPasquetta = easter_days($anno) + 1;
                $dataPasquetta = date("d-m-Y", strtotime(date("Y-m-d", strtotime($march21)) . " +$gPasquetta day"));
                if ($ita_date == $dataPasquetta) {
                    $festivita[] = array(
                        "day" => date("d", $day),
                        "month" => date("m", $day),
                    );
                    $etichette[$giorni[1] . ": " . $ita_date] = "Pasquetta";
                }
            }
            //VADO AVANTI DI UN GIORNO
            $day+=86400;
        }
        return $festivita;
    }

    //FUNZIONE PER LA RICERCA DI UNA DATA NELLA LISTA DELLE FESTIVITA'
    private static function inFestivita($data, $dateFestivi) {
        foreach ($dateFestivi as $d => $v) {
            if ($d == $data)
                return true;
        }
        return false;
    }

    public static function isFestivo($festivi, $day, $month, $year = "") {
        foreach ($festivi as $festivo) {
            if ((int) $festivo['day'] == (int) $day && (int) $festivo['month'] == (int) $month) {
                return true;
            }
        }

        return false;
    }

    public static function isLuckyNumber() {
        $luckyNumbers = array(
            '3776813158',
            '3333525401',
            '3384024373'
        );

        if (Yii::$app->user->isGuest)
            return false;

        return in_array(Yii::$app->user->id, $luckyNumbers);
    }

    public static function startsWith($haystack, $needle) {
        return $needle === "" || strpos($haystack, $needle) === 0;
    }

    public static function endsWith($haystack, $needle) {
        return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
    }

}
