<?php
class ModelExtensionModuleEpikselItags extends Model {
	public function getSetting() {
		$setting = $this->config->get('epiksel_itags_setting');

		if (isset($setting)) {
			return $setting;
		} else {
			return false;
		}
	}

	public function getItags($product_id, $types = 'product') {
		$product_itag_data = array();

		$default_setting = $this->getSetting();

		if ($this->config->get('epiksel_itags_status')) {
			$this->document->addStyle('catalog/view/assets/css/wogaoding.css');

			$sql = "SELECT it.itag_id AS itag_id, it.location, it.position, it.settings, itd.name, itd.caption, itd.image FROM " . DB_PREFIX . "itag it LEFT JOIN " . DB_PREFIX . "itag_description itd ON (it.itag_id = itd.itag_id) LEFT JOIN " . DB_PREFIX . "itag_to_customer_group it2cg ON (it.itag_id = it2cg.itag_id) LEFT JOIN " . DB_PREFIX . "itag_to_store it2s ON (it.itag_id = it2s.itag_id) LEFT JOIN " . DB_PREFIX . "product_to_itag p2it ON (p2it.itag_id = it.itag_id) LEFT JOIN " . DB_PREFIX . "itag_to_category i2c ON (i2c.itag_id = it.itag_id) LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.category_id = i2c.category_id) WHERE it.status = '1' AND it2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND itd.language_id = '" . (int)$this->config->get('config_language_id') . "'";

			// Customer Group
			$sql .=  " AND (it2cg.customer_group_id = '0' OR it2cg.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "')";

			// Category or Product
			$sql .=  " AND (p2it.product_id = '" . (int)$product_id . "' OR p2c.product_id = '" . (int)$product_id . "')";

			// Date Start and Date End
			$sql .=  " AND ((it.date_start = '0000-00-00' OR it.date_start <= '" . $this->db->escape(date('Y-m-d')) . "') AND (it.date_end = '0000-00-00' OR it.date_end >= '" . $this->db->escape(date('Y-m-d')) . "'))";

			$sql .=  " ORDER BY it.sort_order, it.position";

			$this->load->model('tool/image');

			$image_width = $default_setting['width'];
			$image_height = $default_setting['height'];
			$default_tooltip_status = $default_setting['tooltip_status'];
			$default_tooltip_position = $default_setting['tooltip_position'];
			$default_tooltip_title = $default_setting['tooltip_title'];
			$default_pages_status = $default_setting['pages_status'];
			$default_modules_status = $default_setting['modules_status'];

			//$effect = $default_setting['effect'];
			//$speed = $default_setting['speed'];

			$product_itag_query = $this->db->query($sql);

			if ($product_itag_query->num_rows) {
				foreach ($product_itag_query->rows as $product_itag) {
					$itag_setting = json_decode($product_itag['settings'], true);

					$itag_id = $product_itag['itag_id'];

					if ($types == 'pages') {
						if (isset($itag_setting['pages_status']) && $itag_setting['pages_status'] == 2) {
							$type_status = false;
						} elseif (isset($itag_setting['pages_status']) && $itag_setting['pages_status']) {
							$type_status = $itag_setting['pages_status'];
						} else {
							$type_status = $default_pages_status;
						}
					} elseif ($types == 'modules') {
						if (isset($itag_setting['modules_status']) && $itag_setting['modules_status'] == 2) {
							$type_status = false;
						} elseif (isset($itag_setting['modules_status']) && $itag_setting['modules_status']) {
							$type_status = $itag_setting['modules_status'];
						} else {
							$type_status = $default_modules_status;
						}
					} elseif ($types == 'product') {
						$type_status = true;
					}

					if ($type_status) {
						if (!empty($itag_setting['width'])) {
							$image_width = $itag_setting['width'];
						}

						if (!empty($itag_setting['height'])) {
							$image_height = $itag_setting['height'];
						}

						$image = false; $itag_type_class = 'itag-text-content';

						if ($product_itag['image']) {
							$image = $this->model_tool_image->resize($product_itag['image'], $image_width, $image_height);
							$itag_type_class = 'itag-image-content';

							if (!empty($product_itag['name'])) {
								$itag_type_class .= ' itag-text-content';
							}
						}

						$itag_link = (isset($itag_setting['link']) && $itag_setting['link']) ? $itag_setting['link'] : false;

						if ($itag_link) {
							if (isset($itag_setting['target']) && $itag_setting['target']) {
								$itag_link = 'window.open(\'' . $itag_link . '\');';
							} else {
								$itag_link = 'window.location.href=\'' . $itag_link . '\'';
							}
						}

						if (isset($itag_setting['tooltip_status']) && $itag_setting['tooltip_status'] == 2) {
							$tooltip_status = false;
						} elseif (isset($itag_setting['tooltip_status']) && $itag_setting['tooltip_status']) {
							$tooltip_status = $itag_setting['tooltip_status'];
						} else {
							$tooltip_status = $default_tooltip_status;
						}

						if ($tooltip_status && $product_itag['caption']) {
							$tooltip_content = $product_itag['caption'];
						} else {
							$tooltip_content = false;
						}

						if (isset($itag_setting['tooltip_position']) && $itag_setting['tooltip_position'] == 2) {
							$tooltip_position = false;
						} elseif (isset($itag_setting['tooltip_position']) && $itag_setting['tooltip_position']) {
							$tooltip_position = $itag_setting['tooltip_position'];
						} else {
							$tooltip_position = $default_tooltip_position;
						}

						if (isset($itag_setting['tooltip_title']) && $itag_setting['tooltip_title'] == 2) {
							$tooltip_title = false;
						} elseif (isset($itag_setting['tooltip_title']) && $itag_setting['tooltip_title']) {
							$tooltip_title = $itag_setting['tooltip_title'];
						} else {
							$tooltip_title = $default_tooltip_title;
						}

						if ($product_itag['position'] == 'top_left' || $product_itag['position'] == 'top_right') {
							$itag_position = 'top';
						} elseif ($product_itag['position'] == 'bottom_left' || $product_itag['position'] == 'bottom_right') {
							$itag_position = 'bottom';
						} else {
							$itag_position = false;
						}

						$itag_position_class = '';

						if ($types == 'product') {
							if ($product_itag['location'] == 'left') {
								$itag_position_class .= ($product_itag['position'] == 'top_left') ? " itag-product-image itag-top-left-product" : '';
								$itag_position_class .= ($product_itag['position'] == 'top_right') ? " itag-product-image itag-top-right-product" : '';
								$itag_position_class .= ($product_itag['position'] == 'bottom_left') ? " itag-product-image itag-bottom-left-product" : '';
								$itag_position_class .= ($product_itag['position'] == 'bottom_right') ? " itag-product-image itag-bottom-right-product" : '';
							}
							if (isset($itag_setting['padding_status']) && $itag_setting['padding_status']) {
								$itag_position_class .= ($product_itag['location'] == 'bottom' || $product_itag['location'] == 'right') ? " itag-padding-none" : false;
							}
						} else {
							$itag_position_class .= ($product_itag['position'] == 'top_left') ? " itag-top-left-other" : false;
							$itag_position_class .= ($product_itag['position'] == 'top_right') ? " itag-top-right-other" : false;
							$itag_position_class .= ($product_itag['position'] == 'bottom_left') ? " itag-bottom-left-other" : false;
							$itag_position_class .= ($product_itag['position'] == 'bottom_right') ? " itag-bottom-right-other" : false;
						}

						$itag_content = "<li class='itag-content " . $itag_type_class . $itag_position_class . " itag-" . $itag_id . "'";

						if ($itag_link) {
							$itag_content .= ' onclick="' . $itag_link . '"';
						}

						if ($tooltip_content) {
							$itag_content .= " data-container='body' data-toggle='popover'";

							if ($tooltip_title) {
								$itag_content .= " title='" . $product_itag['name'] . "'";
							}

							$itag_content .= " data-content='" . $product_itag['caption'] . "'";
							$itag_content .= " data-placement='" . $tooltip_position . "'";
							$itag_content .= " data-html='true'";
						}

						$itag_content .= ">";

						if ($image && isset($itag_setting['image_text']) && $itag_setting['image_text'] && ($product_itag['location'] == 'right' || $product_itag['location'] == 'bottom')) {
							$itag_content .= "<img src='" . $image . "' width='" . $image_width . "' height='" . $image_height . "' alt='" . $product_itag['name'] . "' class='img-responsive' /><br />" . $product_itag['name'];
						} elseif ($image) {
							$itag_content .= "<img src='" . $image . "' width='" . $image_width . "' height='" . $image_height . "' alt='" . $product_itag['name'] . "' class='img-responsive' />";
						} else {
							$text_color = "style='";
							$text_color .= (!empty($itag_setting['background'])) ? 'background:#' . $itag_setting['background'] . ';' : false;
							$text_color .= (!empty($itag_setting['font_color'])) ? 'color:#' . $itag_setting['font_color'] . ';' : false;
							$text_color .= "'";

							$itag_content .= "<span class='itag-text' " . $text_color . ">" . $product_itag['name'] . "</span>";
						}

						$itag_content .= "</li>";

						$default = $itag_content;
						$special = $free_shipping = false;

						if ($itag_id == $default_setting['special_tag_id']) {
							$default = '';
							$special = $itag_content;
						}

						if ($itag_id == $default_setting['shipping_tag_id']) {
							$default = '';

							if ($this->getShipping($product_id)) {
								$free_shipping = $itag_content;
							}
						}

						$product_itag_data[$itag_id] = array(
							'itag_id'    => $itag_id,
							'product_id' => $product_id,
							'content'    => $default,
							'location'   => $product_itag['location'],
							'position'   => $itag_position,
							'type'       => ($image) ? 'image' : 'text',
							'height'     => $image_height,
							'shipping'   => $free_shipping,
							'special'    => $special,
						);
					} else {
						$product_itag_data = array();
					}
				}
			}
		}

		return $product_itag_data;
	}

	protected function getShipping($product_id) {
		$free_shipping = false;

		$product = $this->getProduct($product_id);

		if ($product && $product['shipping']) {
			if ((float)$product['special']) {
				$price = $this->tax->calculate($product['special'], $product['tax_class_id'], $this->config->get('config_tax'));
			} else {
				$price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'));
			}

			if ($this->config->get('free_status') && $price >= (float)$this->config->get('free_total')) {
				$free_shipping = true;
			}
		}

		return $free_shipping;
	}

	protected function getProduct($product_id) {
		$query = $this->db->query("SELECT DISTINCT p.shipping, p.price, p.tax_class_id, (SELECT ps.price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < '" . $this->db->escape(date('Y-m-d')) . "') AND (ps.date_end = '0000-00-00' OR ps.date_end > '" . $this->db->escape(date('Y-m-d')) . "')) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special FROM " . DB_PREFIX . "product p WHERE p.product_id = '" . (int)$product_id . "'");

		return $query->row;
	}
}