<?php
namespace OfficeBrain\Bundle\UserBundle\Manager\Extended;

use OfficeBrain\Bundle\UserBundle\Manager\AbstractManager\SignUpAbstract;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use OfficeBrain\Bundle\UserBundle\Entity\User;
use OfficeBrain\Bundle\UserBundle\Form\UserType;
use OfficeBrain\Bundle\UserBundle\Service\CustomPasswordEncoder;
use Symfony\Component\Routing\Router;

/**
 *
 * @author Employee Id: 4273
 * OB eCommerce Product - SignUp
 * SignUpExtended Class Add to override the logic for sign up process
 */

class SignUpExtended extends SignUpAbstract
{
    private $entityManager;
    protected $session;
    protected $request;
    protected $emailService;


    /**
     * @author Employee Id: 4273
     * Initialize the required variable on class load
     *
    */
    public function __construct($entityManager, $request, $session, $emailService, $router)
    {
        parent::__construct($entityManager, $request, $session, $emailService, $router);
        $this->entityManager = $entityManager;
        $this->session = $session;
        $this->request = $request;
        $this->emailService = $emailService;
        $this->router = $router;
        $this->projectSetting = $this->request->get('project_setting');
        $this->projectUserSetting = $this->request->get('project_user_setting');
        $this->signUpConfigrations = $this->projectSetting['configuration_rule']['user']['sign_up'];

        $this->entityManager->getRepository('OfficeBrainUserBundle:SignUpForm')->setProjectSetting($this->projectSetting);
        $this->entityManager->getRepository('OfficeBrainUserBundle:User')->setProjectSetting($this->projectSetting);
        $this->entityManager->getRepository('OfficeBrainUserBundle:User')->setProjectUserSetting($this->projectUserSetting);
    }

    /**
     * @author Employee Id: 4273
     * OB eCommerce Product - sendEmail
     * Function created for send email
     */
    public function sendEmail($form, $subject)
    {
        $this->signUpApproveConfigrations = $this->entityManager->getRepository('OfficeBrainUserBundle:SignUpForm')->getSignUpForm($form->getData()->getUserType());

        if ($this->signUpApproveConfigrations['approval_configuration']['is_auto_approve']->option->approval_email==1) {
            $this->sendAprrovalEmail($form->getData(), $subject);
        }
        if ($this->signUpConfigrations['option']['welcome_email']==1) {
            $this->sendWelcomeEmail($form->getData(), $subject);
        }
    }
    
    /**
     * @author Employee Id: 5633
     * OB eCommerce Product - sendEmail
     * Function created for send email
     */
    public function sendAdminEmail($form)
    {
    	$user = $form->getData();
    	$eventKey = 'register_user_admin_acknowledge'; // This is required field. For each email we have to set event with unique event key.
    	$to = ""; // This is required field. Pass email address in array.
    	$params = array( //Please provide dynamic parameters in this array where placeholder must be used as a key of array.
    			'firstname' => $user->getFirstName(),
    			'lastname' => $user->getLastName(),
    			'email' => $user->getEmail(),
    	);
    	$languageId = $this->projectSetting['culture_id'];//This is optional field but when you call this function from admin section you have to pass languageId.
    	$from =  ''; //This is optional field.
    	$subject = ""; //This is optional field.
    	$attachment = array(); //This is optional field. If you have any attachment please provide path in this parameter.
    	$cc = array(); // This is optional field.
    	$bcc = array(); //This is optional field.
    	
    	$this->emailService->sendMail($eventKey, $to, $params, $languageId, $from, $subject, $attachment, $cc, $bcc);
    	
    	return true;
    }

    /**
     * @author Employee Id: 4273
     * OB eCommerce Product - sendAprrovalEmail
     * Function created for send Aprroval Email
     */
    public function sendAprrovalEmail($user, $subject)
    {
        $eventKey = 'approval_email'; // This is required field. For each email we have to set event with unique event key.
        $to = $user->getEmail(); // This is required field. Pass email address in array.
        $password = $this->entityManager->getRepository('OfficeBrainUserBundle:User')->getUserPassword($user->getPassword(),$user->getSalt());
        $params = array( //Please provide dynamic parameters in this array where placeholder must be used as a key of array.
                'firstname' => $user->getFirstName(),
                'lastname' => $user->getLastName(),
                'email' => $user->getEmail(),
                'username' => $user->getLoginUsername(),
        		'password' => $password,
                'link' => rtrim($this->projectSetting['base_url'], "/").$this->router->generate('office_brain_userbundle_sign_up_approve_register', array('confirmation_token' => $user->getConfirmationToken())) ,
        );
        $languageId = $this->projectSetting['culture_id'];//This is optional field but when you call this function from admin section you have to pass languageId.
        $from =  ''; //This is optional field.
        $subject = $subject; //This is optional field.
        $attachment = array(); //This is optional field. If you have any attachment please provide path in this parameter.
        $cc = array(); // This is optional field.
        $bcc = array(); //This is optional field.

        $this->emailService->sendMail($eventKey, $to, $params, $languageId, $from, $subject, $attachment, $cc, $bcc);

        return true;///admin/en_us/email/release-emails
    }

    /**
     * @author Employee Id: 4273
     * OB eCommerce Product - sendEmail
     * Function created for send Welcome Email
     */
    public function sendWelcomeEmail($user, $subject)
    {
        $eventKey = 'welcome_email'; // This is required field. For each email we have to set event with unique event key.
        $to = $user->getEmail(); // This is required field. Pass email address in array.
        $password = $this->entityManager->getRepository('OfficeBrainUserBundle:User')->getUserPassword($user->getPassword(),$user->getSalt());
        $params = array( //Please provide dynamic parameters in this array where placeholder must be used as a key of array.
                'firstname' => $user->getFirstName(),
                'lastname' => $user->getLastName(),
                'email' => $user->getEmail(),
                'username' => $user->getLoginUsername(),
        		'password' => $password,
        		'userPhoto' => $user->getPhoto(),
        );
        $languageId = $this->projectSetting['culture_id'];//This is optional field but when you call this function from admin section you have to pass languageId.
        $from =  ''; //This is optional field.
        $subject = $subject; //This is optional field.
        $attachment = array(); //This is optional field. If you have any attachment please provide path in this parameter.
        $cc = array(); // This is optional field.
        $bcc = array(); //This is optional field.

        $this->emailService->sendMail($eventKey, $to, $params, $languageId, $from, $subject, $attachment, $cc, $bcc);

        return true;
    }
    
    /**
     * @author Employee Id: 4273
     * OB eCommerce Product - getDistributors
     * Function created to get Distributors List
     */
    public function getDistributors()
    {
        return $this->entityManager->getRepository('OfficeBrainUserBundle:User')->getAllDistributors();
    }
}
