<?php

namespace Bixo\Asset\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class AssetResource extends JsonResource
{

    public function itemLink()
    {
        return guard_url('asset/asset') . '/' . $this->getRouteKey();
    }

    public function title()
    {
        if ($this->title != '') {
            return $this->title;
        }

        if ($this->name != '') {
            return $this->name;
        }

        return 'None';
    }

    public function toArray($request)
    {
        return [
            'id' => $this->getRouteKey(),
            'title' => $this->title(),
            'slug' => $this->slug,
            'category' => $this->category,
            'sub_category' => $this->sub_category,
            'name' => $this->name,
            'serial_no' => $this->serial_no,
            'tags' => $this->tags,
            'brand' => $this->brand,
            'model_no' => $this->model_no,
            'ram' => $this->ram,
            'storage' => $this->storage,
            'imei_no' => $this->imei_no,
            'ip_address' => $this->ip_address,
            'current_location' => $this->current_location,
            'purchased_from' => $this->purchased_from,
            'previous_state' => $this->previous_state,
            'purchased_date' => $this->purchased_date,
            'warranty_start_date' => $this->warranty_start_date,
            'warranty_end_date' => $this->warranty_end_date,
            'assign_to' => $this->assign_to,
            'previous_owner' => $this->previous_owner,
            'assign_date' => $this->assign_date,
            'return_date' => $this->return_date,
            'photo' => $this->photo,
            'history' => $this->history,
            'user_id' => $this->user_id,
            'user_type' => $this->user_type,
            'created_at' => !is_null($this->created_at) ? $this->created_at->format('Y-m-d H:i:s') : null,
            'updated_at' => !is_null($this->updated_at) ? $this->updated_at->format('Y-m-d H:i:s') : null,
            'meta' => [
                'exists' => $this->exists(),
                'link' => $this->itemLink(),
                'upload_url' => $this->getUploadURL(''),
                'workflow' => $this->workflows(),
                'actions' => $this->actions(),
            ],
        ];
    }

    /**
     * Get additional data that should be returned with the resource array.
     *
     * @param     \Illuminate\Http\Request  $request
     * @return array
     */
    public function with($request)
    {
        return [
            'meta' => [
                'exists' => $this->exists(),
                'link' => $this->itemLink(),
                'upload_url' => $this->getUploadURL(''),
                'workflow' => $this->workflows(),
                'actions' => $this->actions(),
            ],
        ];
    }

    /**
     * Get the workflows for the resource.
     *
     * @return array
     */
    private function workflows()
    {
        $arr = [];
                $workflow = $this->resource->workflow();
        foreach ($workflow->transitions($this) as $key => $transition) {
            $name = $transition->getName();
            $arr[$key]['url'] = guard_url('asset/asset/workflow/' . $this->getRouteKey() . '/' . $name);
            $arr[$key]['name'] = $name;
            $arr[$key]['key'] = $name;
            $arr[$key]['form'] = $workflow->form($transition);
            $arr[$key]['label'] = trans('asset::asset.workflow.' . $name);
        }
                return $arr;

    }
    
    /**
     * Get the actions for the resource.
     *
     * @return array
     */
    private function actions()
    {

        $arr = [];
                $actions = $this->resource->actions()->details();
        foreach ($actions as $key => $action) {
            $name = $action->name();
            $arr[$key]['url'] = guard_url('asset/asset//action' . $this->getRouteKey() . '/' . $action->name());
            $arr[$key]['name'] = $name;
            $arr[$key]['key'] = $name;
            $arr[$key]['form'] = $action->form();
            $arr[$key]['label'] = trans('asset::asset.actions.' . $name);
        }
        
        return $arr;
    }
}