<?php

namespace OfficeBrain\Bundle\UserBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use OfficeBrain\Bundle\UserBundle\Form\ForgotPasswordType;
use OfficeBrain\Bundle\UserBundle\Form\ChangePasswordType;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

/**
 * Mobiel API Browse Category.
 *
 * @author Employee Id: 410175
 *
 * Desciption: This is a Login controler
 *
 * @param null
 *
 * @return json Response
 *
 * @throws null
 *
 * @Route("/api/v1")
 */
class ApiLoginController extends LoginController
{
    /**
     * @author Employee Id: 5052
     *
     * Create various objects required action in this class
     */
    public function initAction()
    {
        parent::initAction();
        $this->request = $this->getRequest();
    }

    /*
     * @author Employee Id: 5053
     * OB eCommerce Product - Forgot Password
     * Function created to process forget Password for Mobile API
     *
     * @Route("/{_locale}/forgot-password",
     *  options={"expose"=true}
     * )
     *@ApiDoc(
     *      resource=true,
     *      section="User",
     *      description="To Submit Email for Forgot Password.",
     *      input = {
     *      	"class" = "\OfficeBrain\Bundle\UserBundle\Form\ForgotPasswordType"
     *      },
     *      statusCodes={
     *              1="Returned when success",
     *              403="Returned when the user is not authorized to see this link",
     *              0="Returned when form validation is failed",
     *      },
     *      requirements={
     *             {"name"="_locale", "dataType"="string", "required"=true, "default"="en_us"},
     *      }, 
     * )
     * @Method({"POST"})
     * /
    public function forgotPasswordCheckMobileApi()
    {
        $this->initAction();

        $errors = array();

        $form = $this->createForm(new ForgotPasswordType(), null, array(
                'csrf_protection' => false,
        ));

        $form->handleRequest($this->request);
        if ($form->isValid()) {
            $formData = $form->getData();
            $user = $this->userManager->getUserByEmail($formData ['email']);
            if ($user) {
                $isProcessDone = $this->loginManager->doForgotPasswordProcess($user, $this->translator->trans('user.forget.email_subject'));
                if ($isProcessDone) {
                    return array(
                            'status' => 'success',
                            'status_code' => 1,
                            'message' => $this->translator->trans('user.forget.success'),
                    );
                } else {
                    return array(
                            'status' => 'error',
                            'status_code' => 0,
                            'message' => $this->translator->trans('user.forget.exists'),
                    );
                }
            } else {
                $errors ['email'] = $this->translator->trans('user.forget.user_not_exists');
            }
        } else {
            $errors = $this->formErrorManager->prepareErrorArray($this->get('validator')->validate($form));
        }

        if (count($errors) <= 0) {
            foreach ($form as $child) {
                if (!$child->isValid()) {
                    $thiserror = $form->get($child->getName())->getErrors();
                    $errors [$child->getName().$key] = $thiserror [0]->getMessage();
                }
            }
        }
        $error = array_values($errors);
        $singleErrorMsg = $error [0];

        return array(
                'status' => 'error',
                'status_code' => 0,
                'message' => $singleErrorMsg,
        );
    }

    /**
     * @author Employee Id: 5052
     * OB eCommerce Product - Login
     * Function created to change Password
     *
     * @Route("/{_locale}/my-account/change-password",
     * 	name="office_brain_userbundle_login_change_password_check_api",
     *  options={"expose"=true}
     * )
     *@ApiDoc(
     *      resource=true,
     *      section="User",
     *      description="To Submit for change Password.",
     *      input = {
     *      	"class" = "\OfficeBrain\Bundle\UserBundle\Form\ChangePasswordType"
     *      },
     *      requirements={
     *             {"name"="access_token", "dataType"="string", "required"=true, "description"="valid access token"},
     *             {"name"="_locale", "dataType"="string", "required"=true, "default"="en_us"},
     *      },
     *      statusCodes={
     *              1="Returned when success",
     *              403="Returned when the user is not authorized to see this link",
     *              0="Returned when form validation is failed",
     *      }
     * )
     * @Method({"POST"})
     * /
    public function changePasswordCheckAction()
    {
        $this->initAction();

        if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
            throw new AccessDeniedException();
        }

        $user = $this->projectUserSetting ['user_data'];

        $form = $this->createForm(new ChangePasswordType($user), null, array(
                'csrf_protection' => false,
        ));

        $form->handleRequest($this->request);

        if ($form->isValid()) {
            $isProcessDone = $this->loginManager->doChangePasswordProcess($form->getData(), $user, $this->translator->trans('user.change_password.email_subject'));

            return array(
                    'status' => 'success',
                    'status_code' => 1,
                    'message' => $this->translator->trans('user.change_password.success'),
            );
        }

        $aryErrors = $this->formErrorManager->prepareErrorArray($this->get('validator')->validate($form));
        if (count($aryErrors) <= 0) {
            foreach ($form->getErrors(true) as $key => $error) {
                $aryErrors [] = $error->getMessage();
            }
        }
        $errors = array_values($aryErrors);
        $singleErrorMsg = $errors [0];

        return array(
                'status' => 'error',
                'status_code' => 0,
                'message' => $singleErrorMsg,
        );
    }*/
}
