<?php

namespace modules\activities\models;

use Yii;
use modules\cors\models\Vehiclebe as Vehicle;
use modules\users\models\User;

/**
 * This is the model class For table "activities_users".
 *
 * @property string $id
 * @property string $actid
 * @property integer $operativeid
 * @property string $operativename
 * @property string $postcodefrom
 * @property integer $distance
 * @property integer $traveltime
 * @property integer $shifttime
 * @property integer $totaltime
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 * @property string $postcodeto
 */
class Activitiesusers extends  \common\models\Base {

    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'activities_users';
    }

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['actid', 'operativename', 'postcodefrom', 'postcodeto', 'vehicleid'], 'string'],
            [['distance', 'traveltime',  'operativeid' ], 'integer'],
			//Defaults
			[['deleted', 'direct'], 'boolean'],
			[['created_at', 'updated_at','shifttime', 'totaltime', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by','id'],'safe'],
			//Redundant
			[['created_by2', 'updated_by2'], 'integer'],
			['status', 'default', 'value' => 'Active'],
			['id', 'default', 'value' => self::Tempid()],
			['direct', 'default', 'value' => true],
			
		];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'actid' => 'Actid',
            'operativeid' => 'Operativeid',
            'operativename' => 'Operative Name',
            'postcodefrom' => 'Postcode From',
            'distance' => 'Distance (Miles)',
            'traveltime' => 'Travel Time',
            'shifttime' => 'Shift Time',
            'totaltime' => 'Total Time',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
            'postcodeto' => 'Postcode To',
        ];
    }

    public function getVehicle(){
        return $this->hasOne(Vehicle::class, ['id' => 'vehicleid']);
    }

    public function getActivity(){
        return $this->hasOne(Activities::class, ['id' => 'actid']);
    }

    public function beforeSave($insert)
    {
        $user = User::findOne(['username' => $this->operativename]);
        $this->postcodefrom = $this->postcodefrom == NULL ? $user->address->post_code : $this->postcodefrom;
        if (Yii::$app->name !== 'app-console' &&  Yii::$app->name !== 'NGaugeApi') {
            if ( empty($this->postcodefrom) == false && empty($this->postcodeto) == false && $this->direct == true) {
                $data = \modules\activities\components\GoogleDistanceMatrix::Getgoogledm($this->postcodefrom, $this->postcodeto, strtotime('now'), $this->activity->location);
                $this->distance = $data['distance'];
                $tt = $data['duration_in_traffic'];
                $this->traveltime = $tt;
                $journey = $this->activity->journey == 'Return' ? 2 :1;
                $time2  = $data['duration_in_traffic'] * $journey;
                $stime = intval($this->shifttime) * 60 * 60;
                $this->totaltime =  $stime + $time2;
            } elseif ( empty($this->postcodefrom) == false && empty($this->postcodeto) == false && $this->direct == false) {
                //to office
                $data = \modules\activities\components\GoogleDistanceMatrix::Getgoogledm($this->postcodefrom, Activities::Inlineaddress(), strtotime('now'), $this->activity->location);
                //to Site
                $data1 = \modules\activities\components\GoogleDistanceMatrix::Getgoogledm(Activities::Inlineaddress(), $this->postcodeto, strtotime('now'), $this->activity->location);
                $this->distance = str_replace(' mi', '', $data['distance']) + str_replace(' mi', '', $data1['distance']);
                $this->traveltime = $data['duration_in_traffic'] + $data1['duration_in_traffic'];
                $journey = $this->activity->journey == 'Return' ? 2 :1;
                $time2  = $this->traveltime * $journey;
                $stime = intval($this->shifttime) * 60 * 60;
                $this->totaltime =  $stime + $time2;
            }
        }
        return parent::beforeSave($insert);
    }
}
