<?php
class DPTHelper
{
    /** @var  HijriDate */
    private $hijriDate;

    function __construct()
    {
        $this->hijriDate = new HijriDate();
    }

    function todayIsFriday()
    {
        return date_i18n('D') == 'Fri';
    }

    function tomorrowIsFriday()
    {
        return date_i18n('D') == 'Thu';
    }

    function isRamadan()
    {
        return $this->hijriDate->isRamadan();
    }

    function getIqamahClass($number)
    {
        if ( $number > 30 ) {
            return 'green';
        } elseif ( $number > 15 ) {
            return 'orange';
        }

        return 'red';
    }

    /**
     * dim display overnight between Isha and Fajr start
     */
    function canDimOvernight($dbRow, $disableOvernightDim=false)
    {
        $minsAfterIsha = 30;
        if($this->isRamadan()) {
            $minsAfterIsha += (int)get_option('taraweehDim');
        }
        if ($disableOvernightDim) {
            return 0;
        }

        $userTime = user_current_time( 'H:i');
        $now = new DateTime();
        $now->setTimestamp(strtotime($userTime));

        $isha = new DateTime();
        $isha->setTimestamp(strtotime($dbRow['isha_jamah']));
        $isha->modify('+'.$minsAfterIsha. ' mins');

        $fajr = new DateTime();
        $fajr->setTimestamp(strtotime($dbRow['fajr_begins']));

        if ($now < $fajr || $now > $isha) {
            return 1;
        }

        return 0;
    }

    /**
     * set khutbah dimming time on friday between sunrise and Asr 
     */
    function getKhutbahDim(array $dbRow): int
    {
        if (! $this->todayIsFriday()) {
            return 0;
        }

        $userTime = user_current_time( 'H:i');
        $now = new DateTime();
        $now->setTimestamp(strtotime($userTime));

        $sunrise = new DateTime();
        $sunrise->setTimestamp(strtotime($dbRow['sunrise']));

        $asr = new DateTime();
        $asr->setTimestamp(strtotime($dbRow['asr_begins']));

        if ($now > $sunrise && $now < $asr) {
            return (int)get_option('khutbahDim');

        }

        return 0;
    }

    /**
     * get diming time for taraweeh during ramadan after isha and before fajr
     */
    function getTaraweehDim(array $dbRow): int
    {
        if (! $this->isRamadan()) {
            return 0;
        }

        $userTime = user_current_time( 'H:i');
        $now = new DateTime();
        $now->setTimestamp(strtotime($userTime));

        $maghrib = new DateTime();
        $maghrib->setTimestamp(strtotime($dbRow['maghrib_jamah']));

        if ($now > $maghrib) {
            return (int)get_option('taraweehDim');

        }

        return 0;
    }

    /**
     * @param  string $mysqlDate
     * @param  string $format
     * @return string
     */
    function formatDate($mysqlDate, $format=null)
    {
        $phpDate = strtotime($mysqlDate);

        $date =  date( get_option('date_format'), $phpDate );
        if ($format) {
            $date = date($format, $phpDate);
        }

        return $date;
    }

    public function getHijriDate($day, $month, $year, $dbRow, $forMonth=false)
    {
        $hijriCheckbox = get_option('hijri-chbox');
        if ( ! empty($hijriCheckbox) ) {
            if( ! empty($dbRow['hijri_date']) ) {
                $hijriDate = $dbRow['hijri_date'];
            } else {
                $hijridateArray = $this->hijriDate->getDate($day, $month, $year, false, $this->isSunset($dbRow, $forMonth));
                $hijriDate = $hijridateArray['day']. ' '. $hijridateArray['month'] . ' ' . $hijridateArray['year'];
            }

            return '<p class="hijriDate"> '. $hijriDate .'</p>';
        }
        return '';
    }

    public function isJumahDisplay($dbRow)
    {
        if ( date_i18n('D') == 'Fri' &&
            current_time('timestamp') > strtotime($dbRow['sunrise']) &&
            current_time('timestamp') <= strtotime($dbRow['zuhr_jamah']) + 60
        ) {
            return true;
        }

        return false;
    }

    public function isSunset($dbRow, $forMonth=false)
    {
        return  !$forMonth && current_time('timestamp') > strtotime($dbRow['maghrib_begins']);
    }

    public function getIshraqTime($sunrise)
    {
        $ishraqMins = get_option('ishraq');
        if (!$ishraqMins || $ishraqMins == '0') {
            return null;
        }
        return date("H:i", strtotime($sunrise . " +" . intval($ishraqMins) . " minutes"));
    }

    public function isIshraqTimeNext($row)
    {
        $ishraqMins = get_option('ishraq');
        if (!$ishraqMins || $ishraqMins == '0') {
            return false;
        }
        
        $fajrTs = strtotime($row['fajr_begins']);
        $sunrise = $row['sunrise'];
        $ishraqTime = $this->getIshraqTime($sunrise);
        $now = user_current_time('H:i');
        
        $nowTs = strtotime($now);
        $ishraqTs = strtotime($ishraqTime);
        
        return $nowTs >= $fajrTs && $nowTs < $ishraqTs;
    }

    public function getNextPrayerClass($prayerName, $row, $isFajr=false)
    {
        $nextPrayerName = $this->getNextPrayer($row);
        
        if ($this->todayIsFriday() && $nextPrayerName == 'zuhr') {
            $nextPrayerName = 'jumuah';
        }

        // If ishraq is next, highlight sunrise row (which shows ishraq)
        if ($nextPrayerName == 'ishraq' && ($prayerName == 'sunrise' || $prayerName == 'ishraq')) {
            return 'nextPrayer';
        }

        // If zawal time is next, highlight zuhr row (not sunrise row)
        if ($this->isZawalTimeNext($row) && $prayerName == 'zuhr') {
            return 'nextPrayer';
        }
        
        // If next prayer is zuhr or later, don't highlight sunrise/ishraq rows
        if (in_array($prayerName, ['sunrise', 'ishraq']) && in_array($nextPrayerName, ['zuhr', 'asr', 'maghrib', 'isha', 'jumuah'])) {
            return '';
        }

        if ($isFajr && is_null($nextPrayerName)) {
            return 'nextPrayer';
        }
        if ($nextPrayerName && (strpos($nextPrayerName, $prayerName) !== false)) {
            return 'nextPrayer';
        }

        return '';
    }

