<?php

namespace App\Http\Controllers\Helpers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use DateTime;
use Exception;
use File;
use Redirect;
use Str;
use Storage;

class FileHelperController extends Controller
{
    private $IMAGE_TYPE_JPEG;
    private $IMAGE_TYPE_JPG;
    private $IMAGE_TYPE_PNG;
    private $PDF_TYPE;
    private $DOC_TYPE;
    private $DOCX_TYPE;
    private $XLS_TYPE;
    private $XLSX_TYPE;

    public function __construct()
    {
        $this->IMAGE_TYPE_JPEG = "jpeg";
        $this->IMAGE_TYPE_JPG = 'jpg';
        $this->IMAGE_TYPE_PNG = 'png';
        $this->PDF_TYPE = "pdf";
        $this->DOC_TYPE = 'doc';
        $this->DOCX_TYPE = 'docx';
        $this->XLS_TYPE = 'xls';
        $this->XLSX_TYPE = 'xlsx';
    }

    /* ================== Global ================== */
    public function checkImageType($imageType)
    {
        return strtolower($imageType) != $this->IMAGE_TYPE_JPEG && strtolower($imageType) != $this->IMAGE_TYPE_JPG && strtolower($imageType) != $this->IMAGE_TYPE_PNG;
    }

    public function checkMixType($fileType)
    {
        return strtolower($fileType) != $this->IMAGE_TYPE_JPEG &&
            strtolower($fileType) != $this->IMAGE_TYPE_JPG &&
            strtolower($fileType) != $this->IMAGE_TYPE_PNG &&
            strtolower($fileType) != $this->PDF_TYPE &&
            strtolower($fileType) != $this->DOC_TYPE &&
            strtolower($fileType) != $this->DOCX_TYPE &&
            strtolower($fileType) != $this->XLS_TYPE &&
            strtolower($fileType) != $this->XLSX_TYPE;
    }

    public function checkImageSize($fileSize)
    {
        $customFileSize = 20; // able to change here: //Your file size is bigger than 20mb please upload file again
        $limitMaxFileSize = 1024 * 1024 * $customFileSize;
        return $fileSize > $limitMaxFileSize;
    }

    public function checkFileType($fileType)
    {
        return strtolower($fileType) != $this->PDF_TYPE &&
            strtolower($fileType) != $this->DOC_TYPE &&
            strtolower($fileType) != $this->DOCX_TYPE &&
            strtolower($fileType) != $this->XLS_TYPE &&
            strtolower($fileType) != $this->XLSX_TYPE;
    }

    public function checkFileTypePdfOnly($fileType)
    {
        return $fileType != $this->PDF_TYPE;
    }

    public function checkFileSize($fileSize)
    {
        $customFileSize = 20; // able to change here: //Your file size is bigger than 20mb please upload file again
        $limitMaxFileSize = 1024 * 1024 * $customFileSize;
        return $fileSize > $limitMaxFileSize;
    }

    public function checkFileSize3MB($fileSize)
    {
        $customFileSize = 3; // able to change here: //Your file size is bigger than 3mb please upload file again
        $limitMaxFileSize = 1024 * 1024 * $customFileSize;
        return $fileSize > $limitMaxFileSize;
    }

    public function checkFileSize5MB($fileSize)
    {
        $customFileSize = 5; // able to change here: //Your file size is bigger than 3mb please upload file again
        $limitMaxFileSize = 1024 * 1024 * $customFileSize;
        return $fileSize > $limitMaxFileSize;
    }


    /* ================== CK Editor ================== */

    public function uploadImageCkEditor(Request $request)
    {
        $image = $request->file('upload');
        $url = $this->uploadImg($image);
        $funcNum = $_GET['CKEditorFuncNum'];
        if ($url == 'errors') {
            return "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '" . url($url) . "', 'Error file is large.')</script>";
        } else {
            return "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '" . url($url) . "', '')</script>";
        }
    }

    public function uploadImg($file)
    {
        try {
            $folder = "uploads/ckeditor/images";

            if (File::isDirectory($folder) == false) {
                Storage::makeDirectory($folder, 0777, true);
            }

            $name = $file->getClientOriginalName();
            $ext = pathinfo($name, PATHINFO_EXTENSION);
            $rdStr = rand();
            $date = new DateTime();
            $format = $date->format('Ymdhms');
            $newName = $format . '_' . $rdStr . '.' . $ext;
            $file->move($folder, $newName);
            return $folder . "/" . $newName;
        } catch (Exception $e) {
            return 'errors';
        }
    }

    public function mimeContentTypeFile($type)
    {
        $typeLower = strtolower($type);
        if ($typeLower === 'pdf') {
            return 'application/pdf';
        } else if ($typeLower === 'xls' || $typeLower === 'xlsx') {
            return 'application/octet-stream';
        } else if ($typeLower === 'doc' || $typeLower === 'docx') {
            return 'application/octet-stream';
        } else {
            return 'jpg';
        }
    }

    public function mimeContentTypeImage($type)
    {
        $typeLower = strtolower($type);
        if ($typeLower === 'png') {
            return 'image/png';
        } else if ($typeLower === 'jpg') {
            return 'image/jpeg';
        } else if ($typeLower === 'jpeg') {
            return 'image/jpeg';
        } else {
            return 'image/jpeg';
        }
    }

