<?php

//MNM - overwrite to add translation

namespace MNM\Spitzner\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Registry;

class Addtocart extends \Magebees\Ajaxaddtocart\Observer\Addtocart
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $config=$this->ajaxPopup->getConfig();
        if ($config['enable']==1) {
            if ($this->httpRequest->isAjax()) {
                $productId=$observer->getProduct()->getId();
                $product=$this->catalogModel->load($productId);
				$this->_coreRegistry->register('product',$product);
                $url=$this->catalogHelper->getImageUrl($product);
                $content=$this->ajaxPopup->getConfirmContent();
                $dialogConfig=$this->ajaxPopup->getDialogConfig();
//MNM           $totalQuantity =$this->cartModel->getQuote()->getItemsQty();
//MNM           $subTotal = $this->cartModel->getQuote()->getSubtotal();
//MNM           $formated_total=$this->checkoutHelper->formatPrice($subTotal);
                $resultPage= $this->pageFactory->create();
                $layoutblk = $resultPage->addHandle('ajaxcheckout_cart_index')->getLayout();             
                $productblk = $resultPage->addHandle('product_section')->getLayout();
                //$layoutblk = $resultPage->addHandle('checkout_cart_index')->getLayout();
                
//MNM - added translate function
                $html="<p id=\"success-message\">".sprintf(__("%s added to your cart successfully"), $product->getName()).".</p>
                <div class=\"timer active-color\" id=\"timer\">3</div>";
//MNM
                if ($dialogConfig['productimage']==1) {
                    $html.="<img src='".$url."' alt='".$product->getName()."' class='pImage' />";
                }
//MNM           $html.="<div class='totalRow'>There are <b>".$totalQuantity."</b> item in your cart.<br />Cart Subtotal: <b>".$formated_total."</b></div>".$content;
                $html .= $content;
             	if ($dialogConfig['related']==1) {
					 $related_content= $productblk->getBlock('catalog.product.related')->toHtml();
					 $html.=$related_content;
				 }
				 if ($dialogConfig['upsell']==1) {
					 $upsell_content= $productblk->getBlock('product.info.upsell')->toHtml();
					  $html.=$upsell_content;
				 }
				 if ($dialogConfig['crosssell']==1) {
					 $crosssell_content= $productblk->getBlock('addtocart_crosssell')->toHtml();
					  $html.=$crosssell_content;
				 }		
				$_response=$this->productContent;
				$_response->setPopupContent($html);
				$referUrl = $this->redirect->getRefererUrl();
				  if (strpos($referUrl, 'checkout')!== false) {
					 if ($layoutblk->getBlock('checkout.cart')) {
                   $cart_content= $layoutblk->getBlock('checkout.cart')->toHtml();
                    $_response->setCartContent($cart_content);               
                	}                 
				  }
				$_response->addBlockContent($_response);
                $_response->send();
            }
        }
    }
}
