<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\WelcomeMessage;
use Validator;
use Image;

class WelcomeMessageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $messages = WelcomeMessage::latest()->limit(1)->get();
        return view('backend.welcome_message',compact(['messages']));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //validating data
        $rules = [
            'message_title' => 'required',
            'description' => 'required',
            'name' => 'required',
        ];

        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['errors'=>"Crosscheck if You have any Missing Fields."]);
        }

        //image validation
        if($image = $request->file('photo')){
            $image_rules = [
                'photo' => 'required|mimes:png,jpg,jpeg,tiff,gif|max:2048',
            ];

            $error_image = Validator::make($request->all(),$image_rules);

            if($error_image->fails()){
                return response()->json(['errors'=>"The System Accept PNG,JPEG,JPG,GIF,TIFF File Format Not More than 2MB."]);
            }
            else{
                $photo_image = Image::make($image);
                $photo_destination = public_path('/uploads/photos/');
                $photo_file = time().uniqid().".".$image->getClientOriginalExtension();//to save in db
                $resized = $photo_image->resize(250,250);
                $resized->save($photo_destination.$photo_file);
            }
        }

        //data to save
        $data_save = [
            'message_title' => $request->message_title,
            'name' => $request->name,
            'description' => $request->description,
            'publish_status' => $request->publish_status,
            'image_attachment' => $photo_file,
        ];

        $data = WelcomeMessage::create($data_save);

        if($data){
            return response()->json(['success'=>"Welcome Message Set Successfully"]);
        }
        else{
            return response()->json(['failed'=>"Unable to Set Welcome Message."]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, WelcomeMessage $model)
    {
        //
        $id = $request->hidden_data;
        
        //validating data
        $rules = [
            'message_title' => 'required',
            'description' => 'required',
            'name' => 'required',
        ];

        $error = Validator::make($request->all(),$rules);

        if($error->fails()){
            return response()->json(['errors'=>"Missing Fields"]);
        }

        //getting old photo
        $data_old = WelcomeMessage::whereId($id)->get();

        if($data_old->isEmpty()){
            return response()->json(['errors'=>["Data is Not Available"]]);
        }
        else{
            foreach($data_old as $row){
                $image_files = $row->image_attachment;
            }
        }

        //checking if we having image or not
        if($image = $request->file('photo')){
            $image_rules = [
                'photo' => 'required|mimes:png,jpg,jpeg,tiff,gif|max:2048',
            ];

            $error_image = Validator::make($request->all(),$image_rules);

            if($error_image->fails()){
                return response()->json(['errors'=>"The System Accept PNG,JPEG,JPG,GIF,TIFF File Format Not More than 2MB."]);
            }
            else{
                
                 //deleting old photo since validation pass
                 if(\File::exists(public_path("uploads/photos/$image_files"))){
                    \File::delete(public_path("uploads/photos/$image_files"));
                }

                $photo_image = Image::make($image);
                $photo_destination = public_path('/uploads/photos/');
                $photo_file = time().uniqid().".".$image->getClientOriginalExtension();//to save in db
                $resized = $photo_image->resize(250,250);
                $resized->save($photo_destination.$photo_file);
            }
        }

        //data to update
        if($image == ""){
            $data_update = [
                'message_title' => $request->message_title,
                'name' => $request->name,
                'description' => $request->description,
                'publish_status' => $request->publish_status,
            ];
        }
        else{
            $data_update = [
                'message_title' => $request->message_title,
                'name' => $request->name,
                'description' => $request->description,
                'publish_status' => $request->publish_status,
                'image_attachment' => $photo_file,
            ];
        }
        
        $data_updated = WelcomeMessage::whereId($id)->update($data_update);

        if($data_updated){
            return response()->json(['success'=>"Message Updated Successfully"]);
        }
        else{
            return response()->json(['errors'=>"Crosscheck if You have any Missing Fields."]);
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
