<?php
/**
* 2023 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2023 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

class AnMenu extends ObjectModel
{
    /**
     * @var
     */
    public $id;
    /**
     * @var
     */
    public $id_anmenu;
    /**
     * @var int
     */
    public $id_shop;
    /**
     * @var
     */
    public $name;
    /**
     * @var int
     */
    public $active = 1;
    /**
     * @var int
     */
    public $position;
    /**
     * @var
     */
    public $link;
    /**
     * @var
     */
    public $label;
    /**
     * @var string
     */
    public $label_color = '#17cf00';
    /**
     * @var int
     */
    public $drop_column = 0;
    /**
     * @var string
     */
    public $drop_bgcolor = '#ffffff';
    /**
     * @var
     */
    public $drop_bgimage;
    /**
     * @var string
     */
    public $bgimage_position = 'right bottom';
    /**
     * @var int
     */
    public $position_x = 0;
    /**
     * @var int
     */
    public $position_y = 0;

    /**
     * @var array
     */
    public static $definition = array(
        'table' => 'anmenu',
        'primary' => 'id_anmenu',
        'multilang' => true,
        'multilang_shop' => false,
        'fields' => array(
            'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'label_color' => array('type' => self::TYPE_STRING, 'validate' => 'isColor', 'size' => 32),
            'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
            'position' => array('type' => self::TYPE_INT),
            'drop_column' => array('type' => self::TYPE_INT),
            'drop_bgcolor' => array('type' => self::TYPE_STRING, 'validate' => 'isColor', 'size' => 32),
            'drop_bgimage' => array('type' => self::TYPE_STRING, 'size' => 128),
            'bgimage_position' => array('type' => self::TYPE_STRING, 'size' => 50),
            'position_x' => array('type' => self::TYPE_INT),
            'position_y' => array('type' => self::TYPE_INT),
            'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml', 'required' => true, 'size' => 254),
            'link' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isUrlOrEmpty', 'size' => 254),
            'label' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128),
        ),
    );

    /**
     * AnMenu constructor.
     * @param null $id_anmenu
     * @param null $id_lang
     */
    public function __construct($id_anmenu = null, $id_lang = null)
    {
        parent::__construct($id_anmenu, $id_lang);

        if (!$this->id_shop) {
            $this->id_shop = Context::getContext()->shop->id;
        }

        if (!$this->position) {
            $this->position = 1 + $this->getMaxPosition();
        }

        if (!$id_anmenu && Context::getContext()->language->is_rtl) {
            $this->bgimage_position = 'left bottom';
        }
    }

    public static function getFieldsValueGroups($id)
    {
        $sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'anmegamenu_groups`  WHERE id_anmenu = ' . (int) $id . '';
        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql, true, false);
        
        $idGroup = [];
        if (!empty($result) && count($result) != 0){
            foreach ($result as $key => $var){
                $idGroup[$key] = $var['id_group']; 
            } 
            return $idGroup;
        } else {
            return $idGroup;
        }      
    }

    public static function saveGroups($idRule, $groupBox)
    {
        if (!$idRule){
            return false;
        }
  
        self::deleteGroups($idRule);

        if (!is_array($groupBox) || count($groupBox) == 0){
            $groups = Group::getGroups(Context::getContext()->language->id);
            $groupBox = [];
            foreach ($groups as $group){
                $groupBox[] = $group['id_group'];
            }
        } 
  
        foreach ($groupBox as $id_group){
            $sql = '
                INSERT INTO `'._DB_PREFIX_.'anmegamenu_groups`  (`id_anmenu`, `id_group`) 
                VALUES ("'.(int) $idRule.'", "'.(int) $id_group.'")';
            Db::getInstance(_PS_USE_SQL_SLAVE_)->Execute($sql);
        }
    }

    public static function deleteGroups($id_anmenu)
    {
        $sql = '
            DELETE FROM `' . _DB_PREFIX_ . 'anmegamenu_groups`		
            WHERE `id_anmenu` = ' . (int) $id_anmenu . '';
    
        Db::getInstance(_PS_USE_SQL_SLAVE_)->execute($sql, true, false);
    }    

    
    /**
     * @param $id_lang
     * @param bool $active
     * @return mixed
     */
    public static function getList($id_lang, $active = true, $ignoreGroup = true)
    {
        $id_lang = is_null($id_lang) ? Context::getContext()->language->id : (int)$id_lang;
        $id_shop = Context::getContext()->shop->id;

        $sql_group = '';
        $sql_group_table = '';
        if (!$ignoreGroup){
            $idGroupCustomer = Customer::getGroupsStatic(Context::getContext()->customer->id);
            $sql_group = 'AND m.`id_anmenu` = anmg.`id_anmenu` AND anmg.`id_group` = ' . (int) $idGroupCustomer . '';
            $sql_group_table = ', `'._DB_PREFIX_.'anmegamenu_groups` anmg';
        }
        

        $query = 'SELECT *
            FROM `' . _DB_PREFIX_ . 'anmenu` m
            LEFT JOIN `' . _DB_PREFIX_ . 'anmenu_lang` ml ON m.`id_anmenu` = ml.`id_anmenu`
            '.$sql_group_table.'
            WHERE m.`id_shop` = ' . (int)$id_shop . '
            AND `id_lang` = ' . (int)$id_lang . '
            ' . ($active ? 'AND `active` = 1' : '') . '
            '.$sql_group.'
            GROUP BY m.`id_anmenu`
            ORDER BY m.`position` ASC';

        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);

        return $result;
    }

    /**
     * @return int
     */
    public function getMaxPosition()
    {
        $id_shop = Context::getContext()->shop->id;
        $query = 'SELECT MAX(m.`position`)
            FROM `' . _DB_PREFIX_ . 'anmenu` m
            WHERE m.`id_shop` = ' . (int)$id_shop;

        return (int)Db::getInstance()->getValue($query);
    }

    /**
     * @param $id_anmenu
     * @param $position
     */
    public static function updatePosition($id_anmenu, $position)
    {
        $query = 'UPDATE `' . _DB_PREFIX_ . 'anmenu`
			SET `position` = ' . (int)$position . '
			WHERE `id_anmenu` = ' . (int)$id_anmenu;

        Db::getInstance()->execute($query);
    }
}