    	/**
     * @param $row
     *
     * @return string
     */
    public function getNextPrayer($row)
    {
        $ishraqMins = get_option('ishraq');
        $now = current_time('H:i');
        
        if ($ishraqMins && $ishraqMins != '0') {
            $sunrise = $row['sunrise'];
            $ishraqTime = $this->getIshraqTime($sunrise);
            $zuhrTs = strtotime($row['zuhr_begins']);
            
            $nowTs = strtotime($now);
            $ishraqTs = strtotime($ishraqTime);
            
            // After ishraq starts but before zuhr, next prayer is zuhr
            if ($nowTs >= $ishraqTs && $nowTs < $zuhrTs) {
                return 'zuhr';
            }
        }

        if (get_option('zawal') && $this->isZawalTimeNext($row)) {
            return 'zuhr';
        }

        $jamahTime = $this->getJamahTime( $row );
        $nowTs = strtotime($now);
        foreach ($jamahTime as $key => $jamah) {
            $jamahTs = strtotime($jamah);
            if ($jamahTs > $nowTs) {
                // If next would be sunrise and ishraq is set, show ishraq instead
                if ($key === 'sunrise' && $ishraqMins && $ishraqMins != '0') {
                    return 'ishraq';
                }
                $prayer = array_search( $jamah, $row ); 
                $prayer = explode( '_', $prayer);
                return $prayer[0]; 
            }
        }
    }

    public function getSunriseOrZawalOrIshraq($row)
    {
        $nextPrayer = $this->getNextPrayer($row);
        $ishraqMins = get_option('ishraq');

        // If ishraq is next, show ishraq
        if ($ishraqMins && $ishraqMins != '0' && $nextPrayer == 'ishraq') {
            return 'ishraq';
        }

        // If zawal is enabled and zuhr is next, show zawal
        if (get_option('zawal') && $nextPrayer == 'zuhr') {
            return 'zawal';
        }

        // Otherwise show sunrise
        return 'sunrise';
    }

    public function getJamahTime(array $row)
    {
        $row = $this->updateZuhrWithJummahTimes($row);

        $value = array( $row["fajr_jamah"], $row['sunrise'], $row["zuhr_jamah"], $row["asr_jamah"], $row["maghrib_jamah"], $row["isha_jamah"]);

        $prayerName = ["fajr", "sunrise", "zuhr", "asr", "maghrib", "isha"];

        return array_combine( $prayerName, $value );

    }

    public function getZawalTime($zuhrBegins)
    {
        $zawalMinutes = get_option('zawal');   

        return date( "H:i", strtotime( $zuhrBegins. "-$zawalMinutes minute") );
    }

    /**
     * if today is friday
     *  and now is before zuhr then set zuhr to jummah 1
     *  if now is > jummah1 and now is < jummah 2, then set zuhr to jummah2
     *  if now is > jummah2 and now is < asr, then set zuhr to jummah3
     * 
     * if tomorrow is friday, then set tomorrow zuhr to jummah1
     */
    public function updateZuhrWithJummahTimes(array $row)
    {
        $jumuah1 = get_option('jumuah1');
        $jumuah2 = get_option('jumuah2');
        $jumuah3 = get_option('jumuah3');

         if($this->todayIsFriday()) {
            $userTime = user_current_time( 'H:i');
            $now = new DateTime();
            $now->setTimestamp(strtotime($userTime));
    
            $zuhr = new DateTime();
            $zuhr->setTimestamp(strtotime($row['zuhr_begins']));

            $asr = new DateTime();
            $asr->setTimestamp(strtotime($row['asr_jamah']));
            
            $jumuah1dt = new DateTime();
            $jumuah1dt->setTimestamp(strtotime($jumuah1));

            $jumuah2dt = new DateTime();
            $jumuah2dt->setTimestamp(strtotime($jumuah2));
            
            $jumuah3dt = new DateTime();
            $jumuah3dt->setTimestamp(strtotime($jumuah3));

            if (!empty($jumuah1) && $now < $jumuah1dt) {
                $row['zuhr_jamah'] = $jumuah1dt->format('H:i:s');
            } else if (!empty($jumuah2) && ($now > $jumuah1dt && $now < $jumuah2dt)) {
                $row['zuhr_jamah'] = $jumuah2dt->format('H:i:s');
            } else if (!empty($jumuah3) && ($now > $jumuah2dt && $now < $asr)) {
                $row['zuhr_jamah'] = $jumuah3dt->format('H:i:s');
            } else if (!empty($jumuah3) && ($now > $jumuah3dt)) {
                $row['zuhr_jamah'] = $row['tomorrow']['zuhr_jamah'];
            }
         }

        return $row;
    }

    public function isZawalTimeNext($row)
    {
        $userTime = user_current_time( 'H:i');
        $now = new DateTime();
        $now->setTimestamp(strtotime($userTime));

        $sunrise = new DateTime();
        $sunrise->setTimestamp(strtotime($row['sunrise']));

        $zuhr = new DateTime();
        $zuhr->setTimestamp(strtotime($row['zuhr_begins']));

        if ($now > $sunrise && $now < $zuhr) { 
            return true;
        }
        return false;
    }
}