<?php
$loader = $this->registry->get('load');
$loader->controller('module/filter');

class ControllerModuleFuturagrade extends ControllerModuleFilter
{
  protected function index($setting)
  {

    if (!$this->config->get('futuragrade_status')) {
      return false;
    }

    $language = $this->language->load('module/futuragrade');
    $this->data = array_merge($this->data, $language);
    $this->load->helper('ftr_geral');

    $this->data['heading_title_specification'] = (ftr_geral::isIsset($this->config->get('futuragrade_title_specification')) && $this->config->get('futuragrade_title_specification') != "") ? $this->config->get('futuragrade_title_specification') : $this->language->get('heading_title_specification');

    $data = parent::getFilters();

    if (isset($this->request->get['filcolor'])) {
      $filtroscores = explode(',', (string)$this->request->get['filcolor']);
    } else {
      $filtroscores = false;
    }

    if (isset($this->request->get['filspecification'])) {
      $filtrosspecification = explode(',', (string)$this->request->get['filspecification']);
    } else {
      $filtrosspecification = false;
    }

    if (isset($this->request->get['filsize'])) {
      $filtrossize = explode(',', (string)$this->request->get['filsize']);
    } else {
      $filtrossize = false;
    }

    if (isset($this->request->get['filbrand'])) {
      $filtromarca = explode(',', (string)$this->request->get['filbrand']);
    } else {
      $filtromarca = false;
    }

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

    $this->data['exibe_cor'] = $this->config->get('futuragrade_exibef_cor');
    $this->data['exibe_size'] = $this->config->get('futuragrade_exibef_size');
    $this->data['exibe_specification'] = $this->config->get('futuragrade_exibef_specification');

    if ($this->data['exibe_cor'] == '1') {
      $listCores = $this->model_catalog_product->getGradeProdutosList($data, 'PRODUCT_COLOR');
    } else {
      $listCores = false;
    }
    if ($this->data['exibe_size'] == '1') {
      $listsize = $this->model_catalog_product->getGradeProdutosList($data, 'PRODUCT_SIZE');
    } else {
      $listsize = false;
    }
    if ($this->data['exibe_specification'] == '1') {
      $listSpecification = $this->model_catalog_product->getGradeProdutosList($data, 'PRODUCT_SPECIFICATION');
    } else {
      $listSpecification = false;
    }

    if ($listCores && is_array($listCores)) {
      $this->data['clearcor'] = false;
      if (($listCores) && ($filtroscores)) {
        foreach ($listCores as $coratual) {
          $ind_specification = ftr_geral::getStrToIndex($coratual['descricao_especificacao']);
          $this->data['listCor'][$ind_specification]['titulo_especificacao'] = $coratual['descricao_especificacao'];
          $this->data['listCor'][$ind_specification]['lista_especificacao'][] =
            array(
              'product_color_id' => $coratual['product_color_id'],
              'description' => $coratual['description'],
              'primary_color' => ($coratual['primary_color']) ? $coratual['primary_color'] : false,
              'secundary_color' => ($coratual['secundary_color']) ? $coratual['secundary_color'] : false,
              'checked' => in_array($coratual['product_color_id'], $filtroscores) ? "checked" : ""
            );
        }
        $this->data['clearcor'] = true;
      } else {
        foreach ($listCores as $cor) {
          $ind_specification = ftr_geral::getStrToIndex($cor['descricao_especificacao']);
          $this->data['listCor'][$ind_specification]['titulo_especificacao'] = $cor['descricao_especificacao'];
          $this->data['listCor'][$ind_specification]['lista_especificacao'][] = $cor;
        }
      }

      if ($this->config->get('futuragrade_order_filtro') && ($this->data['listCor'])) {
        foreach ($listCores as $cor) {
          $ind_specification = ftr_geral::getStrToIndex($cor['descricao_especificacao']);
          usort($this->data['listCor'][$ind_specification], function ($a, $b) {
            if (isset($b["checked"]) && isset($a["checked"])) {
              return strcmp($b["checked"], $a["checked"]);
            }
          });
        }
      }

    } else {
      $this->data['listCor'] = false;
    }

    if ($listsize && is_array($listsize)) {
      $this->data['clearsize'] = false;
      if (($listsize) && ($filtrossize)) {
        foreach ($listsize as $sizeatual) {
          $ind_specification = ftr_geral::getStrToIndex($sizeatual['descricao_especificacao']);
          $this->data['listSize'][$ind_specification]['titulo_especificacao'] = $sizeatual['descricao_especificacao'];
          $this->data['listSize'][$ind_specification]['lista_especificacao'][] =
            array(
              'product_size_id' => $sizeatual['product_size_id'],
              'description' => $sizeatual['description'],
              'checked' => in_array($sizeatual['product_size_id'], $filtrossize) ? "checked" : ""
            );
        }
        $this->data['clearsize'] = true;
      } else {
        foreach ($listsize as $size) {
          $ind_specification = ftr_geral::getStrToIndex($size['descricao_especificacao']);
          $this->data['listSize'][$ind_specification]['titulo_especificacao'] = $size['descricao_especificacao'];
          $this->data['listSize'][$ind_specification]['lista_especificacao'][] = $size;
        }
      }

      if ($this->config->get('futuragrade_order_filtro') && ($this->data['listSize'])) {
        foreach ($listsize as $size) {
          $ind_specification = ftr_geral::getStrToIndex($size['descricao_especificacao']);
          usort($this->data['listSize'][$ind_specification], function ($a, $b) {
            if (isset($b["checked"]) && isset($a["checked"])) {
              return strcmp($b["checked"], $a["checked"]);
            }
          });
        }
      }
    } else {
      $this->data['listSize'] = false;
    }

    if ($listSpecification && is_array($listSpecification)) {
      $this->data['clearspecifications'] = false;
      if (($listSpecification) && ($filtrosspecification)) {
        foreach ($listSpecification as $specifications) {
          $ind_specification = ftr_geral::getStrToIndex($specifications['descricao_especificacao']);
          $this->data['listSpecification'][$ind_specification]['titulo_especificacao'] = $specifications['descricao_especificacao'];
          $this->data['listSpecification'][$ind_specification]['lista_especificacao'][] =
            array(
              'product_specification_id' => $specifications['product_specification_id'],
              'description' => $specifications['description'],
              'checked' => in_array($specifications['product_specification_id'], $filtrosspecification) ? "checked" : ""
            );

        }
        $this->data['clearspecifications'] = true;
      } else {
        foreach ($listSpecification as $specifications) {
          $ind_specification = ftr_geral::getStrToIndex($specifications['descricao_especificacao']);
          $this->data['listSpecification'][$ind_specification]['titulo_especificacao'] = $specifications['descricao_especificacao'];
          $this->data['listSpecification'][$ind_specification]['lista_especificacao'][] = $specifications;
        }
      }

      if ($this->config->get('futuragrade_order_filtro') && ($this->data['listSpecification'])) {
        foreach ($listSpecification as $specifications) {
          $ind_specification = ftr_geral::getStrToIndex($specifications['descricao_especificacao']);
          usort($this->data['listSpecification'][$ind_specification], function ($a, $b) {
            if (isset($b["checked"]) && isset($a["checked"])) {
              return strcmp($b["checked"], $a["checked"]);
            }
          });
        }
      }
    } else {
      $this->data['listSpecification'] = false;
    }

    if ((!$this->data['listCor']) && (!$this->data['listSpecification']) && (!$this->data['listSize'])) {
      return false;
    }


    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/futuragrade.twig')) {
      $this->template = $this->config->get('config_template') . '/template/module/futuragrade.twig';
    } else {
      $this->template = 'default/template/module/futuragrade.twig';
    }

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

}
