<?php

namespace App\Http\Controllers;

use App\Models\WebProperties;
use App\Http\Controllers\CommonController;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class WebPropertiesController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
        $this->common_class_obj = new CommonController();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $id = 1;
        $data = WebProperties::findorFail(1);
        return view('webProperties.edit', compact('data','id'));
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\WebProperties  $webProperties
     * @return \Illuminate\Http\Response
     */
    public function show(WebProperties $webProperties)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\WebProperties  $webProperties
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, WebProperties $webProperties)
    {
        //
    }

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

    public function updateData(Request $request,$id)
    {
        
        $data = WebProperties::findorFail($id);

        $existing_logo = $data->logo;
        $existing_owner_signature_image = $data->owner_signature_image;

        if($request->email != null && !empty($request->email)){
            if (!filter_var($request->email, FILTER_VALIDATE_EMAIL)) {
                return redirect()->route('website-info.index')->with('error',  'E-mail is not valid');
              } else {
                $data->email = $request->email;
              }
        }

        if($request->support_email != null && !empty($request->support_email)){
            if (!filter_var($request->support_email, FILTER_VALIDATE_EMAIL)) {
                return redirect()->route('website-info.index')->with('error',  'E-mail is not valid');
              } else {
                $data->support_email = $request->support_email;
              }
        }

        if($request->business_email != null && !empty($request->business_email)){
            if (!filter_var($request->business_email, FILTER_VALIDATE_EMAIL)) {
                return redirect()->route('website-info.index')->with('error',  'E-mail is not valid');
              } else {
                $data->business_email = $request->business_email;
              }
        }
        
        $data->company_name = $request->company_name;
        $data->slogan = $request->slogan;
        $data->phone_no = $request->phone_no;
        $data->fb_link = $request->fb_link;
        $data->twitter_link = $request->twitter_link;
        $data->youtube_link = $request->youtube_link;
        $data->linkedin_link = $request->linkedin_link;
        $data->address = $request->address;
        $data->footer_text = $request->footer_text;
        $data->count_one = $request->count_one;
        $data->count_one_text = $request->count_one_text;
        $data->count_two = $request->count_two;
        $data->count_two_text = $request->count_two_text;

        $data->owner_designation = $request->owner_designation;
        $data->owner_message = $request->ck_editor_owner_message;
        $data->owner_name = $request->owner_name;

        if($request->youtube_link){
            // check valid youtube url
            $checkVideo = $this->common_class_obj->youtubeCheck($request->youtube_link);

            if($checkVideo['success'] == false){
                return redirect()->route('website-info.index')->with('error',  'Youtube video url is not valid');
            } else if($checkVideo['success'] == true){
                $data->youtube_video_key = $checkVideo['value'];
            }
        }
        
        if ($request->hasFile('logo')) { // if logo is not empty.

            $request->validate([
                'logo' => 'required|image|mimes:jpeg,png,jpg',
            ]);

            $slug = Str::slug($request->logo);

            $currentDate = Carbon::now()->toDateString();
            $logoImageName = $slug . '-' . $currentDate . '-' . uniqid() . '.' .$request->logo->getClientOriginalExtension();

            if (!file_exists('images/web-properties')) {
                mkdir('images/web-properties', 0777, true);
            }
            $request->logo->move('images/web-properties', $logoImageName);

            /// Delete Existing Image
            // if($existing_image != null){
            //     if (file_exists('images/websiteInfo/' . $existing_image)) {
            //         unlink('images/websiteInfo/' . $existing_image);
            //     }
            // }
        
            $data->logo = $logoImageName;
        
        }

        if ($request->hasFile('owner_signature_image')) { // if owner_signature_image is not empty.

            $request->validate([
                'owner_signature_image' => 'required|image|mimes:jpeg,png,jpg',
            ]);

            $slug = Str::slug($request->owner_signature_image);

            $currentDate = Carbon::now()->toDateString();
            $logoImageName = $slug . '-' . $currentDate . '-' . uniqid() . '.' .$request->owner_signature_image->getClientOriginalExtension();

            if (!file_exists('images/web-properties')) {
                mkdir('images/web-properties', 0777, true);
            }
            $request->owner_signature_image->move('images/web-properties', $logoImageName);

            // / Delete Existing Image
            if($existing_owner_signature_image != null){
                if (file_exists('images/websiteInfo/' . $existing_owner_signature_image)) {
                    unlink('images/websiteInfo/' . $existing_owner_signature_image);
                }
            }
        
            $data->owner_signature_image = $logoImageName;
        
        }


        $data->updated_by = Auth::user()->id;


        $data->save();

        if ($data) {
            return redirect()->route('website-info.index')->with('success', 'Information Has Been updated Successfully');
        }
    }
}