<?php
/**
 * Created by PhpStorm.
 * User: P_rexxyz
 * Date: 13/08/2018
 * Time: 11:00 AM
 */

namespace App\Http\Controllers;
use App\advert;
use App\Client;
use App\payment;
use App\Voters;
use App\Votes;
use Faker\Provider\File;
use finfo;
use function foo\func;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
//use App\Advert;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Notifications\verify;
use Illuminate\Support\Facades\Auth;
use App\Mail\pingAdmin;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;



class ApiController
{
    public function users()
    {
        $users = Client::query()->get();

        return response()->json($users);
    }

    public function register(Request $request)
    {



        $data = $request->except(['user_photo','id_photo_front','id_photo_back','form_photo']);


        try {

            $user = Client::query()->create($data);

            if(!empty($request->user_photo))
            {

                $img  = $request->user_photo;   //base64 code


//                Log::error($img);

                $name = $user->name().date('h:i:s').'.jpg';
                $name = str_replace(' ', '_', $name);

                $img = str_replace(' ', '+', $img);

                /*** End of Base64 Conversion ***/
                $img = base64_decode($img);
                    \File::put(storage_path(). '/app/public/user_photo/' . $name, $img);
//
                    $data['user_photo'] = $name;
//
                $user->update($data);

            }

            if(!empty($request->id_photo_front))
            {

                $img  = $request->id_photo_front;   //base64 code


                $name = $user->name().date('h:i:s').'.jpg';
                $name = str_replace(' ', '_', $name);

                $img = str_replace(' ', '+', $img);

                /*** End of Base64 Conversion ***/
                $img = base64_decode($img);
                \File::put(storage_path(). '/app/public/id_photo_front/' . $name, $img);
//
                $data['id_photo_front'] = $name;
//
                $user->update($data);

            }

            if(!empty($request->id_photo_back))
            {

                $img  = $request->id_photo_back;   //base64 code


                $name = $user->name().date('h:i:s').'.jpg';
                $name = str_replace(' ', '_', $name);

                $img = str_replace(' ', '+', $img);

                /*** End of Base64 Conversion ***/
                $img = base64_decode($img);
                \File::put(storage_path(). '/app/public/id_photo_back/' .$name, $img);
//
                $data['id_photo_back'] = $name;
//
                $user->update($data);

            }

            if(!empty($request->form_photo))
            {

                $img  = $request->form_photo;   //base64 code


                $name = $user->name().date('h:i:s').'.jpg';
                $name = str_replace(' ', '_', $name);

                $img = str_replace(' ', '+', $img);

                /*** End of Base64 Conversion ***/
                $img = base64_decode($img);
                \File::put(storage_path(). '/app/public/form_photo/' .$name, $img);
//
                $data['form_photo'] = $name;
//
                $user->update($data);

            }





        }
        catch (\Exception $ex){
            return response()->json(['code' => 0, 'msg' => $ex->getMessage()]);
        }

        return response()->json(['code' => 200, 'msg' => $user->id]);


    }


