<?php

namespace App\Modules\Bookings;

use Laracasts\Presenter\Presenter;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Log;

class DevicePresenter extends Presenter
{
    protected $boolProperties = ['Has PSU', 'Has Case', 'Has Antivirus', 'Has Viruses', 'Is Secure'];
    protected $statusProperties = ['HDD Status', 'RAM Status'];
    public function ramStatusMessage()
    {
        if ($this->ram_status === 0) {
            return "<span class='label label-danger'>FAULTY</span>";
        }

        if ($this->ram_size && $this->recommended_ram_size && $this->ram_size < $this->recommended_ram_size) {
            return "<span class='label label-danger'>LOWER THAN RECOMMENDED (".$this->recommended_ram_size.'GB)</span>';
        }

        if ($this->ram_status === null || $this->ram_size === null || $this->recommended_ram_size === null) {
            return '';
        }

        return "<span class='label label-success'>ALRIGHT</span>";
    }

    public function ramSize()
    {
        if ($this->recommended_ram_size) {
            return $this->entity->ram_size < $this->entity->recommended_ram_size ? '<span class="text-danger"><i class="fa fa-exclamation-triangle"></i> '.$this->entity->ram_size.'GB</span>' : $this->entity->ram_size.'GB';
        }

        return $this->ram_size;
    }

    public function ramSpeed()
    {
        return $this->ram_speed.'GHz';
    }

    public function hddSize()
    {
        return $this->hdd_size.'GB';
    }

    public function hddStatusMessage()
    {
        if ($this->hdd_status === 0) {
            return "<span class='label label-danger'>FAULTY</span>";
        }

        if ($this->hdd_status === null) {
            return '';
        }

        return "<span class='label label-success'>ALRIGHT</span>";
    }

    public function antivirusStatusMessage()
    {
        if ($this->has_antivirus === 0) {
            return "<span class='label label-danger'>NO ANTIVIRUS</span>";
        }

        if ($this->antivirus_expiration_date != null) {
            $expiration_date = new Carbon($this->antivirus_expiration_date);
            $now = Carbon::now();

            if ($expiration_date < $now) {
                return "<span class='label label-danger'>EXPIRED LICENCE</span>";
            }

            if ($expiration_date->diffInDays($now) <= 28) {
                return "<span class='label label-danger'>LICENCE EXPIRING SOON</span>";
            }
        }

        if ($this->num_viruses) {
            return "<span class='label label-danger'>HAS VIRUSES</span>";
        }

        if ($this->is_secure === 0 && $this->has_antivirus) {
            return "<span class='label label-danger'>NOT RECOMmENDED ANTIVIRUS</span>";
        }

        if ($this->is_secure === null || $this->num_viruses === null || $this->antivirus_expiration_date === null || $this->has_antivirus) {
            return '';
        }

        return "<span class='label label-success'>ALRIGHT</span>";
    }

    public function antivirusProgram()
    {
        if ($this->has_antivirus) {
            return $this->is_secure ? $this->antivirus : '<span class="text-danger"><i class="fa fa-exclamation-triangle"></i> '.$this->antivirus.'</span>';
        } else {
            return '<span class="text-danger"><i class="fa fa-exclamation-triangle"></i> No program</span>';
        }
    }

    public function antivirusExpirationDate()
    {
        $expiration_date = new Carbon($this->antivirus_expiration_date);
        $now = Carbon::now();

        return ($expiration_date < $now || $expiration_date->diff($now, false)->days < 10) ? "<span class='text-danger'><i class='fa fa-exclamation-triangle'></i> ".$expiration_date->format('d-m-Y').'</span>' : $expiration_date->format('d-m-Y');
    }

    public function numViruses()
    {
        return ($this->num_viruses > 0) ?  '<span class="text-danger"><i class="fa fa-exclamation-triangle"></i> '.$this->num_viruses.'</span>' : $this->num_viruses;
    }

    public function isSecure()
    {
        return ($this->is_secure === 0) ?  '<span class="text-danger"><i class="fa fa-exclamation-triangle"></i> No</span>' : 'Yes';
    }

    public function hasAntivirus()
    {
        return ($this->has_antivirus === 0) ?  '<span class="text-danger"><i class="fa fa-exclamation-triangle"></i> No</span>' : 'Yes';
    }

    public function printChange($history)
    {
        try {
            if ($history->fieldName() == 'Antivirus Expiration Date') {
                $newValue = new Carbon($history->oldValue());
                $oldValue = new Carbon($history->newValue());

                return 'changed <strong>'.$history->fieldName().'</strong> from <strong>'.$newValue->format('d-m-Y').'</strong> to <strong>'.$oldValue->format('d-m-Y').'</strong>';
            }

            if (in_array($history->fieldName(), $this->boolProperties)) {
                $from = $history->oldValue() ? 'Yes' : 'No';
                $to = $history->newValue() ? 'Yes' : 'No';

                return 'changed <strong>'.$history->fieldName()."</strong> from <strong>$from</strong> to <strong>$to</strong>";
            }

            if (in_array($history->fieldName(), $this->statusProperties)) {
                $from = $history->oldValue() ? 'Healthy' : 'Failed';
                $to = $history->newValue() ? 'Healthy' : 'Failed';

                return 'changed <strong>'.$history->fieldName()."</strong> from <strong>$from</strong> to <strong>$to</strong>";
            }

            return 'changed <strong>'.$history->fieldName().'</strong> from <strong>'.$history->oldValue().'</strong> to <strong>'.$history->newValue().'</strong>';
        } catch (Exception $e) {
            dd($e);
            Log::warning($e);
        }
    }
}
?>