<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class File_service
{

    protected $ci;

    public function __construct()
    {
        $this->ci = & get_instance();
    }

    public function load_parser()
    {
        $this->ci->load->library('ciext_help/readme_parser');

        $this->ci->readme_parser->setBreaksEnabled(true);
        $this->ci->readme_parser->setMarkupEscaped(true);
        $this->ci->readme_parser->setUrlsLinked(true);

        return true;
    }

    public function search_files()
    {
        $folders = $this->ci->config->item('modules_locations');

        $buffer = [];
        foreach (array_keys($folders) as $dir) {
            $modules = array_diff(scandir($dir), ['..', '.']);
            if (!empty($modules)) {
                foreach ($modules as $item) {
                    if (!is_dir($dir . $item)) {
                        continue;
                    }
                    $file = $dir . $item . '/config/doc.php';
                    if (file_exists($file)) {
                        $tmp = include_once $file;
                        $buffer = $this->merge_recursive($buffer, $tmp);
                    }
                }
            }
        }

        return $this->recursive_sort($buffer);
    }

    public function get_file_path($files = [])
    {
        $page = $this->ci->input->get('page');
        $keys = explode('/', (string) $page);
        if ($page && count($keys) > 1) {
            foreach ($keys as $val) {
                $files = (!empty($files[$val]['pages']) && $val != $keys[count($keys) - 1]) ? $files[$val]['pages'] : $files[$val];
            }
            $path = $files['file'];
        } else {
            if ($page) {
                $path = $files[$keys[0]]['file'];
            } else {
                foreach ($files as $f) {
                    $path = $f['file'];
                    break;
                }
            }
        }

        return $path;
    }

    protected function merge_recursive($arr1 = array(), $arr2 = array())
    {
        if (is_array($arr2)) {
            foreach ($arr2 as $k2 => $v2) {
                if (is_array($v2)) {
                    $tmp = isset($arr1[$k2]) ? $arr1[$k2] : array();
                    $arr1[$k2] = $this->merge_recursive($tmp, $v2);
                }
                if (!isset($arr1[$k2])) {
                    $arr1[$k2] = $v2;
                } elseif (isset($arr1[$k2]) && is_array($arr1) && is_int($k2)) {
                    $arr1[] = $v2;
                }
            }
        }
        
        return $arr1;
    }

    protected function recursive_sort($array = [])
    {
        if (count($array) > 1) {
            uasort($array, [$this, 'cmp']);
            foreach ($array as & $item) {
                if (!empty($item['pages'])) {
                    $item['pages'] = $this->recursive_sort($item['pages']);
                }
            }
            unset($item);
        }

        return $array;
    }

    protected function cmp($a, $b)
    {
        if ($a['position'] == $b['position']) {

            return 0;
        }

        return ($a['position'] < $b['position']) ? -1 : 1;
    }

}
