<?php

class ControllerModuleCategory extends Controller
{
    protected function index($setting)
    {
        $this->language->load('module/category');

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

        $this->load->type('tipocomum');

        $style = isset($setting['style']) && $setting['style'] ? $setting['style'] : 1;
        $this->data['category_style'] = Type_StyleCategory::getNickFromValue($style)['class'];

        if (isset($setting['fidelity'])) {
            $route = 'fidelity/category';
        } else {
            $route = 'product/category';
        }

        if (isset($setting['fidelity'])) {
            $fidelity = $setting['fidelity'];
        } else {
            $fidelity = false;
        }

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

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

        if (isset($parts[2])) {
            $this->data['subchild_id'] = $parts[2];
        } else {
            $this->data['subchild_id'] = 0;
        }

        $this->load->model('catalog/category');
        $this->load->model('catalog/product');
        $this->data['categories'] = array();

        $isFilterModule = (isset($setting['filtersubcategory']) && $setting['filtersubcategory']) || (isset($setting['subcategory']));
        $this->data['is_filter'] = $isFilterModule;

        if (($isFilterModule) && (sizeof($parts) > 0)) {
            if ((count($parts) > 2) || ((isset($parts[1])) && (!$this->model_catalog_category->hasChildren($parts[1])))) {
                $category_id = $parts[count($parts) - 2];
            } else {
                $category_id = $parts[count($parts) - 1];
            }
        } else {
            $category_id = 0;
        }

        if ($category_id === 0) {
            $categories = $this->model_catalog_category->getCategories($category_id, $fidelity);
        } else {
            $cataux = $this->model_catalog_category->getCategory($category_id);
            $categories = array();
            if (sizeof($cataux) > 0) {
                $categories = array($this->model_catalog_category->getCategory($category_id));
            }
        }

        foreach ($categories as $category) {
            $children_data = array();
            $children = $this->model_catalog_category->getCategories($category['category_id'], $fidelity);

            $catname = $category['name'];

            foreach ($children as $child) {
                $data = array(
                    'filter_category_id' => $child['category_id'],
                    'filter_sub_category' => true
                );

                $subchildren_data = array();
                $subchildren = $this->model_catalog_category->getCategories($child['category_id'], $fidelity);


                $urlchild = '';
                $filterPath = '';
                if (isset($this->request->get['category_id'])) {
                    $urlchild .= '&category_id=' . $child['category_id'];
                } else {
                    $filterPath = '';
                    if (($isFilterModule) && ($category['parent_id'] != 0)) {
                        if ($child['parent_id'] != $category['parent_id']) {
                            $filterPath = $category['parent_id'] . '_' . $child['parent_id'] . '_';
                        } else {
                            $filterPath = $child['parent_id'] . '_';
                        }
                    } else {
                        $filterPath = $category['category_id'] . '_';
                    }

                    $urlchild .= '&path=' . $filterPath . $child['category_id'];
                }

                // if (isset($this->request->get['search'])) {
                //     $urlchild .= '&search=' . $this->request->get['search'];
                // }

                //level 3
                foreach ($subchildren as $subchild) {

                    $urlsubchild = '';


                    if (isset($this->request->get['category_id'])) {
                        $urlsubchild .= '&category_id=' . $subchild['category_id'];
                    } else {
                        $urlsubchild .= $urlchild . '_' . $subchild['category_id'];
                    }

                    // if (isset($this->request->get['search'])) {
                    //     $urlsubchild .= '&search=' . $this->request->get['search'];
                    // }

                    $subchildren_data[] = array(
                        'category_id' => $subchild['category_id'],
                        'name' => $subchild['name'],
                        'href' => $this->url->link($route, $urlsubchild)
                    );
                }

                $children_data[] = array(
                    'category_id' => $child['category_id'],
                    'name' => $child['name'],
                    'children' => $subchildren_data,
                    'href' => $this->url->link($route, $urlchild)
                );
            }


            if ($isFilterModule) {
                $this->data['categories'] = $children_data;
            } else {

                $url = '';

                if (isset($this->request->get['category_id'])) {
                    $url = '&category_id=' . $category['category_id'];
                } else {
                    $url .= '&path=' . $category['category_id'];
                }


                // if (isset($this->request->get['search'])) {
                //     $url .= '&search=' . $this->request->get['search'];
                // }

                $this->data['categories'][] = array(
                    'category_id' => $category['category_id'],
                    'name' => $category['name'],
                    'children' => $children_data,
                    'href' => $this->url->link($route, $url)
                );
            }
        }

        $this->data['display_product_grid'] = $this->config->get('futuragrade_opcao_cor');

        $this->data['text_see_all'] = $this->language->get('text_see_all');

        if (((sizeof($categories) > 0)) && ((isset($setting['filtersubcategory']) && $setting['filtersubcategory']) || (isset($setting['subcategory'])))) {
            if (strlen($catname) > 20) {
                $this->data['heading_title'] = substr($catname, 0, 20) . '...';
                $this->data['title_heading_title'] = $catname;
            } else {
                $this->data['heading_title'] = $catname;
            }
        } else {
            $this->data['heading_title'] = $this->language->get('heading_title');
        }

        $this->document->addScript('public/js/module/category.js');

        $this->render('module/category');
    }
}
