<?php

namespace App\Helpers;

use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class Helper
{
    public static function genderNormalization($anyGenderCode)
    {
        if ($anyGenderCode == 'M') {
            return 'male';
        } else if ($anyGenderCode == 'F') {
            return 'female';
        } else if ($anyGenderCode == 'O') {
            return 'others';
        } else if ($anyGenderCode == 'U') {
            return 'unknown';
        } else {
            return null;
        }
    }

    public static function genderPerson($anyGenderCode)
    {
        if ($anyGenderCode == 'M') {
            return 'L';
        } else if ($anyGenderCode == 'F') {
            return 'P';
        } else if ($anyGenderCode == 'O') {
            return 'others';
        } else if ($anyGenderCode == 'U') {
            return 'unknown';
        } else {
            return null;
        }
    }

    public static function paginateResources($resource)
    {
        return [
            'current_page' => $resource->currentPage(),
            'data' => $resource->items(),
            'first_page_url' => '',
            'from' => $resource->firstItem(),
            'last_page' => $resource->lastPage(),
            'last_page_url' => '',
            'links' => $resource->links(null, $resource->items()),
            'next_page_url' => $resource->nextPageUrl(),
            'per_page' => $resource->perPage(),
            'total' => $resource->total()
        ];
    }

    public static function dailyAvailabilitiesToDate($dailyAvailabilities, $startDate, $endDate = null)
    {

        Carbon::setLocale('id');

        $startDate = Carbon::parse($startDate);
        if (empty($endDate)) {
            $endDate = $startDate;
        } else {
            $endDate = Carbon::parse($endDate);
        }
        $ranges = CarbonPeriod::create($startDate, $endDate);

        $datesAvailabilities = [];
        foreach ($ranges as $date) {

            $datesAvailabilities[$date->format('Y-m-d')][] = [
                'date' => $date->format('Y-m-d'),
                'day' => $date->dayName,
                'slot' => $dailyAvailabilities[$date->dayName],
                'timezone' => 'WIB'
            ];
        }

        return $datesAvailabilities;
    }

    public static function dailyAvailabilities($availabilities)
    {
        $hours = [
            'Pagi' => [],
            'Siang' => [],
            'Sore' => [],
            'Petang' => [],
            'Malam' => []
        ];

        foreach ($availabilities as $availability) {
            $time = explode(':', $availability->start_time);

            if (count($availability->days)) {
                foreach ($availability->days as $key => $day) {
                    $periods = CarbonPeriod::create(now(), now()->lastOfMonth()->addDays(1))
                        ->filter(function (Carbon $date) use ($day) {
                            switch ($day->day) {
                                case 'Senin':
                                    return $date->isMonday();
                                    break;
                                case 'Selasa':
                                    return $date->isTuesday();
                                    break;
                                case 'Rabu':
                                    return $date->isWednesday();
                                    break;
                                case 'Kamis':
                                    return $date->isThursday();
                                    break;
                                case 'Jumat':
                                    return $date->isFriday();
                                    break;
                                case 'Sabtu':
                                    return $date->isSaturday();
                                    break;
                                case 'Minggu':
                                    return $date->isSunday();
                                    break;
                            }
                        })->toArray();
                }

                foreach ($periods as $day) {
                    foreach ($hours as $hour => $value) {
                        if (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(05, 00, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(10, 59, 00)->timestamp && $hour === "Pagi") {
                            $schedules[Carbon::create($day)->day][$hour][] = substr($availability->start_time, 0, -3);
                        } elseif (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(11, 00, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(15, 00, 00)->timestamp && $hour === "Siang") {
                            $schedules[Carbon::create($day)->day][$hour][] = substr($availability->start_time, 0, -3);
                        } elseif (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(15, 01, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(18, 00, 00)->timestamp && $hour === "Sore") {
                            $schedules[Carbon::create($day)->day][$hour][] = substr($availability->start_time, 0, -3);
                        } elseif (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(18, 00, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(19, 00, 00)->timestamp && $hour === "Petang") {
                            $schedules[Carbon::create($day)->day][$hour][] = substr($availability->start_time, 0, -3);
                        } elseif (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(19, 00, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(24, 00, 00)->timestamp && $hour === "Malam") {
                            $schedules[Carbon::create($day)->day][$hour][] = substr($availability->start_time, 0, -3);
                        }
                    }
                }
            } else {
                foreach ($hours as $hour => $value) {
                    for ($i = Carbon::now()->day; $i <= Carbon::now()->daysInMonth; $i++) {
                        if (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(05, 00, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(10, 59, 00)->timestamp && $hour === "Pagi") {
                            $schedules[$i][$hour][] = substr($availability->start_time, 0, -3);
                        } elseif (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(11, 00, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(15, 00, 00)->timestamp && $hour === "Siang") {
                            $schedules[$i][$hour][] = substr($availability->start_time, 0, -3);
                        } elseif (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(15, 01, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(18, 00, 00)->timestamp && $hour === "Sore") {
                            $schedules[$i][$hour][] = substr($availability->start_time, 0, -3);
                        } elseif (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(18, 00, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(19, 00, 00)->timestamp && $hour === "Petang") {
                            $schedules[$i][$hour][] = substr($availability->start_time, 0, -3);
                        } elseif (Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp >= Carbon::createFromTime(19, 00, 00)->timestamp && Carbon::createFromTime($time[0], $time[1], $time[2])->timestamp <= Carbon::createFromTime(24, 00, 00)->timestamp && $hour === "Malam") {
                            $schedules[$i][$hour][] = substr($availability->start_time, 0, -3);
                        }
                    }
                }
            }
        }

        return (object) $schedules;
    }

    public static function currencyIdrFormat(int $price)
    {
        return "Rp. " . number_format($price, 0, ',', '.');
    }

    /**
     * Helper Response Json.
     *
     * @param  array|object  $data
     * @param  int  $statusCode
     * @param  string  $message
     * @return JsonResponse
     */
    public static function responseJson(array|object $data = [], string $status = 'success', int $statusCode = Response::HTTP_OK, string $message = 'Data berhasil di ambil'): JsonResponse
    {
        return response()->json([
            'status' => $status,
            'statusCode' => $statusCode,
            'message' => $message,
            'data' => $data,
        ], $statusCode);
    }
}
