<?php

    class Modules {

        private $modules = null;
        private $wrongModules = null;
        private $toReturn = array();
        private $configs = array();

        function __construct(){
            $this->checkModules();
            $this->readModules();
        }

        protected function checkModules(){

            $this->modules      = array();
            $this->wrongModules = array();

            $modules = dir(ROOT_PATH . 'modules');

            while($module = $modules->read()){

                if($module != ".." && $module != "."
                && is_dir(ROOT_PATH . 'modules/' . $module)){

                    $file = ROOT_PATH . 'modules/' . $module . '/module.json';

                    if(file_exists($file)){
                        $this->modules[] = $module;
                    }else{
                        $this->wrongModules[] = $module;
                    }
                }
            }

            sort($this->modules);

        }

        protected function readModules(){

            $modules = array();

            foreach($this->modules as $module){
                $file = file_get_contents(ROOT_PATH . 'modules/' . $module . '/module.json');
                $config = json_decode($file);

                $menu = $config->menu;
                $module = $config->folder_name;

                $submenus = null;
                if( isset($menu->submenus) && $menu->submenus != "" ){
                    $submenus = array();
                    foreach( $menu->submenus as $subs ){
                        $submenus[] = array(
                            "name" => $subs->name,
                            "link" => '/' . $module . '/' . $subs->controller,
                            "is_admin" =>
                                ( isset($subs->is_admin) && $subs->is_admin != "" )
                                ? $subs->is_admin : false
                        );
                    }
                }

                $toReturn = array();

                if(isset($config->id))
                    $toReturn["id"] = $config->id;
                    
                $toReturn["name"] = $menu->name;
                $toReturn["position"] = $menu->position;
                
                if(!$submenus){
                    $toReturn['link'] = '/' . $module . '/' . $menu->controller;
                }else{
                    $toReturn['submenus'] = $submenus;
                }

                $this->toReturn[] = $toReturn;

                $configurator = @$config->configurator;

                if(isset($configurator)){
                    $this->configs[] = array(
                        "name" => $configurator->name,
                        "link" => '/' . $module . '/' . $configurator->controller
                    );
                }
            }
        }

        public function getModules(){
            return $this->toReturn;
        }

        public function getConfigurators(){
            return $this->configs;
        }

        public function getWrongModules(){
            return implode(', ', $this->wrongModules);
        }

    }
