<?php

use Carbon\Carbon;

class DateHelper
{
    public static function formatToday()
    {
        $date = Carbon::now();

        $month = $date->month;
        $monthLabel = '';

        if ($month == 1) {
            $monthLabel = 'Januari';
        } else if ($month == 2) {
            $monthLabel = 'Februari';
        } else if ($month == 3) {
            $monthLabel = 'Maret';
        } else if ($month == 4) {
            $monthLabel = 'April';
        } else if ($month == 5) {
            $monthLabel = 'Mei';
        } else if ($month == 6) {
            $monthLabel = 'Juni';
        } else if ($month == 7) {
            $monthLabel = 'Juli';
        } else if ($month == 8) {
            $monthLabel = 'Agustus';
        } else if ($month == 9) {
            $monthLabel = 'September';
        } else if ($month == 10) {
            $monthLabel = 'Oktober';
        } else if ($month == 11) {
            $monthLabel = 'November';
        } else if ($month == 12) {
            $monthLabel = 'Desember';
        }

        return $date->day . ' ' . $monthLabel . ' ' . $date->year;
    }

    public static function formatTodayIndex()
    {
        $date = Carbon::now('+07:00');

        return $date->format('ymdHi');
    }

    public static function formatFromDate($year, $month, $day = null, $language = 'ID') {
        $date = Carbon::createFromDate($year, $month, $day);

        $month = $date->month;
        $monthLabel = '';

        if ($month == 1) {
            $monthLabel = ($language == 'ID' ? 'Januari' : 'January');
        } else if ($month == 2) {
            $monthLabel = ($language == 'ID' ? 'Februari' : 'February');
        } else if ($month == 3) {
            $monthLabel = ($language == 'ID' ? 'Maret' : 'March');
        } else if ($month == 4) {
            $monthLabel = 'April';
        } else if ($month == 5) {
            $monthLabel = ($language == 'ID' ? 'Mei' : 'May');
        } else if ($month == 6) {
            $monthLabel = ($language == 'ID' ? 'Juni' : 'June');
        } else if ($month == 7) {
            $monthLabel = ($language == 'ID' ? 'Juli' : 'July');
        } else if ($month == 8) {
            $monthLabel = ($language == 'ID' ? 'Agustus' : 'August');
        } else if ($month == 9) {
            $monthLabel = 'September';
        } else if ($month == 10) {
            $monthLabel = ($language == 'ID' ? 'Oktober' : 'October');
        } else if ($month == 11) {
            $monthLabel = 'November';
        } else if ($month == 12) {
            $monthLabel = ($language == 'ID' ? 'Desember' : 'December');
        }

        if ($day) {
            return $date->day . ' ' . $monthLabel . ' ' . $date->year;
        } else {
            return $monthLabel . ' ' . $date->year;
        }
    }

    public static function listAvailableMonth($year = '')
    {
        $result = [];
        $date = Carbon::now();
        if(empty($year)) {
            $year = $date->year;
        }

        if($year < $date->year) {
            $result = [
                'Januari',
                'Februari',
                'Maret',
                'April',
                'Mei',
                'Juni',
                'Juli',
                'Agustus',
                'September',
                'Oktober',
                'November',
                'Desember'
            ];
        }
        else if($year == $date->year) {
            if($date->month >= 1) {
                $result[] = 'Januari';
            }
            if($date->month >= 2) {
                $result[] = 'Februari';
            }
            if($date->month >= 3) {
                $result[] = 'Maret';
            }
            if($date->month >= 4) {
                $result[] = 'April';
            }
            if($date->month >= 5) {
                $result[] = 'Mei';
            }
            if($date->month >= 6) {
                $result[] = 'Juni';
            }
            if($date->month >= 7) {
                $result[] = 'Juli';
            }
            if($date->month >= 8) {
                $result[] = 'Agustus';
            }
            if($date->month >= 9) {
                $result[] = 'September';
            }
            if($date->month >= 10) {
                $result[] = 'Oktober';
            }
            if($date->month >= 11) {
                $result[] = 'November';
            }
            if($date->month >= 12) {
                $result[] = 'Desember';
            }

        }

        return $result;
    }