    public function update(Request $request,$id)
    {

        $user = Client::query()->find($id);

        if($request->isMethod('POST'))
        {
            $data = $request->except(['user_photo','id_photo_front','id_photo_back','form_photo']);


            try {
                $user->update($data);

                if(!empty($request->user_photo))
                {

                    $img  = $request->user_photo;   //base64 code

                    $name = $user->name().date('h:i:s').'.jpg';
                    $name = str_replace(' ', '_', $name);


                    $img = str_replace(' ', '+', $img);

                    /*** End of Base64 Conversion ***/
                    $img = base64_decode($img);
                    \File::put(storage_path(). '/app/public/user_photo/' . $name, $img);
                    Storage::delete('public/user_photo/'.$user->user_photo);
                    $data['user_photo'] = $name;
//
                    $user->update($data);

                }

                if(!empty($request->id_photo_front))
                {

                    $img  = $request->id_photo_front;   //base64 code

                    $name = $user->name().date('h:i:s').'.jpg';
                    $name = str_replace(' ', '_', $name);


                    $img = str_replace(' ', '+', $img);

                    /*** End of Base64 Conversion ***/
                    $img = base64_decode($img);
                    \File::put(storage_path(). '/app/public/id_photo_front/' . $name, $img);
                    Storage::delete('public/id_photo_front/'.$user->id_photo_front);
                    $data['id_photo_front'] = $name;
//
                    $user->update($data);

                }

                if(!empty($request->id_photo_back))
                {

                    $img  = $request->id_photo_back;   //base64 code

                    $name = $user->name().date('h:i:s').'.jpg';
                    $name = str_replace(' ', '_', $name);


                    $img = str_replace(' ', '+', $img);

                    /*** End of Base64 Conversion ***/
                    $img = base64_decode($img);
                    \File::put(storage_path(). '/app/public/id_photo_back/' . $name, $img);
                    Storage::delete('public/id_photo_back/'.$user->id_photo_back);
                    $data['id_photo_back'] = $name;
//
                    $user->update($data);

                }

                if(!empty($request->form_photo))
                {

                    $img  = $request->form_photo;   //base64 code

                    $name = $user->name().date('h:i:s').'.jpg';
                    $name = str_replace(' ', '_', $name);


                    $img = str_replace(' ', '+', $img);

                    /*** End of Base64 Conversion ***/
                    $img = base64_decode($img);
                    \File::put(storage_path(). '/app/public/form_photo/' . $name, $img);
                    Storage::delete('public/form_photo/'.$user->form_photo);
                    $data['form_photo'] = $name;
//
                    $user->update($data);

                }
            }
            catch (\Exception $ex){
                return response()->json(['code' => 0, 'msg' => $ex->getMessage()]);
            }

            return response()->json(['code' => 200, 'msg' => 'Client successfully updated']);

        }

        return response()->json($user);


    }


    public function login(Request $request)
    {
        $username = $request->post('username');

        $password = $request->post('password');


        $user = User::where('username',$username)->first();

        if (empty($user)) {
            return response()->json(['code' => 0,'auth'=>false, 'data' => 'Account Not Found']);
        }

        if (Hash::check($password, $user->password))
        {
                return response()->json(['code' => 1, 'auth'=>true, 'data' => $user]);
        }
        else{
            return response()->json(['code'=>3,'auth'=>false,'data' =>'Invalid Credentials']);
        }

    }

    public function destroy($id)
    {
        $purge = Client::query()->find($id);
        try{

            $purge->delete();

        }catch (\Exception $ex)
        {
            return response()->json(['code' => 0, 'msg' => $ex->getMessage()]);
        }

        return response()->json(['code' => 200, 'msg' => 'Client successfully deleted']);

    }

    public function qrScan(Request $request,$id)
    {
        $client = Client::query()->find($id);
        $status= $client->status;

        if ($client==null){
            $string="
            
Name: NULL 
Email: NULL 
Phone Number: NULL 
Nationality: NULL 
Package: NULL 
Status: Invalid code";
            return $string;
        }
        elseif ($status=='Verified'){
            $fullName=$client->first_name .' '.$client->last_name;
            $email=$client->email;
            $phone=$client->phone_number;
            $nationality=$client->nationality;
            $package=$client->package;

            $string="
            
Name: $fullName 
Email: $email 
Phone Number: $phone 
Nationality: $nationality 
Package: $package 
Status: Already verified";

            return $string;
        }
        else{
            $fullName=$client->first_name .' '.$client->last_name;
            $email=$client->email;
            $phone=$client->phone_number;
            $nationality=$client->nationality;
            $package=$client->package;

            $string="
            
Name: $fullName 
Email: $email 
Phone Number: $phone 
Nationality: $nationality 
Package: $package 
Status: Verified";

            $client->status='Verified';
            $client->save();
            return $string;
        }
    }




    public function code(Request $request)
    {
        $code = $request->user_code;

        if(empty($code))
            return response()->json(['code'=>204,'msg'=>'Parameter code is required']);

          $insert = Voters::query()->where('msisdn',$code)->first();

        if(!empty($insert)):
              $insert->update(['verified'=>true]);
             return response()->json(['code'=>'200','msg'=>'Validation successful']);
        else:
            return response()->json(['code'=>'404','msg'=>'Invalid code, user not found']);
        endif;

    }


    public function check(Request $request)
    {
        $code = $request->user_code;

        if(empty($code))
            return response()->json(['code'=>204,'msg'=>'Parameter code is required']);

        $exist = Voters::query()->where('msisdn',$code)->first();

        if(!empty($exist)):
            return response()->json(['code'=>'200','msg'=>'User Exists']);
        else:
            return response()->json(['code'=>'404','msg'=>'Invalid code, user not found']);
        endif;

    }

}
