<?php

namespace App\Repositories\Webprofile;

use App\Models\Webprofile\File;
use App\Repositories\StorageRepository;
use DataTables;

class FileRepository extends StorageRepository
{
    protected $model;

    public function __construct(File $model)
    {
        $this->model = $model;
    }

    public function get($with = null, $title = null, $orderBy = null)
    {
        return $this->model
            ->when($with, function ($query) use ($with) {
                return $query->with($with);
            })
            ->when($title, function ($query) use ($title) {
                return $query->where('title', 'ilike', '%'.$title.'%');
            })
            ->when($orderBy, function ($query) use ($orderBy) {
                return $query->orderBy($orderBy[0], $orderBy[1]);
            })
            ->get();
    }

    public function datatable($data)
    {
        $setting = webprofilesetting();

        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($row) {
                $btn = '<a href="'.url('/webprofile/file/'.$row->id.'/edit').'" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="'.trans('label.edit').'" class="edit btn btn-warning btn-round btn-sm edit">'.trans('label.edit').'</a>';

                $btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="'.trans('label.delete').'" class="btn btn-danger btn-round btn-sm delete">'.trans('label.delete').'</a>';

                $btn = $btn.'<br>';

                return $btn;
            })
            ->addColumn('category', function ($row) {
                if ($row->rCategoryFile) {
                    return ucfirst($row->rCategoryFile->name);
                } else {
                    return '';
                }
            })
            ->addColumn('file', function ($row) use ($setting) {
                if ($setting['external_storage'] == 1) {
                    $str = '<a href="'.$setting['url_static'].'/'.$setting['directory'].'/file/'.$row->file.'" style="font-size: 12pt;">'.ucfirst($row->title).'</a>';
                    $str .= '<label id="furl_'.$row->id.'" hidden>'.$setting['url_static'].'/'.$setting['directory'].'/file/'.$row->file.'</label>';
                } else {
                    $str = '<a href="'.url('/storage/file/'.$row->file).'" style="font-size: 12pt;">'.ucfirst($row->title).'</a>';
                    $str .= '<label id="furl_'.$row->id.'" hidden>'.url('/storage/file/'.$row->file).'</label>';
                }

                $str .= '<br>';
                $str .= '<button onclick="copyToClipboard(\'#furl_'.$row->id.'\')" class="btn btn-info btn-xs">Copy URL</button>';

                return $str;
            })
            ->addColumn('downloaded', function ($row) {
                return $row->downloaded;
            })
            ->addColumn('status', function ($row) {
                if ($row->is_active == true) {
                    $str = '<div style="color: green;"><i class="fa fa-check"></i></div>';
                } else {
                    $str = '<div style="color: red;"><i class="fa fa-times"></i></div>';
                }

                return $str;
            })
            ->rawColumns(['action', 'status', 'category', 'file', 'downloaded'])
            ->make(true);
    }

    public function is_url_exist($url)
    {
        stream_context_set_default([
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ]);

        $headers = get_headers($url);

        return stripos($headers[0], '200 OK') ? true : false;
    }

    public function countDownload(File $file)
    {
        $file->downloaded = $file->downloaded + 1;

        return $file->save();
    }
}
