<?php

namespace OfficeBrain\Bundle\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use OfficeBrain\Bundle\CoreBundle\CustomeHandler as FormErrorManager;
use OfficeBrain\Bundle\UserBundle\Entity\User;
use OfficeBrain\Bundle\UserBundle\Form\UserType;
use OfficeBrain\Bundle\UserBundle\Form\ForgotPasswordType;
use OfficeBrain\Bundle\UserBundle\Form\ResetPasswordType;
use OfficeBrain\Bundle\UserBundle\Form\ChangePasswordType;

class LoginAdminController extends Controller
{
    /**
     *
     * @author Employee Id: 4273
     *
     *         Create various objects required action in this class
     *
     * @return NULL
     */
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->translator = $this->get('translator');
        $this->session = $this->request->getSession();

        $this->projectSetting = $this->request->get('project_setting');
        $this->loginConfigrations = $this->projectSetting['configuration_rule']['user']['login'];
        //echo '<pre>'; print_r($this->projectSetting['configuration_rule']['user']); die;
        $this->forgotPasswordConfigrations = $this->projectSetting['configuration_rule']['user']['forgot_password'];
        $this->userId = $this->projectSetting['user_id'];
        $this->userManager = $this->get('office_brain_user_manager');
        $this->loginManager = $this->get('office_brain_user_login_manager');
        $this->formErrorManager = new FormErrorManager\JsonError();
    }


    /**
     * @author Employee Id: 4273
     * OB eCommerce Product - Login
     * Function created to show login form
     *
     * @Route("/admin/login",
     *  name="office_brain_userbundle_login_admin_login_without_locale",
     *  options={"expose"=true}
     * )
     * @Route("/admin/{_locale}/login",
     * 	name="office_brain_userbundle_login_admin_login",
     *  options={"expose"=true}
     * )
     * @Method({"GET"})
     * @Template("OfficeBrainUserBundle:LoginAdmin:login.html.twig")
     */

    public function loginAction()
    {
        $this->initAction();
        if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) { //IS_AUTHENTICATED_FULLY
        	return $this->redirect($this->generateUrl('office_brain_page_admin_dashboard'));
        }
        $data = array();
        
        if ($this->request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $this->request->attributes->get(
                    SecurityContext::AUTHENTICATION_ERROR
            );
        } else {
            $error = $this->session->get(SecurityContext::AUTHENTICATION_ERROR);
            $this->session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }

        $data['username'] = $this->session->get(SecurityContext::LAST_USERNAME);
        $data['error'] = $error;
        if (isset($_COOKIE['adminuserdata'])) {
            $data['user_cookie'] = unserialize($_COOKIE['adminuserdata']);
        }
      //  $data['csrf_token'] = $this->container->get('form.csrf_provider')->generateCsrfToken('authenticate');
        return $data;
    }




    /**
     * @author Employee Id: 4273
     * OB eCommerce Product - Login
     * Function created to logout
     *
     * @Route("admin/{_locale}/logout",
     * 	name="office_brain_userbundle_login_admin_logout",
     *  options={"expose"=true}
     * )
     *
     * @Template()
     */

    public function logoutAction()
    {
    }
}
