<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Toastr;
use EF;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    private $message;

    protected function successMessage($message,$log_message = "",$status = 1)
    {
        $this->clearMessageLog($status);

        Toastr::success($message, 'Success', ["positionClass" => "toast-top-right"]);

        if(strlen(trim($log_message)) > 0)
        {
            EF::createLogs($log_message);
        }
        else
        {
            EF::createLogs($message);
        }
    }

    protected function errorMessage($message,$status = 1)
    { 
      $this->clearMessageLog($status);
        Toastr::error($message, 'Fail', ["positionClass" => "toast-top-right"]);
    }

    protected function warningMessage($message,$status = 1)
    {
      $this->clearMessageLog($status);
        Toastr::warning($message, 'Warning', ["positionClass" => "toast-top-right"]);
    }

    protected function clearMessageLog($status)
    {
      if($status == 1)
      {
        Toastr::clear();
      }
      
    }

    public function filterConditionalArray($array)
    {
        $title = "";

        foreach ($array as $key => $value) 
        {

            if($value == "0")
            {
                unset($array[$key]);
            }
            else
            {
                $title.=ucwords($value).' ';
            }
       }

       return [$array,$title];
    }

    // protected function createLogs($message)
    // {
    //    $file_name = 'estatech_logs/'.date('d-m-Y').'.json';    

    //     try {
    //        // my data storage location is project_root/storage/app/data.json file.
    //         $old_logs = \Storage::disk('local')->exists($file_name) ? json_decode(\Storage::disk('local')->get($file_name)) : [];
    //        $user = \Auth::user();

    //        if(isset($user->id))
    //        {

    //            date_default_timezone_set("Asia/karachi");

              
    //            $inputData = array(
    //                    'user_id'   => $user->id,
    //                    'name'      => $user->name,
    //                    'branch'    => $user->branch->name,
    //                    'log_date'  => date('d-M-Y'),
    //                    'log_time'  => date('H:i:s'),
    //                    'message'   => str_replace('.','', $message.' by '.$user->name).'.',
    //                );



    //            array_push($old_logs,$inputData);

    //            \Storage::disk('local')->put($file_name, json_encode($old_logs),'public');

    //             // Session::put('logs_key',$primary_key);
    //        }

    //    } catch(Exception $e) {

    //        return ['error' => true, 'message' => $e->getMessage()];

    //    }
    // }

    public function setLogMessage($msg)
    {
       $this->message = $msg;
    }

    public function getLogMessage()
    {
        return $this->message;
    }
}
