<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Controller\Product;

use Magento\Framework\App\Action;

class CloneDesign extends Action\Action
{
    const CUSTOM_PREVIEWS = 'custom_previews';
    protected $jsonFactory;
    protected $file;
    protected $advancePreviewFileFactory;

    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magento\Framework\Filesystem\Driver\File $file,
        \Magetop\Advancepreview\Model\AdvancePreviewFileFactory $advancePreviewFileFactory
    )
    {
        parent::__construct($context);
        $this->jsonFactory = $jsonFactory;
        $this->file = $file;
        $this->advancePreviewFileFactory = $advancePreviewFileFactory;

    }

    public function execute()
    {
        $response = [
            'status'=>'error',
            'message'=>'error',
            'clone_folder'=>'',
        ];
        $productId = $this->getRequest()->getParam('product_id',0);
        $defaultModel =  $this->getRequest()->getParam('default_model',0);
        try {
            $customFolder = self::CUSTOM_PREVIEWS.'/product_id_'.$productId;
            $advancePreviewFile = $this->advancePreviewFileFactory->create();
            $customFolderUpload = $advancePreviewFile->getBaseDir($customFolder);
            $ownFolder = $this->cloneDesignFolder($customFolderUpload,$productId,$defaultModel);
            $response['message'] = $customFolderUpload. '/' . $customFolder .'/'. $defaultModel;
            $response['status'] = 'success';
            $response['clone_folder'] = $ownFolder;
        }
        catch (\Exception $exception) {
            $response['message'] = $exception->getMessage();
        }
        return $this->jsonFactory->create()->setData($response);
    }
    protected function cloneDesignFolder($customFolderUpload, $productId, $folderPreview, $ignoreFile = '') {
        $ownFolder = $productId.'_'.time();
        $this->copyPreviewModel($customFolderUpload.'/model-'.$folderPreview,$customFolderUpload,$ownFolder, $ignoreFile);
        return $ownFolder;
    }
    private function copyPreviewModel(
        $sourceDirectory,
        $destinationDirectory,
        $childFolder = '',
        $ignoreFile = ''
    ) {
        $directory = opendir($sourceDirectory);

        if (is_dir($destinationDirectory) === false) {
            mkdir($destinationDirectory);
        }

        if ($childFolder !== '') {
            if (is_dir("$destinationDirectory/$childFolder") === false) {
                mkdir("$destinationDirectory/$childFolder");
            }

            while (($file = readdir($directory)) !== false) {
                if ($file === '.' || $file === '..' || $file == $ignoreFile) {
                    continue;
                }

                if (is_dir("$sourceDirectory/$file") === true) {
                    recurseCopy("$sourceDirectory/$file", "$destinationDirectory/$childFolder/$file");
                } else {
                    copy("$sourceDirectory/$file", "$destinationDirectory/$childFolder/$file");
                }
            }

            closedir($directory);

            return;
        }

        while (($file = readdir($directory)) !== false) {
            if ($file === '.' || $file === '..') {
                continue;
            }

            if (is_dir("$sourceDirectory/$file") === true) {
                recurseCopy("$sourceDirectory/$file", "$destinationDirectory/$file");
            }
            else {
                copy("$sourceDirectory/$file", "$destinationDirectory/$file");
            }
        }

        closedir($directory);
    }
}
