<?php

/**
 * Landofcoder
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Landofcoder.com license that is
 * available through the world-wide-web at this URL:
 * https://landofcoder.com/terms
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category   Landofcoder
 * @package    Lof_Formbuilder
 * @copyright  Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
 * @license    https://landofcoder.com/terms
 */

namespace Lof\Formbuilder\Controller\Form;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\DataPersistorInterface;

class Post extends \Magento\Framework\App\Action\Action
{
    const FILE_TYPES = 'jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG,doc,DOC,docx,DOCX,pdf,PDF,zip,ZIP,tar,TAR,rar,RAR,tgz,TGZ,7zip,7ZIP,gz,GZ';

    /**
     * @var DataPersistorInterface
     */
    private $dataPersistor;

    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirectFactory;

    /**
     * @var \Lof\Formbuilder\Helper\Data
     */
    protected $helper;

    /**
     * @var \Magento\Framework\Controller\Result\ForwardFactory
     */
    protected $resultForwardFactory;

    /**
     * @var \Magento\Framework\Translate\Inline\StateInterface
     */
    protected $inlineTranslation;

    /**
     * @var \Lof\Formbuilder\Model\Form
     */
    protected $form;

    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $transportBuilder;

    /**
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface
     */
    protected $mediaDirectory;

    /**
     * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
     */
    protected $remoteAddress;

    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Magento\Framework\App\ResourceConnection
     */
    protected $resource;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var \Lof\Formbuilder\Model\MessageFactory
     */
    protected $_messageFactory;

    /**
     * @var \Lof\Formbuilder\Model\BlacklistFactory
     */
    protected $_blacklistFactory;

    /**
     * @var \Lof\Formbuilder\Helper\Barcode
     */
    protected $barcodeHelper;

    // TUN2U Fix, Creation of dynamic property is deprecated since php 8.2
    protected $resultPageFactory;
    protected $_coreRegistry;
    protected $formFieldHelper;
    protected $scopeConfig;
    protected $_layout;
    protected $httpRequest;

    /**
     * Post constructor.
     * @param Context $context
     * @param \Magento\Store\Model\StoreManager $storeManager
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @param \Lof\Formbuilder\Helper\Data $helper
     * @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
     * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Lof\Formbuilder\Model\Form $form
     * @param \Lof\Formbuilder\Helper\Fields $formFieldHelper
     * @param \Magento\Framework\View\LayoutInterface $layout
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Framework\Filesystem $filesystem
     * @param \Magento\Framework\App\Request\Http $httpRequest
     * @param \Magento\Framework\Module\Manager $moduleManager
     * @param \Magento\Framework\App\ResourceConnection $resource
     * @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
     * @param DataPersistorInterface $dataPersistor
     * @param \Lof\Formbuilder\Model\MessageFactory $messageFactory
     * @param \Lof\Formbuilder\Model\BlacklistFactory $blacklistFactory
     * @param \Lof\Formbuilder\Helper\Barcode $barcodeHelper
     * @throws \Magento\Framework\Exception\FileSystemException
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        Context $context,
        \Magento\Store\Model\StoreManager $storeManager,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Lof\Formbuilder\Helper\Data $helper,
        \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Lof\Formbuilder\Model\Form $form,
        \Lof\Formbuilder\Helper\Fields $formFieldHelper,
        \Magento\Framework\View\LayoutInterface $layout,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\App\Request\Http $httpRequest,
        \Magento\Framework\Module\Manager $moduleManager,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
        DataPersistorInterface $dataPersistor,
        \Lof\Formbuilder\Model\MessageFactory $messageFactory,
        \Lof\Formbuilder\Model\BlacklistFactory $blacklistFactory,
        \Lof\Formbuilder\Helper\Barcode $barcodeHelper
    ) {
        $this->storeManager = $storeManager;
        $this->resultPageFactory = $resultPageFactory;
        $this->helper = $helper;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->_coreRegistry = $registry;
        $this->inlineTranslation = $inlineTranslation;
        $this->form = $form;
        $this->formFieldHelper = $formFieldHelper;
        $this->transportBuilder = $transportBuilder;
        $this->scopeConfig = $scopeConfig;
        $this->_layout = $layout;
        $this->_customerSession = $customerSession;
        $this->httpRequest = $httpRequest;
        $this->remoteAddress = $remoteAddress;
        $this->moduleManager = $moduleManager;
        $this->resource = $resource;
        $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
        $this->dataPersistor = $dataPersistor;
        $this->_messageFactory = $messageFactory;
        $this->_blacklistFactory = $blacklistFactory;
        $this->barcodeHelper = $barcodeHelper;
        parent::__construct($context);
    }

    protected function parse_size($size)
    {
        $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
        return round($size);
    }

    /**
     * Get Data Persistor
     *
     * @return DataPersistorInterface
     */
    private function getDataPersistor()
    {
        if ($this->dataPersistor === null) {
            $this->dataPersistor = ObjectManager::getInstance()
                ->get(DataPersistorInterface::class);
        }

        return $this->dataPersistor;
    }

