<?php namespace Visiosoft\ProfileModule\Profile\Password;

use Anomaly\Streams\Platform\Message\MessageBag;
use Anomaly\UsersModule\User\User;
use Anomaly\UsersModule\User\UserPassword;
use Illuminate\Support\Facades\Auth;

class PasswordFormHandler
{
    public function handle(
        PasswordFormBuilder $builder,
        MessageBag $messages,
        UserPassword $userPassword

    )
    {
        $errorList = array();

        if (!$builder->canSave()) {
            return;
        }

        if ($builder->getPostValue('new_password') != $builder->getPostValue('re_new_password')) {
            $messages->error(trans('visiosoft.module.profile::message.password_do_not_match'));
            return redirect()->back();
        }
        $validator = $userPassword->validate($builder->getPostValue('new_password'));
        foreach ($validator->errors()->all() as $error) {
            $errorList[] = $error;
        }

        if (count($errorList) != 0) {
            $messages->error($errorList);
            return redirect()->back();
        }

        $user = User::query()->find(Auth::id());
        $user->setAttribute('password', $builder->getPostValue('new_password'));
        $user->save($user->toArray());
        $messages->success(trans('visiosoft.module.profile::message.your_password_changed'));
    }
}
