<?php
class ModelExtensionDashboardWebskyIranmap extends Model {
	public function getOrderTotalsByProvince($source = 'payment') {
		$status_ids = array();
		$complete_statuses = $this->config->get('config_complete_status');

		if (is_array($complete_statuses)) {
			foreach ($complete_statuses as $order_status_id) {
				$status_ids[] = (int)$order_status_id;
			}
		}

		if (!$status_ids) {
			return array();
		}

		$prefix = $source === 'shipping' ? 'shipping' : 'payment';
		$zone_id = 'o.' . $prefix . '_zone_id';
		$zone_name = 'o.' . $prefix . '_zone';

		$query = $this->db->query("SELECT COUNT(*) AS total, SUM(o.total) AS amount, COALESCE(NULLIF(z.code, ''), '') AS zone_code, COALESCE(NULLIF(z.name, ''), " . $zone_name . ") AS zone_name FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "zone` z ON (z.zone_id = " . $zone_id . ") WHERE o.order_status_id IN (" . implode(',', $status_ids) . ") GROUP BY " . $zone_id . ", z.code, z.name, " . $zone_name);

		return $query->rows;
	}

	public function resolveProvinceCode($zone_code, $zone_name) {
		$code_map = array(
			'TEH' => 'IR.TH', 'QOM' => 'IR.QM', 'MKZ' => 'IR.MK', 'QAZ' => 'IR.QZ',
			'GIL' => 'IR.GI', 'ARD' => 'IR.AR', 'ZAN' => 'IR.ZA', 'EAZ' => 'IR.EA',
			'WEZ' => 'IR.WA', 'WAZ' => 'IR.WA', 'KRD' => 'IR.KD', 'HMD' => 'IR.HD',
			'KRM' => 'IR.BK', 'ILM' => 'IR.IL', 'LRS' => 'IR.LO', 'KZT' => 'IR.KZ',
			'CMB' => 'IR.CM', 'KBA' => 'IR.KB', 'BSH' => 'IR.BS', 'FAR' => 'IR.FA',
			'HRM' => 'IR.HG', 'SBL' => 'IR.SB', 'KRB' => 'IR.KE', 'YZD' => 'IR.YA',
			'EFH' => 'IR.ES', 'ISF' => 'IR.ES', 'SMN' => 'IR.SM', 'MZD' => 'IR.MN',
			'GLS' => 'IR.GO', 'NKH' => 'IR.KS', 'RKH' => 'IR.KV', 'SKH' => 'IR.KJ',
			'ALB' => 'IR.AL'
		);

		$zone_code = strtoupper(trim((string)$zone_code));

		if (isset($code_map[$zone_code])) {
			return $code_map[$zone_code];
		}

		if (preg_match('/^IR\.[A-Z]{2}$/', $zone_code)) {
			return $zone_code;
		}

		$name = $this->normalizeName($zone_name);
		$name_map = $this->getNameMap();

		return isset($name_map[$name]) ? $name_map[$name] : '';
	}

	private function normalizeName($name) {
		$name = trim((string)$name);
		$name = str_replace(array('ي', 'ى', 'ك', 'ۀ', 'ة', '‌', '-', '_', '.', ',', 'استان'), array('ی', 'ی', 'ک', 'ه', 'ه', ' ', ' ', ' ', ' ', ' ', ''), $name);
		$name = preg_replace('/\s+/u', ' ', $name);

		return function_exists('mb_strtolower') ? mb_strtolower(trim($name), 'UTF-8') : strtolower(trim($name));
	}

	private function getNameMap() {
		$aliases = array(
			'IR.TH' => array('تهران', 'tehran'),
			'IR.QM' => array('قم', 'qom'),
			'IR.MK' => array('مرکزی', 'markazi'),
			'IR.QZ' => array('قزوین', 'qazvin'),
			'IR.GI' => array('گیلان', 'gilan'),
			'IR.AR' => array('اردبیل', 'ardabil', 'ardebil'),
			'IR.ZA' => array('زنجان', 'zanjan'),
			'IR.EA' => array('آذربایجان شرقی', 'اذربایجان شرقی', 'east azerbaijan', 'east azarbaijan'),
			'IR.WA' => array('آذربایجان غربی', 'اذربایجان غربی', 'west azerbaijan', 'west azarbaijan'),
			'IR.KD' => array('کردستان', 'kurdistan', 'kordestan'),
			'IR.HD' => array('همدان', 'hamedan', 'hamadan'),
			'IR.BK' => array('کرمانشاه', 'kermanshah'),
			'IR.IL' => array('ایلام', 'ilam'),
			'IR.LO' => array('لرستان', 'lorestan'),
			'IR.KZ' => array('خوزستان', 'khuzestan'),
			'IR.CM' => array('چهارمحال و بختیاری', 'چهار محال و بختیاری', 'chaharmahal and bakhtiari', 'chahar mahall and bakhtiari'),
			'IR.KB' => array('کهگیلویه و بویراحمد', 'کهگیلویه و بویر احمد', 'kohgiluyeh and boyer ahmad', 'kohgiluyeh and buyer ahmad'),
			'IR.BS' => array('بوشهر', 'bushehr'),
			'IR.FA' => array('فارس', 'fars'),
			'IR.HG' => array('هرمزگان', 'hormozgan'),
			'IR.SB' => array('سیستان و بلوچستان', 'sistan and baluchestan'),
			'IR.KE' => array('کرمان', 'kerman'),
			'IR.YA' => array('یزد', 'yazd'),
			'IR.ES' => array('اصفهان', 'esfahan', 'isfahan'),
			'IR.SM' => array('سمنان', 'semnan'),
			'IR.MN' => array('مازندران', 'mazandaran'),
			'IR.GO' => array('گلستان', 'golestan'),
			'IR.KS' => array('خراسان شمالی', 'north khorasan'),
			'IR.KV' => array('خراسان رضوی', 'razavi khorasan', 'khorasan razavi'),
			'IR.KJ' => array('خراسان جنوبی', 'south khorasan'),
			'IR.AL' => array('البرز', 'alborz')
		);

		$map = array();

		foreach ($aliases as $code => $names) {
			foreach ($names as $name) {
				$map[$this->normalizeName($name)] = $code;
			}
		}

		return $map;
	}
}