    /**
     * Default customer account page
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        try {
            $store = $this->storeManager->getStore();
            //$resultRedirect = $this->resultRedirectFactory->create();
            $mediaUrl = $this->helper->getBaseMediaUrl();
            $error = false;
            $data = $this->getRequest()->getParams();
            if (empty($data)) {
                return $this->_redirect($store->getBaseUrl());
            }
            $current_url = isset($data['current_url']) ? $data['current_url'] : '';

            if (!$this->verifyBlacklist()) {
                return;
            }
            if (count($data) > 2) {
                $form = $this->form->load($data['formId']);
                $fields = $form->getFields();
                //$successMessage = $this->helper->filter($form->getData('success_message'));

                if (!$this->verifyCaptcha($data, $form)) {
                    return;
                }
                // UPLOAD FILE
                //$fieldPrefix = $this->helper->getFieldPrefix();
                $data = $this->uploadFormFiles($data, $form, $fields, $mediaUrl);
                if (!$data) {
                    return;
                }
                // Validate some fields.
                $form->escapeFormData($data);

                // Build form data.
                $customform_data = $form->getCustomFormFields($data);
                $phones = [];
                $data = $this->generateMessageHtml($customform_data, $data);

                /* Format form data to save in message params */
                $form_submit_data = [];
                if ($customform_data) {
                    foreach ($customform_data as $key => $val) {
                        if (isset($form_submit_data[$val['label']])) {
                            $val['label'] .= " " . $key;
                        }
                        $form_submit_data[$val['label']] = $this->helper->xssClean($val['value']);
                        if (isset($val['phone']) && $val['phone']) {
                            $phones[] = $val['phone'];
                        }
                    }
                    $this->_eventManager->dispatch('formbuilder_init_post_data', ['form_data' => $customform_data]);
                }
                //if enable check blacklist, then check all emails in form was in blacklist or not, if have one email in black list, then redirect it to homepage, else continue action
                $checkBlacklistEmail = $this->checkBlacklistEmails($form_submit_data);
                if (!$checkBlacklistEmail) {
                    return;
                }

                //Save message
                $message_data = [];
                $message_data['form_id'] = $form->getFormId();
                $message_data['ip_address'] = $this->remoteAddress->getRemoteAddress();
                $message_data['ip_address_long'] = $this->remoteAddress->getRemoteAddress(true);
                $message_data['customer_id'] = $this->_customerSession->getCustomerId();

                $params = [];
                $params['brower'] = $this->getRequest()->getServer('HTTP_USER_AGENT');
                if ($params['brower'] && is_array($params['brower'])) {
                    $tmp = [];
                    foreach ($params['brower'] as $key => $val) {
                        $val = $this->helper->xssClean($val);
                        $tmp[$key] = $val;
                    }
                    $params['brower'] = $tmp;
                } else {
                    $params['brower'] = $this->helper->xssClean($params['brower']);
                }
                $creationTime = $this->helper->getTimezoneDateTime(); //date('Y-m-d H:i:s');
                $sender_email = $this->helper->getSenderEmail();
                $params['http_host'] = $this->getRequest()->getHttpHost();
                $params['http_host'] = $this->helper->xssClean($params['http_host']);
                $params['submit_data'] = $form_submit_data;
                $params['current_url'] = $this->helper->xssClean($current_url);
                $message_data['params'] = $this->helper->encodeData($params);
                $message_data['message'] = isset($data["message_html"]) ? $data["message_html"] : "";
                $message_data['creation_time'] = $creationTime;
                $message_data['email_from'] = $sender_email;
                $message_data['form_data'] = json_encode($customform_data);

