<?php


namespace CMSMSExt\FS;

use CMSMSExt\core\Error;
use CMSMSExt\Exceptions\FileSystemException;

class manifest_builder
{
  
  use \CMSMSExt\core\TSingleton;
  
  protected $_source;
  protected $_target;
  protected $_source_version;
  protected $_target_version;
  protected $_out_dir;
  protected $_root_path;
  protected $_dir_compr;
  protected $_excludes_list = [];
  protected $_excludes_pattern_list = [];
  protected $_manifest;
  
//  protected array $_copy_excludes = [
//                                      '/git/',
//                                      '/\.md$/i',
//                                      '/svn/',
//                                      '/index\.html?$/',
//                                      '/config\.php$/',
//                                      '/phar_installer/',
//                                      '/scripts/',
//                                      '/svn-.*/',
//                                      '/tests/',
//                                      '/UNUSED/',
//                                      '/HIDE/',
//                                      '/uploads/',
//                                      '/~$/',
//                                      '/\.bak$/',
//                                      '/#.*/',
//                                      '/\.#.*/',
//                                    ];
//  //TODO root-dir  '/\.htaccess$/',
//
//  protected array $_compare_excludes = [
//                                          '.git*',
//                                          '*.md',
//                                          '*.MD',
//                                          '.svn',
//                                          'svn-*',
//                                          '*.bak',
//                                          '*~',
//                                          '*.sh',
//                                          '*.pl',
//                                          '*.bat',
//                                          '.#*',
//                                          '#*',
//                                          'config.php',
//                                          'index.html',
//                                          'UNUSED*',
//                                          'tmp',
//                                          'scripts',
//                                          'tests',
//                                          'install',
//                                          'phar_installer',
//                                        ];
  
//  /**
//   * manifest_builder constructor.
//   *
//   * @param string $source
//   * @param string $target
//   *
//   * @throws \CMSMSExt\Exceptions\FileSystemException
//   */
//  public function __construct(string $source = '', string $target = '', string $ini_name = '')
//  {
//

//
//    //$this->_dir_compr->
//  }
  
  protected function _reset()
  {
    $this->_source         = '';
    $this->_target         = '';
    $this->_source_version = '';
    $this->_target_version = '';
    $this->_target         = '';
    $this->_root_path      = '';
    $this->_out_dir        = '';
  }
  
  protected function _initialize()
  {
    $source = utils::join_path($this->_root_path, $this->_source);
    
    if(!\is_dir($source))
    {
      throw new FileSystemException(\xt_utils::get_xt(), Error::INVALID_DIR_ERROR, $source);
    }

    if(!\is_readable($source))
    {
      throw new FileSystemException('Directory ' . $source . ' is not readable');
    }
  
    $target = utils::join_path($this->_root_path, $this->_target);

    if(!\is_dir($target))
    {
      throw new FileSystemException(\xt_utils::get_xt(), Error::INVALID_DIR_ERROR, $target);
    }

    if(!\is_readable($target))
    {
      throw new FileSystemException('Directory ' . $target . ' is not readable');
    }
    
    $this->_source  = $source;
    $this->_target  = $target;
  }
  
  /**
   * public methods
   */
  
  /**
   * @param mixed $source
   *
   * @return manifest_builder
   */
  public function setSource($source)
  {
    $this->_source = $source;
    
    return $this;
  }
  
  /**
   * @param mixed $target
   *
   * @return manifest_builder
   */
  public function setTarget($target)
  {
    $this->_target = $target;
    
    return $this;
  }
  
  /**
   * @param mixed $source_version
   *
   * @return manifest_builder
   */
  public function setSourceVersion($source_version)
  {
    $this->_source_version = $source_version;
    
    return $this;
  }
  
  /**
   * @param mixed $target_version
   *
   * @return manifest_builder
   */
  public function setTargetVersion($target_version)
  {
    $this->_target_version = $target_version;
    
    return $this;
  }
  
  /**
   * @param mixed $out_dir
   *
   * @return manifest_builder
   */
  public function setOutDir($out_dir)
  {
    $this->_out_dir = $out_dir;
    
    return $this;
  }
  
  /**
   * @param mixed $root_path
   *
   * @return manifest_builder
   */
  public function setRootPath($root_path)
  {
    $this->_root_path = $root_path;
    
    return $this;
  }
  
//  /**
//   * @param mixed $dir_compr
//   *
//   * @return manifest_builder
//   */
//  public function setDirCompr($dir_compr)
//  {
//    $this->_dir_compr = $dir_compr;
//
//    return $this;
//  }
  
  /**
   * @param array $excludes_list
   *
   * @return manifest_builder
   */
  public function setExcludesList(array $excludes_list) : manifest_builder
  {
    $this->_excludes_list = $excludes_list;
    
    return $this;
  }
  
  /**
   * @param array $excludes_pattern_list
   *
   * @return manifest_builder
   */
  public function setExcludesPatternList(array $excludes_pattern_list) : manifest_builder
  {
    $this->_excludes_pattern_list = $excludes_pattern_list;
    
    return $this;
  }
  
  public function run()
  {
  
  }
  
  
  
}