<?php

namespace App\Utils;

use App\Entities\Agenda\Agenda;
use App\Utils\Collections\GeneralCollection;
use App\ViewModels\Agenda\AgendaConflictingViewModel\AgendaConflictingViewModel;
use DateInterval;
use DateTime;

class DateUtils {

	public static function plus_minutes_to_datetime(string $datetime, int $minutes): string {

		return date( 'Y-m-d H:i:s', strtotime( "+$minutes minute", strtotime( $datetime ) ) );

	}

	public static function compare_dates(string $date_1, $date_2): int {

		return $date_1 > $date_2 ? 1 : $date_1 < $date_2 ? -1 : 0;

	}

	public static function datetime_to_time(DateTime $date_time): string {

		return $date_time->format( 'H:i' );

	}

	public static function datetime_to_date(DateTime $date_time): string {

		return $date_time->format( 'Y-m-d' );

	}

	public static function requestInt_to_date(int $valor): string {

		$texto = strval( $valor );
		if (strlen( trim( $texto ) ) == 8) {
			$ano = substr( $texto, 0, 4 );
			$mes = substr( $texto, 4, 2 );
			$dia = substr( $texto, 6, 2 );
			if (checkdate( intval( $mes ), intval( $dia ), intval( $ano ) )) {
				return ($ano . '-' . $mes . '-' . $dia);
			}
		}
		return 'falso';

	}

	public static function today(): string {

		return date( 'Y-m-d' );

	}

	public static function now(): string {

		return date( 'Y-m-d H:i:s' );

	}

	public static function now_time(): string {

		return date( 'H:i:s' );

	}

	public static function getDateTime(DateTime $dateTime): DateTime {

		return $dateTime;

	}

	public static function dateIntervalMinutes(DateInterval $date_inverval): int {

		return $date_inverval->h * MINUTE + $date_inverval->i;

	}

	public static function calculateConflictsOnSchedule(string $initial_schedule, string $final_schedule,
			GeneralCollection $potential_conflicts_filtered_by_date) {

		for($i = 0; $i < $potential_conflicts_filtered_by_date->length; $i++) {
			$agenda_conflicting_vm = $potential_conflicts_filtered_by_date->get( $i );
			$potential_conflict_initial_schedule = $agenda_conflicting_vm->getHoraInicio();
			$potential_conflict_final_schedule = $agenda_conflicting_vm->getHoraFinal();
			if (($initial_schedule >= $potential_conflict_initial_schedule and $initial_schedule < $potential_conflict_final_schedule) or
					($initial_schedule >= $potential_conflict_initial_schedule and $final_schedule < $potential_conflict_final_schedule) or
					($initial_schedule < $potential_conflict_initial_schedule and $initial_schedule >= $potential_conflict_final_schedule) or
					($initial_schedule < $potential_conflict_initial_schedule and $final_schedule >= $potential_conflict_final_schedule) or
					($final_schedule >= $potential_conflict_initial_schedule and $initial_schedule < $potential_conflict_final_schedule) or
					($final_schedule >= $potential_conflict_initial_schedule and $final_schedule < $potential_conflict_final_schedule) or
					($final_schedule < $potential_conflict_initial_schedule and $initial_schedule >= $potential_conflict_final_schedule) or
					($final_schedule < $potential_conflict_initial_schedule and $final_schedule >= $potential_conflict_final_schedule)) {
				return $agenda_conflicting_vm->getId();
			}
		}
		return 0;

	}

	public static function isDateValid(string $data): bool {

		$ano = substr( $data, 0, 4 );
		$mes = substr( $data, 5, 2 );
		$dia = substr( $data, 8, 2 );
		
		if(!intval($ano) || !intval($mes) || !intval($dia)){
			return false;
		}
		
		return checkdate(intval($mes), intval($dia), intval($ano));

	}
	
	public static function isHoraValid(string $hora): bool {
		$hr = substr($hora, 0,2);
		$mn = substr($hora, 3,2);
		
		if((intval($hr) < 24) && (intval($mn) < 60)) {
			return true;
		}
		
		return false;
	}
	
	public static function addDayToDate(int $days, string $data ):String {
		$adddays = ' + ' . strval($days) . ' days';
		$newdata = date('Y-m-d', strtotime($data . $adddays));
		return $newdata;
	}

}