                $message = $this->_messageFactory->create();
                $message->setData($message_data);
                $message->save();

                /** run for hideprice module */
                $this->runFormForHidePrice($form, $data, $message);

                if ($message->getMessageId()) {
                    $this->_eventManager->dispatch('formbuilder_saved_message', ['message' => $message]);
                }

                $data["qrcode"] = $message->getQrcode();
                $data["barcode"] = $this->barcodeHelper->generateBarcodeLabel($message, false);
                $data["qrcode_tracking_link"] = $this->helper->getQrcodeTracklink($message);
                $data["track_url"] = $this->helper->getTrackUrl($message);

                $this->_eventManager->dispatch(
                    'formbuilder_submitted_message_after',
                    [
                        'object' => $this,
                        'form_data' => $customform_data,
                        'send_data' => $data,
                        'track_url' => $data["track_url"],
                        'form' => $form,
                        'phones' => $phones,
                        'message' => $message
                    ]
                );

                // SEND EMAIL
                // Update Data Field Value if array will convert to string
                $field_prefix = $this->formFieldHelper->getFieldPrefix();
                foreach ($data as $data_key => $data_value) {
                    if (false !== strpos($data_key, $field_prefix)) {
                        if (is_array($data_value)) {
                            $data[$data_key] = implode(", ", $data_value);
                        }
                    }
                }
                $sendEmailReturn = $this->sendNotificationEmails($form, $data, $fields);
                if (!$sendEmailReturn["flag"]) {
                    return;
                }

