<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MeetingReminder extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'meeting:reminder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send reminder to scheduled meetings.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    protected $files;

    public function __construct()
    {
        parent::__construct();
        $this->files = new \Illuminate\Filesystem\Filesystem;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        /*foreach ($this->files->files(storage_path().'/views') as $file)
        {
            $this->files->delete($file);
        }*/

        $currentTimeZone = date_default_timezone_get();
        $yesterdaysDate = date('Y-m-d', strtotime('-1 days'));
        $tomorrowsDate = date('Y-m-d', strtotime('+1 days'));

        $this->appointmentRepository = app('App\Repositories\\AppointmentRepository');
        $upcomingAppointments = $this->appointmentRepository->getUpcomingAppointments($yesterdaysDate, $tomorrowsDate);

        /*$headers = "From: shubhangee@wakhal.in" . "\r\n";
        mail("shubhangee@wakhal.in","Test","Reminder test",$headers);*/

        foreach ($upcomingAppointments as $appointment) {
            $aptTimeZone = $appointment->time_zone;
            date_default_timezone_set($aptTimeZone);
            $aptTime = $appointment->appointment_date.' '.$appointment->start_time;
            $convertedTime = getConvrtedTime($aptTime, $aptTimeZone);
            $currentTime = date('Y-m-d h:i A');

            $convertedNextsZero = date('Y-m-d h:i A', strtotime(' +0 minutes', strtotime(date('Y-m-d H:i:s'))));

            if ($convertedNextsZero == $convertedTime) {
                // echo '15 min alert';
                $this->appointmentRepository->sendReminderEmail($appointment);
            }

            /*$convertedNextsFifteen=date('Y-m-d h:i A', strtotime(' +2 minutes',strtotime(date('Y-m-d H:i:s'))));

            if($convertedNextsFifteen==$convertedTime){
                //echo '15 min alert';
                $this->appointmentRepository->sendReminderEmail($appointment);
            }

            $convertedNextsFifteen=date('Y-m-d h:i A', strtotime(' +5 minutes',strtotime(date('Y-m-d H:i:s'))));

            if($convertedNextsFifteen==$convertedTime){
                //echo '5 min alert';
                $this->appointmentRepository->sendReminderEmail($appointment);
            }

            $convertedNextsFifteen=date('Y-m-d h:i A', strtotime(' +10 minutes',strtotime(date('Y-m-d H:i:s'))));

            if($convertedNextsFifteen==$convertedTime){
                //echo '10 min alert';
                $this->appointmentRepository->sendReminderEmail($appointment);
            }*/

            $convertedNextsFifteen = date('Y-m-d h:i A', strtotime(' +15 minutes', strtotime(date('Y-m-d H:i:s'))));

            if ($convertedNextsFifteen == $convertedTime) {
                // echo '15 min alert';
                $this->appointmentRepository->sendReminderEmail($appointment);
            }

            $convertedNextsThirty = date('Y-m-d h:i A', strtotime(' +30 minutes', strtotime(date('Y-m-d H:i:s'))));
            if ($convertedNextsThirty == $convertedTime) {
                // echo '30 min alert';
                $this->appointmentRepository->sendReminderEmail($appointment);
            }

            /*$convertedNextshr=date('Y-m-d h:i A', strtotime(' +60 minutes',strtotime(date('Y-m-d H:i:s'))));
            if($convertedNextshr==$convertedTime){
                echo '1 hr alert';

                $this->appointmentRepository->sendReminderEmail($appointment);
            }

            $convertedNextsday=date('Y-m-d h:i A', strtotime(' +1 day',strtotime(date('Y-m-d H:i:s'))));

            if($convertedNextsday==$convertedTime){
                echo '1 day alert';

                $this->appointmentRepository->sendReminderEmail($appointment);
            }		*/
        }
        // echo $appointment->id;
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */

    /*protected function getArguments()
    {
        return array(
            array('example', InputArgument::REQUIRED, 'An example argument.'),
        );
    }*/

    protected function getArguments()
    {
        return [];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            ['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
        ];
    }
}
