<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class ChangePasswordController extends Controller
{

    public function subDealerView()
    {

        $dealer = Auth::guard('sub-dealer')->user();

        return view('pages.sub-dealer.change-password')->with(
            compact('dealer')
        );
    }

    public function subDealerStore(Request $request)
    {

        $this->validate($request, [
            'old_password' => 'required|string:min:8',
            'new_password' => 'required|string:min:8',
            'confirm_password' => 'required|string:min:8'
        ]);

        $oldPassword = $request->old_password;
        $newPassword = $request->new_password;
        $confirmPassword = $request->confirm_password;

        $user = auth()->guard('sub-dealer')->user();


        if ($newPassword != $confirmPassword) {
            return response()->json([
                'status' => 'error',
                'message' => 'New Password and Confirm Password does not match'
            ], 202);
        } elseif (!Hash::check($oldPassword, $user->password)) {
            return response()->json([
                'status' => 'error',
                'message' => 'Old Password does not match'
            ], 202);

        } else {
            $user->password = Hash::make($newPassword);
            $user->save();
            return response()->json([
                'status' => 'success',
                'message' => 'Password Changed Successfully'
            ], 200);
        }

    }


    public function masterDealerView()
    {

        $dealer = Auth::guard('master-dealer')->user();

        return view('pages.master-dealer.change-password')->with(
            compact('dealer')
        );
    }

    public function masterDealerStore(Request $request)
    {

        $this->validate($request, [
            'old_password' => 'required|string:min:8',
            'new_password' => 'required|string:min:8',
            'confirm_password' => 'required|string:min:8'
        ]);

        $oldPassword = $request->old_password;
        $newPassword = $request->new_password;
        $confirmPassword = $request->confirm_password;

        $user = auth()->guard('master-dealer')->user();


        if ($newPassword != $confirmPassword) {
            return response()->json([
                'status' => 'error',
                'message' => 'New Password and Confirm Password does not match'
            ], 202);
        } elseif (!Hash::check($oldPassword, $user->password)) {
            return response()->json([
                'status' => 'error',
                'message' => 'Old Password does not match'
            ], 202);

        } else {
            $user->password = Hash::make($newPassword);
            $user->save();
            return response()->json([
                'status' => 'success',
                'message' => 'Password Changed Successfully'
            ], 200);
        }

    }

    public function trialAccountView()
    {

        $dealer = Auth::guard('trial-account')->user();

        return view('pages.trial-account.change-password')->with(
            compact('dealer')
        );
    }

    public function trialAccountStore(Request $request)
    {

        $this->validate($request, [
            'old_password' => 'required|string:min:8',
            'new_password' => 'required|string:min:8',
            'confirm_password' => 'required|string:min:8'
        ]);

        $oldPassword = $request->old_password;
        $newPassword = $request->new_password;
        $confirmPassword = $request->confirm_password;

        $user = auth()->guard('trial-account')->user();


        if ($newPassword != $confirmPassword) {
            return response()->json([
                'status' => 'error',
                'message' => 'New Password and Confirm Password does not match'
            ], 202);
        } elseif (!Hash::check($oldPassword, $user->password)) {
            return response()->json([
                'status' => 'error',
                'message' => 'Old Password does not match'
            ], 202);

        } else {
            $user->password = Hash::make($newPassword);
            $user->raw_password = $newPassword;
            $user->save();
            return response()->json([
                'status' => 'success',
                'message' => 'Password Changed Successfully'
            ], 200);
        }

    }

    public function trialAccountChangePIN()
    {
        $dealer = Auth::guard('trial-account')->user();

        return view('pages.trial-account.change-pin')->with(
            compact('dealer')
        );
    }

    public function trialAccountChangePINStore(Request $request)
    {
        $this->validate($request, [
            'old_pin' => 'required|string:min:6,max:6',
            'new_pin' => 'required|string:min:6,max:6',
            'confirm_pin' => 'required|string:min:6,max:6'
        ]);

        $oldPIN = $request->old_pin;
        $newPIN = $request->new_pin;
        $confirmPIN = $request->confirm_pin;

        $user = auth()->guard('trial-account')->user();

        $ezypos = $user->app;

        if ($newPIN != $confirmPIN) {
            return response()->json([
                'status' => 'error',
                'message' => 'New PIN and Confirm PIN does not match'
            ], 202);
        } elseif ($oldPIN !== $ezypos->pin) {
            return response()->json([
                'status' => 'error',
                'message' => 'Old PIN does not match'
            ], 202);

        } else {
            $ezypos->pin = $newPIN;
            $ezypos->save();
            return response()->json([
                'status' => 'success',
                'message' => 'PIN Changed Successfully'
            ], 200);
        }
    }



    public function paidAccountView()
    {

        $dealer = Auth::guard('paid-account')->user();

        return view('pages.paid-account.change-password')->with(
            compact('dealer')
        );
    }

    public function paidAccountStore(Request $request)
    {

        $this->validate($request, [
            'old_password' => 'required|string:min:8',
            'new_password' => 'required|string:min:8',
            'confirm_password' => 'required|string:min:8'
        ]);

        $oldPassword = $request->old_password;
        $newPassword = $request->new_password;
        $confirmPassword = $request->confirm_password;

        $user = auth()->guard('paid-account')->user();


        if ($newPassword != $confirmPassword) {
            return response()->json([
                'status' => 'error',
                'message' => 'New Password and Confirm Password does not match'
            ], 202);
        } elseif (!Hash::check($oldPassword, $user->password)) {
            return response()->json([
                'status' => 'error',
                'message' => 'Old Password does not match'
            ], 202);

        } else {
            $user->password = Hash::make($newPassword);
            $user->raw_password = $newPassword;
            $user->save();
            return response()->json([
                'status' => 'success',
                'message' => 'Password Changed Successfully'
            ], 200);
        }

    }

    public function paidAccountChangePIN()
    {
        $dealer = Auth::guard('paid-account')->user();

        return view('pages.paid-account.change-pin')->with(
            compact('dealer')
        );
    }

    public function paidAccountChangePINStore(Request $request)
    {
        $this->validate($request, [
            'old_pin' => 'required|string:min:6,max:6',
            'new_pin' => 'required|string:min:6,max:6',
            'confirm_pin' => 'required|string:min:6,max:6'
        ]);

        $oldPIN = $request->old_pin;
        $newPIN = $request->new_pin;
        $confirmPIN = $request->confirm_pin;

        $user = auth()->guard('paid-account')->user();

        $ezypos = $user->app;

        if ($newPIN != $confirmPIN) {
            return response()->json([
                'status' => 'error',
                'message' => 'New PIN and Confirm PIN does not match'
            ], 202);
        } elseif ($oldPIN !== $ezypos->pin) {
            return response()->json([
                'status' => 'error',
                'message' => 'Old PIN does not match'
            ], 202);

        } else {
            $ezypos->pin = $newPIN;
            $ezypos->save();
            return response()->json([
                'status' => 'success',
                'message' => 'PIN Changed Successfully'
            ], 200);
        }
    }


}