                // SEND THANKYOU EMAIL
                $sendThanksEmailReturn = $this->sendThanksyouEmails($form, $data, $fields);
                if (!$sendThanksEmailReturn["flag"]) {
                    return;
                }
                $error = $sendThanksEmailReturn["error"] || $sendEmailReturn["error"];
                $status = false;
                $responseData = [];
                if (!$error) {
                    $status = true;
                    $success_message = $form->getData('success_message');
                    if ($success_message) {
                        $successMessage1 = $this->helper->filter($success_message);
                        $this->messageManager->addSuccess($successMessage1);
                        $responseData['message'] = $successMessage1;
                    }
                }
                $responseData['error'] = $error;
                $responseData['status'] = $status;
                $this->getDataPersistor()->clear('formbuilder');
                $this->getResponse()->representJson(
                    $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                );
                return;
            }
        } catch (\Exception $e) {
            $this->messageManager->addError($e->getMessage());
            $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
        }
    }

    /**
     * @param $message
     * @return mixed
     */
    public function getMessage($message)
    {
        return $this->getResponse()->representJson(
            $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($message)
        );
    }

    /**
     * Set back redirect url to response
     *
     * @param null|string $backUrl
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    protected function goBack($backUrl = null)
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($backUrl) {
            $resultRedirect->setUrl($backUrl);
        }
        return $resultRedirect;
    }

    /**
     * Verify black list
     *
     * @return bool
     */
    public function verifyBlacklist()
    {
        $flag = true;
        $enable_blacklist = $this->helper->getConfig('general_settings/enable_blacklist');
        //check if enabled config blacklist, then check if ip in blacklist, then redirect it to home, else continue action
        if ($enable_blacklist) {
            $client_ip = $this->remoteAddress->getRemoteAddress();
            $blacklist_model = $this->_blacklistFactory->create();
            if ($client_ip) {
                $blacklist_model->loadByIp($client_ip);
                if ((0 < $blacklist_model->getId()) && $blacklist_model->getStatus()) {

                    $responseData = [];
                    $responseData['message'] =
                        __('Your IP was blocked in our blacklist. So, we will not allow submit the form.');
                    $responseData['status'] = false;
                    $this->messageManager->addError(
                        __('Your IP was blocked in our blacklist. So, we will not allow submit the form.')
                    );
                    $this->getResponse()->representJson(
                        $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                    );
                    $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                    $flag = false;
                }
            }
        }
        return $flag;
    }

    /**
     * Upload form files
     *
     * @param mixed|array $data
     * @param \Lof\Formbuilder\Model\Form|mixed $form
     * @param mixed|array $fields
     * @param string $mediaUrl
     * @return mixed|bool|array
     */
    public function uploadFormFiles($data, $form, $fields, $mediaUrl)
    {
        $flag = true;
        $fieldPrefix = $this->helper->getFieldPrefix();
        if (!empty($fields)) {
            $mediaFolder = $this->helper->getMediaFilePath();
            $savePath = $this->helper->getUploadMediaFilePath();
            foreach ($fields as $key => $field) {
                $cid = $this->helper->getFieldId($field);
                $field_name = $fieldPrefix . $cid . $form->getId();
                $image = $this->httpRequest->getFiles($field_name);
                if (isset($image['error']) && $image['error'] == 0) {

                    if (empty($field)) {
                        continue;
                    }
                    if ($field['field_type'] == 'file_upload') {
                        $fieldTypes = '';
                        if (isset($field['image_type'])) {
                            $fieldTypes = $field['image_type'];
                        }
                        if (!$fieldTypes) {
                            $fieldTypes = self::FILE_TYPES;
                        }
                        $fieldTypes = str_replace(" ", "", $fieldTypes);
                        if (!is_array($fieldTypes)) {
                            $fieldTypes = explode(',', $fieldTypes);
                        }
                        $file = '';
                        $file_exists = false;
                        if ($_FILES && isset($_FILES[$field_name])) {
                            if (file_exists($_FILES[$field_name]['tmp_name'])) {
                                $file_exists = true;
                            }
                        }
                        if (!isset($field['required']) || (isset($field['required']) && !$field['required'])) {
                            if ($_FILES && isset($_FILES[$field_name])) {
                                if (file_exists($_FILES[$field_name]['tmp_name'])) {
                                    $file_exists = true;
                                }
                            }
                        }
                        if (!$file_exists && (!isset($field['required']) || (isset($field['required']) && !$field['required']))) {
                            $file = '';
                        } else {
                            $uploader = $this->_objectManager->create(
                                'Magento\Framework\File\Uploader',
                                ['fileId' => $field_name]
                            );
                            $uploader->setAllowedExtensions($fieldTypes);
                            $uploader->setAllowRenameFiles(true);
                            $uploader->setFilesDispersion(false);
                            $file = $uploader->save($savePath);
                        }
                        if ($file && empty($file)) {
                            continue;
                        }

                        if ($file && !empty($file)) {
                            try {
                                $field_label = isset($field['label']) ? $field['label'] : '';

                                $image_maximum_size = $this->parse_size(@ini_get('upload_max_filesize'));
                                if ($image_maximum_size <= 0) {
                                    $image_maximum_size = 2;
                                }
                                if ($field && isset($field['image_maximum_size']) && $field['image_maximum_size']) {
                                    $image_maximum_size = $field['image_maximum_size'];
                                }

                                if (isset($field['image_maximum_size']) && ($image_maximum_size * 1024 * 1024) < $file['size']) {
                                    $this->messageManager->addError(__($field_label . ' - The file is too big.'));
                                    $this->mediaDirectory->delete('lof/formbuilder/files/' . $file['file']);
                                    $this->getDataPersistor()->set(
                                        'formbuilder',
                                        $this->getRequest()->getParams()
                                    );
                                    $flag = false;
                                    break;
                                }

                                $imgExtens = ["gif", "jpeg", "jpg", "png"];
                                $temp = explode(".", $file['file']);
                                $extension = end($temp);
                                $data[$field_name] = $field_name;
                                $data[$field_name . '_filename'] = $file['file'];
                                $data[$field_name . '_fileurl'] = $mediaUrl . $mediaFolder . '/' . $file['file'];
                                $data[$field_name . '_filesize'] = $file['size'];
                                if (in_array($extension, $imgExtens)) {
                                    $data[$field_name . '_isimage'] = true;
                                }
                            } catch (\Exception $e) {
                                if ($this->getRequest()->isAjax()) {
                                    $responseData = [];
                                    $responseData['message'] = $field_label . ' - ' . $e->getMessage();
                                    $responseData['status'] = false;
                                    $this->getResponse()->representJson(
                                        $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                                    );
                                }
                                $this->messageManager->addError($field_label . ' - ' . $e->getMessage());
                                $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                                $this->_redirect($data['return_url']);
                                $flag = false;
                                break;
                            }
                        }
                    }
                }
            }
        }
        return $flag ? $data : false;
    }

    /**
     * verify captcha
     *
     * @param mixed|data $data
     * @return bool
     */
    public function verifyCaptcha($data)
    {
        $flag = true;
        $post     = $this->getRequest()->getPostValue();
        // reCaptcha
        if (isset($post['g-recaptcha-response']) && ((int)$post['g-recaptcha-response']) === 0) {
            $flag = false;
            if ($this->getRequest()->isAjax()) {
                $this->messageManager->addError(__('Please check reCaptcha and try again.'));
            } else {
                $this->messageManager->addError(__('Please check reCaptcha and try again.'));
                $this->_redirect($data['return_url']);
                $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
            }
        }
        if (isset($post['g-recaptcha-response'])) {
            $flag = false;
            $captcha = $post['g-recaptcha-response'];
            $secretKey = $this->helper->getConfig('general_settings/captcha_privatekey');
            $ip = $this->remoteAddress->getRemoteAddress();

            $postData = http_build_query([
                'secret'   => $secretKey,
                'response' => $captcha,
                'remoteip' => $ip
            ]);

            $opts = [
                'http' => [
                    'method'  => 'POST',
                    'header'  => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $postData
                ]
            ];

            $context  = stream_context_create($opts);
            $response = @file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
            $result   = @json_decode($response);

            $flag = $result && isset($result->success) ? $result->success : false;
            if (!$flag) {
                if ($this->getRequest()->isAjax()) {
                    $responseData = [];
                    $responseData['message'] = __('Please check reCaptcha and try again.');
                    $responseData['status'] = false;
                    $this->getResponse()->representJson(
                        $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                    );
                    $this->messageManager->addError(__('Please check reCaptcha and try again.'));
                } else {
                    $this->messageManager->addError(__('Please check reCaptcha and try again.'));
                    $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                }
            }
        }
        return $flag;
    }

    /**
     * send notification emails
     *
     * @param mixed $form
     * @param mixed $data
     * @param mixed $fields
     * @return mixed
     */
    public function sendNotificationEmails($form, $data, $fields)
    {
        $this->inlineTranslation->suspend();

        $flag = true;
        $error = false;
        $store = $this->storeManager->getStore();
        $fieldPrefix = $this->helper->getFieldPrefix();
        $emails = $form->getData('email_receive');
        $current_url = isset($data['current_url']) ? $data['current_url'] : '';
        $reply_email_arr = [];
        if ($fields) {
            foreach ($fields as $fitem) {
                $field_type = isset($fitem['field_type']) ? $fitem['field_type'] : "";
                $cid = $this->helper->getFieldId($fitem);
                $field_id = $fieldPrefix . $cid . $form->getId();
                if ($field_type == "email" && isset($data[$field_id]) && $data[$field_id] && $this->helper->validateEmailAddress($data[$field_id])) {
                    $reply_email_arr[] = @trim($data[$field_id]);
                }
            }
            //$reply_emails = implode(",", $reply_email_arr);
        }
        if (@trim($emails) != '') {
            $fromAddress = $this->helper->getConfig('email_setting/sender_email_identity');
            $fromAddress = $fromAddress === null ? 'general' : $fromAddress;
            //If form have custom sender, will get email and name on the submitted form data
            $sender_email_field = $form->getData('sender_email_field');
            $sender_name_field = $form->getData('sender_name_field');
            if ($sender_email_field) {
                $sender_email_field = $this->formFieldHelper->getFieldPrefix() . $sender_email_field . $form->getId();
                if (isset($data[$sender_email_field]) && $data[$sender_email_field] && $this->helper->validateEmailAddress($data[$sender_email_field])) {
                    $fromAddress = ['email' => $data[$sender_email_field]];

                    $sender_name_field = $this->formFieldHelper->getFieldPrefix() . $sender_name_field . $form->getId();
                    if (isset($data[$sender_name_field]) && $data[$sender_name_field]) {
                        $fromAddress['name'] = $data[$sender_name_field];
                    }
                }
            }

            $emails = explode(',', $emails);
            $this->_eventManager->dispatch(
                'formbuilder_init_email_data',
                ['data' => $data, 'form' => $form, 'from_address' => $fromAddress, 'emails' => $emails]
            );

            foreach ($emails as $v) {
                try {
                    $postObject = new \Magento\Framework\DataObject();
                    $data['form_id'] = $form->getId();
                    $data['title'] = $form->getData('title');
                    $data['current_url'] = $current_url;
                    $tags = $form->getTags();
                    if ($tags) {
                        $tags_array = explode(",", $tags);
                        $newTags = [];
                        foreach ($tags_array as $_tag) {
                            $newTags[] = @trim($_tag);
                        }
                        $data['tags'] = implode(" , ", $newTags);
                    }
                    $postObject->setData($data);
                    //$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
                    $v = @trim($v);
                    if ($this->helper->validateEmailAddress($v)) {
                        $transportBuilder = $this->transportBuilder
                            ->setTemplateIdentifier($form->getData('email_template'))
                            ->setTemplateOptions(
                                [
                                    'area' => 'frontend',
                                    'store' => $store->getId()
                                ]
                            )
                            ->setTemplateVars(['data' => $postObject])
                            ->setFrom($fromAddress)
                            ->addTo($v);
                        if (is_array($fromAddress) && isset($fromAddress['email']) && $fromAddress['email']) {
                            $transportBuilder->setReplyTo($fromAddress['email']);
                        } else {
                            if ($reply_email_arr) {
                                $transportBuilder->setReplyTo($reply_email_arr[0]);
                            }
                        }

                        $transport = $transportBuilder->getTransport();
                        try {
                            $transport->sendMessage();
                            $this->inlineTranslation->resume();
                        } catch (\Exception $e) {
                            $error = true;
                            $this->helper->writeLogData($e);
                            if ($this->getRequest()->isAjax()) {
                                $responseData = [];
                                $responseData['message'] = __('An error when send email. We can\'t process your request right now. Sorry, that\'s all we know.');
                                $responseData['message'] .= $e->getMessage();
                                $responseData['status'] = false;
                                $this->getResponse()->representJson(
                                    $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                                );
                            }
                            $this->messageManager->addError(
                                __('An error when send email. We can\'t process your request right now. Sorry, that\'s all we know.')
                            );
                            $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                        }
                    }
                } catch (\Exception $e) {
                    $this->inlineTranslation->resume();
                    $this->helper->writeLogData($e);
                    if ($this->getRequest()->isAjax()) {
                        $responseData = [];
                        $responseData['message'] = __('An error when send email. We can\'t process your request right now. Sorry, that\'s all we know.');
                        $responseData['message'] .= $e->getMessage();
                        $responseData['status'] = false;
                        $this->getResponse()->representJson(
                            $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                        );
                    }
                    $this->messageManager->addError(
                        __('Errors when send emails. We can\'t process your request right now. Sorry, that\'s all we know.')
                    );
                    $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                    $flag = false;
                    break;
                }
            }
        }
        return [
            "flag" => $flag,
            "error" => $error
        ];
    }

    /**
     * send thanksyou emails
     *
     * @param mixed $form
     * @param mixed $data
     * @param mixed $fields
     * @return mixed
     */
    public function sendThanksyouEmails($form, $data, $fields)
    {
        $this->inlineTranslation->suspend();

        $error = false;
        $flag = true;
        $store = $this->storeManager->getStore();
        $fieldPrefix = $this->helper->getFieldPrefix();
        $field = $form->getData('thankyou_field');
        $send_thanks_email = $this->helper->getConfig('email_settings/send_thanks_email');
        $thanks_email_all = $this->helper->getConfig('email_settings/thanks_email_all');

        if ($send_thanks_email && $thanks_email_all && $fields) { //Send thanks you email to all email fields

            foreach ($fields as $fitem) {
                $field_type = isset($fitem['field_type']) ? $fitem['field_type'] : "";
                $cid = $this->helper->getFieldId($fitem);
                $field_id = $fieldPrefix . $cid . $form->getId();
                if ($field_type == "email" && isset($data[$field_id]) && $data[$field_id] && $this->helper->validateEmailAddress($data[$field_id]) && $form->getData('thankyou_email_template')) {
                    try {
                        $postObject = new \Magento\Framework\DataObject();
                        $data['form_id'] = $form->getFormId();
                        $data['title'] = $form->getData('title');
                        $data[$field_id] = @trim($data[$field_id]);
                        $data["ticket_id"] = $this->_customerSession->getTicketId();
                        $postObject->setData($data);
                        $transport = $this->transportBuilder
                            ->setTemplateIdentifier($form->getData('thankyou_email_template'))
                            ->setTemplateOptions(
                                [
                                    'area' => 'frontend',
                                    'store' => $store->getId()
                                ]
                            )
                            ->setTemplateVars(['data' => $postObject])
                            ->setFrom($this->helper->getConfig('email_settings/sender_email_identity'))
                            ->addTo($data[$field_id])
                            ->getTransport();
                        try {
                            $transport->sendMessage();
                            $this->inlineTranslation->resume();
                        } catch (\Exception $e) {
                            $error = true;
                            $this->helper->writeLogData($e);
                            if ($this->getRequest()->isAjax()) {
                                $responseData = [];
                                $responseData['message'] = __('An error when send email. We can\'t process your request right now. Sorry, that\'s all we know.');
                                $responseData['message'] .= $e->getMessage();
                                $responseData['status'] = false;
                                $this->getResponse()->representJson(
                                    $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                                );
                            }
                            $this->messageManager->addError(
                                __('An error when send thanks you email. We can\'t process your request right now. Sorry, that\'s all we know.')
                            );
                            $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                        }
                    } catch (\Exception $e) {
                        $this->helper->writeLogData($e);
                        $this->inlineTranslation->resume();
                        if ($this->getRequest()->isAjax()) {
                            $responseData = [];
                            $responseData['message'] = __('An error when send email. We can\'t process your request right now. Sorry, that\'s all we know.');
                            $responseData['message'] .= $e->getMessage();
                            $responseData['status'] = false;
                            $this->getResponse()->representJson(
                                $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                            );
                        }
                        $this->messageManager->addError(
                            __('Errors when send thanks you emails. We can\'t process your request right now. Sorry, that\'s all we know.')
                        );
                        $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                        $flag = false;
                        break;
                    }
                }
            }
        } elseif ($field && $send_thanks_email) { //Send thanks you email to selected email field

            $field = $this->formFieldHelper->getFieldPrefix() . $field . $form->getId();
            if (isset($data[$field]) && $data[$field] && $this->helper->validateEmailAddress($data[$field])) {
                try {
                    $postObject = new \Magento\Framework\DataObject();
                    $data['form_id'] = $form->getId();
                    $data['title'] = $form->getData('title');
                    $data[$field] = @trim($data[$field]);
                    $postObject->setData($data);
                    //$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
                    $transport = $this->transportBuilder
                        ->setTemplateIdentifier($form->getData('thankyou_email_template'))
                        ->setTemplateOptions(
                            [
                                'area' => 'frontend',
                                'store' => $store->getId()
                            ]
                        )
                        ->setTemplateVars(['data' => $postObject])
                        ->setFrom($this->helper->getConfig('email_settings/sender_email_identity'))
                        ->addTo($data[$field])
                        ->getTransport();
                    try {
                        $transport->sendMessage();
                        $this->inlineTranslation->resume();
                    } catch (\Exception $e) {
                        $error = true;
                        $this->helper->writeLogData($e);
                        if ($this->getRequest()->isAjax()) {
                            $responseData = [];
                            $responseData['message'] = __('An error when send email. We can\'t process your request right now. Sorry, that\'s all we know.');
                            $responseData['message'] .= $e->getMessage();
                            $responseData['status'] = false;
                            $this->getResponse()->representJson(
                                $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                            );
                        }
                        $this->messageManager->addError(
                            __('An error when send a thanks you email. We can\'t process your request right now. Sorry, that\'s all we know.')
                        );
                        $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                    }
                } catch (\Exception $e) {
                    $this->helper->writeLogData($e);
                    $this->inlineTranslation->resume();
                    if ($this->getRequest()->isAjax()) {
                        $responseData = [];
                        $responseData['message'] = __('An error when send email. We can\'t process your request right now. Sorry, that\'s all we know.');
                        $responseData['status'] = false;
                        $this->getResponse()->representJson(
                            $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                        );
                    }
                    $this->messageManager->addError(
                        __('An error when send a thanks you emails. We can\'t process your request right now. Sorry, that\'s all we know.')
                    );
                    $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                    $flag = false;
                }
            }
        }
        return [
            "flag" => $flag,
            "error" => $error
        ];
    }

    /**
     * Check emails in submitted data is in blacklist
     *
     * @param mixed $form_submit_data
     * @return bool
     */
    public function checkBlacklistEmails($form_submit_data)
    {
        $flag = true;
        $enable_blacklist = $this->helper->getConfig('general_settings/enable_blacklist');
        if ($form_submit_data && $enable_blacklist) {
            $emails = $this->helper->getEmailsFromData($form_submit_data);
            if ($emails) {
                $blacklist_collection = $this->_blacklistFactory->create()->getCollection()->addFieldToFilter(
                    "status",
                    1
                );
                $blacklist_collection->addEmailsToFilter($emails);

                if ($blacklist_collection->getSize()) {
                    $responseData = [];
                    $responseData['message'] = __('One or more emails in form were blocked in our blacklist. So, we will not allow submit the form.');
                    $responseData['status'] = false;
                    $this->messageManager->addError(__('One or more emails in form were blocked in our blacklist. So, we will not allow submit the form.'));
                    $this->getDataPersistor()->set('formbuilder', $this->getRequest()->getParams());
                    $this->getResponse()->representJson(
                        $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseData)
                    );

                    $flag = false;
                }
            }
        }
        return $flag;
    }

    /**
     * generate message html content
     *
     * @param mixed $customform_data
     * @param mixed $data
     * @return mixed
     */
    public function generateMessageHtml($customform_data, $data)
    {
        $creationTime = $this->helper->getTimezoneDateTime(); //date('Y-m-d H:i:s');
        $message_html = $this->_layout->createBlock('\Magento\Framework\View\Element\Template')
            ->setTemplate("Lof_Formbuilder::email/items.phtml")
            ->setCustomFormData($customform_data)
            ->setCreationTime($creationTime)
            ->toHtml();

        $show_all_fields = $this->helper->getConfig('email_settings/show_all_fields');
        if (!$show_all_fields) {
            $data['message'] = $this->_layout->createBlock('\Magento\Framework\View\Element\Template')
                ->setTemplate("Lof_Formbuilder::email/items_check_enable.phtml")
                ->setCustomFormData($customform_data)
                ->setCreationTime($creationTime)
                ->toHtml();
        } else {
            $data['message'] = $message_html;
        }
        $data["message_html"] = $message_html;
        return $data;
    }

    /**
     * run form for hide price compatible
     *
     * @param mixed $data
     * @param \Lof\Formbuilder\Model\Message $message
     * @return void
     */
    public function runFormForHidePrice($data, $message)
    {
        if ($this->moduleManager->isEnabled('Lof_HidePrice') && isset($data['hideprice_id']) && $data['hideprice_id']) {
            try {
                $connection = $this->resource->getConnection();
                $table = $this->resource->getTableName('lof_hideprice_hideprice_message');
                $_data = [];
                $_data[] = [
                    'hideprice_id' => $data['hideprice_id'],
                    'entity_id' => $data['entity_id'],
                    'message_id' => $message->getMessageId()
                ];
                $connection->insertMultiple($table, $_data);
            } catch (\Exception $e) {
                $this->messageManager->addError($e->getMessage());
            }
        }
    }
}