    public static function convertMonthStringtoDigit($monthString)
    {
        $tempString = strtolower($monthString);

        if($tempString == 'januari') {
            return 1;
        } else if($tempString == 'februari') {
            return 2;
        } else if($tempString == 'maret') {
            return 3;
        } else if($tempString == 'april') {
            return 4;
        } else if($tempString == 'mei') {
            return 5;
        } else if($tempString == 'juni') {
            return 6;
        } else if($tempString == 'juli') {
            return 7;
        } else if($tempString == 'agustus') {
            return 8;
        } else if($tempString == 'september') {
            return 9;
        } else if($tempString == 'oktober') {
            return 10;
        } else if($tempString == 'november') {
            return 11;
        } else if($tempString == 'desember') {
            return 12;
        } else {
            return 0;
        }
    }

    public static function convertMonthDigittoString($monthDigit)
    {
        $monthLabel = '';

        if ($monthDigit == 1) {
            $monthLabel = 'Januari';
        } else if ($monthDigit == 2) {
            $monthLabel = 'Februari';
        } else if ($monthDigit == 3) {
            $monthLabel = 'Maret';
        } else if ($monthDigit == 4) {
            $monthLabel = 'April';
        } else if ($monthDigit == 5) {
            $monthLabel = 'Mei';
        } else if ($monthDigit == 6) {
            $monthLabel = 'Juni';
        } else if ($monthDigit == 7) {
            $monthLabel = 'Juli';
        } else if ($monthDigit == 8) {
            $monthLabel = 'Agustus';
        } else if ($monthDigit == 9) {
            $monthLabel = 'September';
        } else if ($monthDigit == 10) {
            $monthLabel = 'Oktober';
        } else if ($monthDigit == 11) {
            $monthLabel = 'November';
        } else if ($monthDigit == 12) {
            $monthLabel = 'Desember';
        }

        return $monthLabel;
    }

    const PERIOD_TRIWULAN = 2;
    public static function monthGroupOrderConverter($period, $type)
    {
        $result = 0;
        $period = strtolower($period);
        if($type == self::PERIOD_TRIWULAN) {
            if ($period == 'triwulan i') {
                $result = 1;
            } else if ($period == 'triwulan ii') {
                $result = 2;
            } else if ($period == 'triwulan iii') {
                $result = 3;
            } else if ($period == 'triwulan iv') {
                $result = 4;
            } else {
                $result = 0;
            }
        }

        return $result;
    }

    public static function getCurrentDate()
    {
        $date = Carbon::now('+07:00');

        return $date;
    }

    public static function getNowTimezoned($tz = 'Asia/Jakarta')
    {
        try {
            $timestamp = time();
            $dt = new DateTime("now", new DateTimeZone($tz));
            $dt->setTimestamp($timestamp);
            $now = $dt->format('Y-m-d H:i:s');
        } catch (Exception $e) {
            $now = date('Y-m-d H:i:s');
        }

        return $now;
    }

    public static function getNowDateTimeTimeZoned($tz = 'Asia/Jakarta')
    {
        try {
            $dt = new DateTime("now", new DateTimeZone($tz));
        } catch (Exception $e) {
            $dt = new DateTime("now");
        }

        return $dt;
    }

    public static function formatDateTime($dateTime)
    {
        try {
            $formatted = $dateTime->format('Y-m-d H:i:s');
        } catch (Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Gagal format Date Time");
        }

        return $formatted;
    }



    public static function formatFromTimestamp(String $date, $language = 'ID') {
        try {
            $now = Carbon::createFromFormat('Y-m-d H:i:s', $date);
            $now = DateHelper::formatFromDate($now->year, $now->month, $now->day, $language);
        } catch (Exception $e) {
            $now = $date;
        }

        return $now;
    }

    public static function formatFromDateString(String $date) {
        try {
            $now = Carbon::createFromFormat('Y-m-d', $date);
            $now = DateHelper::formatFromDate($now->year, $now->month, $now->day);
        } catch (Exception $e) {
            $now = $date;
        }

        return $now;
    }

    public static function listAvailableYears(){
        $result = [];
        $currentYear = Carbon::now()->year;

        for ($year = $currentYear; $year >= 2018; $year--) {
            $result[] = $year;
        }

        return $result;
    }


    /**
     * @return bool
     */
    public static function compareAwsCheckpoint($attachmentCreatedAt) {
        try {
            $checkpoint = '2024-07-15';
            $checkpointTime = strtotime(DateTime::createFromFormat('Y-m-d', $checkpoint)->format('Y-m-d'));
            $inputTime = strtotime($attachmentCreatedAt);
            return $inputTime > $checkpointTime;
        } catch (\Exception $e) {
            return false;
        }
    }
}
