<?php

class ControllerExtensionModuleCategory extends Controller {
    private $language_id;

    public function __construct($registry) {
        parent::__construct($registry);
        $this->language_id = $registry->config->get('config_language_id');
    }

    public function index() {

        $data = array_merge(array(), $this->language->load('extension/module/category'));
        $data['heading_title'] = $this->language->get('heading_title');

        if (isset($this->request->get['path'])) {
            $parts = explode('_', (string)$this->request->get['path']);
        } else {
            $parts = array();
        }

        if (isset($parts[0])) {
            $data['category_id'] = $parts[0];
        } else {
            $data['category_id'] = 0;
        }

        if (isset($parts[1])) {
            $data['child_id'] = $parts[1];
        } else {
            $data['child_id'] = 0;
        }

        if (isset($parts[1], $parts[2])) {
            $data['child_id2'] = $parts[2];
        } else {
            $data['child_id2'] = 0;
        }


        $this->load->model('catalog/category');
        $this->load->model('catalog/product');

        if (Config::get('theme_default_module_category_show_only_subcategories', false)) {

            $current_category = array_reverse($parts);
            if (!$current_category) {
                $current_category[0] = 0;
            }

            $categories = $this->model_catalog_category->getCategories($current_category[0]);
        } else {
            $categories = $this->model_catalog_category->getCategories(0);
        }

        $data['categories'] = [];
        $data['cats'] = [];

        foreach ($parts as $part) {
            $cat = $this->model_catalog_category->getCategory($part);

            if($cat){
                $cat['href'] = $this->url->link('product/category', 'path=' . $this->model_catalog_category->getCategoryPath($cat['category_id']));
                $data['cats'][] = $cat;
            }
        }



        foreach ($categories as $category) {
            $children_data = $this->getChildren($category['category_id']);

            $filter_data = array(
              'filter_category_id'  => $category['category_id'],
              'filter_sub_category' => true
            );

            $data['categories'][] = array(
              'category_id' => $category['category_id'],
              'name'        => $category['name'],
              'total'       => $this->config->get('config_product_count') ? $this->model_catalog_product->getTotalProducts($filter_data) : false,
              'children'    => $children_data,
              'href'        => $this->url->link('product/category', 'path=' . $category['path']),
              'active'      => (in_array($category['category_id'], $parts) ? true : false)
            );

        }


        $data['category_path'] = $parts;

        return $this->load->view('extension/module/category', $data);
    }



    public function getChildren($category_id, $children_data = array()) {

        //pr( ddd());

        if (isset($this->request->get['path'])) {
            $parts = explode('_', (string)$this->request->get['path']);
        } else {
            $parts = array();
        }

        $this->load->model('catalog/category');

        $children = $this->model_catalog_category->getCategories($category_id);

        foreach ($children as $child) {


            $children_data[] = array(
              'category_id' => $child['category_id'],
              'name'        => $child['name'],
              'children'    => $this->getChildren($child['category_id']),
              'total'       => ($this->config->get('config_product_count') ? $this->model_catalog_product->getTotalProducts(['filter_category_id' => $child['category_id']]) : ''),
              'href'        => $this->url->link('product/category', 'path=' . $this->model_catalog_category->getCategoryPath($child['category_id'])),
              'active'      => (in_array($child['category_id'], $parts) ? true : false),
              'level'       => count(explode('_', (string)$this->model_catalog_category->getCategoryPath($child['category_id'])))
            );
        }

        return $children_data;
    }

}