<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class WorkOrderSchedule extends Model
{

	use SoftDeletes;
	
	protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
	
	protected $appends = ['start_at_formatted', 'end_at_formatted', 'start_date_formatted', 'end_date_formatted', 'start_time_formatted', 'end_time_formatted', 'schedule_formatted', 'frequency_formatted'];
	
	public function company()
	{
		return $this->belongsTo('App\Company');
	}
	
	public function work_order()
	{
		return $this->belongsTo('App\WorkOrder')->withTrashed();
	}
	
	public function getStartAtFormattedAttribute()
    {
		
		$start_time = ($this->attributes['start_time'] != '00:00:00') ? $this->attributes['start_time'] : 'ANYTIME';
		$start_at = date('m/d/Y', strtotime($this->attributes['start_date'])) . ' ' . $start_time;
		return $start_at;
		
    }
	
	public function getStartDateFormattedAttribute()
    {
		return date('m/d/Y', strtotime($this->attributes['start_date']));
    }
	
	public function getEndDateFormattedAttribute()
    {
		if ($this->attributes['end_date']) {
			if ($this->attributes['frequency'] == 'once') {
				return date('m/d/Y', strtotime($this->attributes['start_date']));
			} else {
				return date('m/d/Y', strtotime($this->attributes['end_date']));
			}
		} else {
			return 'NEVER';
		}
    }
	
	public function getStartTimeFormattedAttribute()
    {
		if ($this->attributes['start_time'] != '00:00:00') {
			return date('h:i A', strtotime($this->attributes['start_time']));
		} else {
			return 'ANYTIME';
		}
    }
	
	public function getEndTimeFormattedAttribute()
    {
		if ($this->attributes['end_time'] != '00:00:00') {
			return date('h:i A', strtotime($this->attributes['end_time']));
		} else {
			return 'ANYTIME';
		}
    }
	
	public function getScheduleFormattedAttribute()
    {
		return $this->getFrequencyFormattedAttribute() . ' / ' . $this->getStartTimeFormattedAttribute() . ' - ' . $this->getEndTimeFormattedAttribute();
    }
	
	public function getEndAtFormattedAttribute()
    {
		
		$end_at = '';
		
		if ($this->attributes['frequency'] == 'once' && (!$this->attributes['end_date'] || $this->attributes['end_date'] != $this->attributes['start_date'])) {
			$end_at = date('m/d/Y', strtotime($this->attributes['start_date']));
		}
		
		if ($this->attributes['end_time'] != '00:00:00' && $this->attributes['end_time'] != '23:59:59') {
			$end_at .= ' ' . $this->attributes['end_time'];
		} else {
			$end_at .= ' ANYTIME';
		}
		
		if (!$end_at) {
			return '-';
		}
		
		return $end_at;
		
    }
	
	private function _formatByDay($byday)
	{
		
		$byday = str_replace('MO', 'Monday', $byday);
		$byday = str_replace('SU', 'Sunday', $byday);
		$byday = str_replace('TU', 'Tuesday', $byday);
		$byday = str_replace('WE', 'Wednesday', $byday);
		$byday = str_replace('TH', 'Thursday', $byday);
		$byday = str_replace('FR', 'Friday', $byday);
		$byday = str_replace('SA', 'Saturday', $byday);
		$byday = str_replace(',', ', ', $byday);
		
		return $byday;
		
	}
	
	public function getFrequencyFormattedAttribute()
    {
		
		$frequency =  strtoupper($this->attributes['frequency']);
		
		if ($this->attributes['byday']) {
			$frequency .= ' (' . $this->_formatByDay($this->attributes['byday']) . ')';
		}
		
		if ($this->attributes['bymonthday']) {
			$frequency .= ' (' . $this->attributes['bymonthday'] . ')';
		}
		
		$frequency .= ' / INT ' . $this->attributes['interval'];
		
        return $frequency;
		
    }

}