    /*
        public function uploadImageCkEditorOld($file)
        {
            try {
                $folder = "uploads/files/ckeditor/images";

                $name = $file->getClientOriginalName();
                $ext = pathinfo($name, PATHINFO_EXTENSION);
                $rdStr = Uuid::generate();
                $date = new DateTime();
                $format = $date->format('Ymdhms');
                $newName = $format . '_' . $rdStr . '.' . $ext;
                $file->move($folder, $newName);

                return $folder . "/" . $newName;
            } catch (\Exception $e) {
                return 'errors';
            }
        }
    */
}
/* File Types */
//[
//    '3gp' => 'video/3gpp',
//    '7z' => 'application/x-7z-compressed',
//    'aac' => 'audio/x-aac',
//    'ai' => 'application/postscript',
//    'aif' => 'audio/x-aiff',
//    'asc' => 'text/plain',
//    'asf' => 'video/x-ms-asf',
//    'atom' => 'application/atom+xml',
//    'avi' => 'video/x-msvideo',
//    'bmp' => 'image/bmp',
//    'bz2' => 'application/x-bzip2',
//    'cer' => 'application/pkix-cert',
//    'crl' => 'application/pkix-crl',
//    'crt' => 'application/x-x509-ca-cert',
//    'css' => 'text/css',
//    'csv' => 'text/csv',
//    'cu' => 'application/cu-seeme',
//    'deb' => 'application/x-debian-package',
//    'doc' => 'application/msword',
//    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
//    'dvi' => 'application/x-dvi',
//    'eot' => 'application/vnd.ms-fontobject',
//    'eps' => 'application/postscript',
//    'epub' => 'application/epub+zip',
//    'etx' => 'text/x-setext',
//    'flac' => 'audio/flac',
//    'flv' => 'video/x-flv',
//    'gif' => 'image/gif',
//    'gz' => 'application/gzip',
//    'htm' => 'text/html',
//    'html' => 'text/html',
//    'ico' => 'image/x-icon',
//    'ics' => 'text/calendar',
//    'ini' => 'text/plain',
//    'iso' => 'application/x-iso9660-image',
//    'jar' => 'application/java-archive',
//    'jpe' => 'image/jpeg',
//    'jpeg' => 'image/jpeg',
//    'jpg' => 'image/jpeg',
//    'js' => 'text/javascript',
//    'json' => 'application/json',
//    'latex' => 'application/x-latex',
//    'log' => 'text/plain',
//    'm4a' => 'audio/mp4',
//    'm4v' => 'video/mp4',
//    'mid' => 'audio/midi',
//    'midi' => 'audio/midi',
//    'mov' => 'video/quicktime',
//    'mkv' => 'video/x-matroska',
//    'mp3' => 'audio/mpeg',
//    'mp4' => 'video/mp4',
//    'mp4a' => 'audio/mp4',
//    'mp4v' => 'video/mp4',
//    'mpe' => 'video/mpeg',
//    'mpeg' => 'video/mpeg',
//    'mpg' => 'video/mpeg',
//    'mpg4' => 'video/mp4',
//    'oga' => 'audio/ogg',
//    'ogg' => 'audio/ogg',
//    'ogv' => 'video/ogg',
//    'ogx' => 'application/ogg',
//    'pbm' => 'image/x-portable-bitmap',
//    'pdf' => 'application/pdf',
//    'pgm' => 'image/x-portable-graymap',
//    'png' => 'image/png',
//    'pnm' => 'image/x-portable-anymap',
//    'ppm' => 'image/x-portable-pixmap',
//    'ppt' => 'application/vnd.ms-powerpoint',
//    'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
//    'ps' => 'application/postscript',
//    'qt' => 'video/quicktime',
//    'rar' => 'application/x-rar-compressed',
//    'ras' => 'image/x-cmu-raster',
//    'rss' => 'application/rss+xml',
//    'rtf' => 'application/rtf',
//    'sgm' => 'text/sgml',
//    'sgml' => 'text/sgml',
//    'svg' => 'image/svg+xml',
//    'swf' => 'application/x-shockwave-flash',
//    'tar' => 'application/x-tar',
//    'tif' => 'image/tiff',
//    'tiff' => 'image/tiff',
//    'torrent' => 'application/x-bittorrent',
//    'ttf' => 'application/x-font-ttf',
//    'txt' => 'text/plain',
//    'wav' => 'audio/x-wav',
//    'webm' => 'video/webm',
//    'webp' => 'image/webp',
//    'wma' => 'audio/x-ms-wma',
//    'wmv' => 'video/x-ms-wmv',
//    'woff' => 'application/x-font-woff',
//    'wsdl' => 'application/wsdl+xml',
//    'xbm' => 'image/x-xbitmap',
//    'xls' => 'application/vnd.ms-excel',
//    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
//    'xml' => 'application/xml',
//    'xpm' => 'image/x-xpixmap',
//    'xwd' => 'image/x-xwindowdump',
//    'yaml' => 'text/yaml',
//    'yml' => 'text/yaml',
//    'zip' => 'application/zip',
//];