<?php
namespace OfficeBrain\Bundle\EmailBundle\Manager\AbstractManager;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\Common\Collections\ArrayCollection;

/**
 *
 * @author OfficeBrain 4795 <info@officebrain.com>
 *
 * Description: Abstract class of email
 *
 */
abstract class EmailServiceAbstract
{
    protected $container;
    protected $em;
    protected $dm;
    protected $session;
    protected $request;
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $userId;
    protected $cultureId;
    protected $cultureCode;
    protected $countryId;
    protected $languageId;
    protected $countryCode;
    protected $languageCode;
    protected $createdUid;
    protected $updatedUid;
    protected $sessionInstanceKey;
    protected $translator;
    protected $to;
    protected $from;
    protected $cc;
    protected $bcc;
    protected $replyTo;
    protected $messageInstance;
    protected $subject;
    protected $contentType;
    protected $attachment;
    
    
    
    protected $emailQueueRepo;
    

    /**
     * @author Employee Id: 4795
     * OB eCommerce Product -
     * Initialize the required variable on class load
     */
    public function __construct($container, $request, $countries, $languages)
    {
        $this->container          = $container;
        $this->em                 = $container->get('doctrine.orm.entity_manager');
        $this->request            = $request;
        $this->dm                 = $container->get('doctrine_mongodb.odm.document_manager');
        
        $masterRequest = $container->get('request_stack')->getMasterRequest();
        if($masterRequest) {
        	$this->projectSetting = $masterRequest->get('project_setting');
        }
        else {
        	$this->projectSetting = $this->request->get('project_setting');
        }
        unset($masterRequest);
        
        $this->instanceId         = $this->projectSetting['instance_id'];
        $this->instanceType       = $this->projectSetting['instance_type'];
        $this->userId             = $this->projectSetting['user_id'];
        $this->cultureId          = $this->projectSetting['culture_id'];
        $this->cultureCode        = $this->projectSetting['culture_code'];
        $this->createdUid         = $this->userId;
        $this->updatedUid         = $this->userId;
        $this->sessionInstanceKey = $this->instanceId.'-'.$this->cultureId;
        $this->session            = $container->get('session');
        $this->translator         = $container->get('translator');
                
        $this->to                  = new ArrayCollection();
        $this->cc                  = new ArrayCollection();
        $this->bcc                 = new ArrayCollection();
        $this->replyTo             = new ArrayCollection();
        $this->contentType          = "text/html";
        $this->attachment          = array();
        $this->getMessageInstance();
        $this->messageInstance->setContentType($this->contentType);
        
        $this->culture = $this->request->get('culture', false);
        $this->countries = $countries;
        $this->languages = $languages;
        
        $this->getCultureValue();
        
        $this->emailQueueRepo     = $this->dm->getRepository('OfficeBrainEmailBundle:EmailQueue');
        $this->emailQueueRepo->setProjectSetting($this->projectSetting);
        
      
    }
    
    public function __destruct()
    {
        unset($this->container);
        unset($this->em);
        unset($this->dm);
        unset($this->request);
        unset($this->sessionManager);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->createdUid);
        unset($this->upatedUid);
        unset($this->sessionInstanceKey);
        unset($this->session);
        unset($this->translator);
        
        unset($this->emailQueueRepo);
    }
    
    /**
     * @author Employee Id: 4795
     * Description : Seprate culture with language and country.
     *
     * @param string $culture
     *
     * @return array
     */
    public function getCultureValue()
    {
        $cultures = array();
    
        if ($this->culture && $this->culture != "" && (int)$this->culture == 0) {
            list($this->languageCode, $this->countryCode) = explode("_", $this->culture);
        }
        return $cultures;
    }
    
    /**
     * @author Employee Id: 4795
     * Description :
     *
     * @param 1 : string $cultureCode
     *
     * @return int
     * */
    public function getCountryIdByCode($cultureCode)
    {
        $countries = $this->countries;
        foreach ($countries as $key => $country) {
            if ($cultureCode == $key) {
                return $country['id'];
            }
        }
    }
    
    /**
     * @author Employee Id: 4795
     * Description :
     *
     * @param 1 : string $languageCode
     *
     * @return int
     * */
    public function getLanguageIdByCode($languageCode)
    {
        $languages = $this->languages;
        foreach ($languages as $key => $lan) {
            if ($languageCode == $key) {
                return $lan['id'];
            }
        }
    }
    
    /**
     * @author Employee Id: 4795
     * Description : Get culture code by cultureId
     *
     * @param $cultureId
     * */
    public function getCultureCodeById($cultureId)
    {
    }
    
    /**
     * add email address name in to.
     *
     * @param string $emailAddress email address
     * @param string $name         name
     *
     * @return void
     */
    public function addTo($emailAddress, $name="")
    {
        $this->to[$emailAddress] = $name;
    }
    
    /**
     * add email address name in cc
     *
     * @param string $emailAddress email address
     * @param string $name         name
     *
     * @return void
     */
    public function addCc($emailAddress, $name="")
    {
        $this->cc[$emailAddress] = $name;
    }
    
    /**
     * add email address name in bcc
     *
     * @param string $emailAddress email address
     * @param string $name         name
     *
     * @return void
     */
    public function addBcc($emailAddress, $name="")
    {
        $this->bcc[$emailAddress] = $name;
    }
    
    /**
     * add email address name in replyTo
     *
     * @param string $emailAddress email address
     * @param string $name         name
     *
     * @return void
     */
    public function addReplyTo($emailAddress, $name="")
    {
        $this->replyTo[$emailAddress] = $name;
    }
    
    /**
     * add email address name in from.
     *
     * @param string $emailAddress email address
     * @param string $name         name
     *
     * @return void
     */
    public function addFrom($fromEmail, $fromName="")
    {
        $this->getMessageInstance();
        $this->messageInstance->setFrom($fromEmail, $fromName);
        $this->messageInstance->setContentType($this->contentType);
    }
    
    /**
     * set email subject
     *
     * @param string $subject subject
     *
     * @return void
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;
    }
    
    /**
     * attachment path
     * @param string $attachmentPath
     *
     * @return void
     */
    public function addAttachment($attachmentPath,$inline=false)
    {
        if($inline==true){
            $fileName = end(explode("/",$attachmentPath));
            $attachment = \Swift_Attachment::fromPath($attachmentPath)->setDisposition('inline');
            $attachment->getHeaders()->addTextHeader('Content-ID', '<'.$fileName.'>');
            $attachment->getHeaders()->addTextHeader('X-Attachment-Id', $fileName);
            $this->attachment[]=$attachment;
                        
        }else{
        //for sendgrid mail option only path required.
            $this->attachment[]= \Swift_Attachment::fromPath($attachmentPath);
        }
    }
    
    
    public function clearAddress()
    {
        $this->to  = new ArrayCollection();
        $this->cc  = new ArrayCollection();
        $this->bcc = new ArrayCollection();
        
        $this->messageInstance = null;
    }
    
    /**
     * get Swift message instance.
     *
     * @return \Swift_Message()
     */
    public function getMessageInstance()
    {
        if ($this->messageInstance == null) {
            $this->messageInstance = new \Swift_Message();
        }
    
        return $this->messageInstance;
    }
    
    public function getMailerService()
    {
        return $this->container->get('mailer');
    }
    
    public function getQueuedEmailList()
    {
    }
    
    public function resendEmail($id)
    {
    }
    
    public function readEmail($id)
    {
    }
        
    /** Common methods **/
  
    
    public function commitDocument($document)
    {
        $this->dm->persist($document);
        $this->dm->flush();
        return $document;
    }
    
    /** End of common methods **/
}
