<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/3/22
 * Time: 上午10:40
 */

namespace area;
require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
require_once(BASE_ROOT_PATH . '/helper/algorithm.php');

use algorithm;

class area_validator extends area_check
{
    private $mProvinces;
    private $mCities;
    private $mCountries;

    public function __construct($table)
    {
        parent::__construct($table);
        $this->fill();
    }

    public function province($area_id)
    {
        $area_id = intval($area_id);
        if(algorithm::binary_search($this->mProvinces,$area_id)) {
            return array('province' => $this->mItems[$area_id]);
        } else {
            return false;
        }
    }
    public function city($area_id)
    {
        $area_id = intval($area_id);
        if(algorithm::binary_search($this->mCities,$area_id)) {
            $result = [];
            $result['city'] = $this->mItems[$area_id];
            $parent_id = $this->mItems[$area_id]['area_parent_id'];
            $result['province'] = $this->mItems[$parent_id];
            return $result;
        } else {
            return false;
        }
    }
    public function country($area_id)
    {
        $area_id = intval($area_id);
        if(algorithm::binary_search($this->mCountries,$area_id)) {
            $result = [];
            $result['country'] = $this->mItems[$area_id];

            $city_id = $result['country']['area_parent_id'];
            $result['city'] = $this->mItems[$city_id];

            $province_id = $this->mItems[$city_id]['area_parent_id'];
            $result['province'] = $this->mItems[$province_id];
            return $result;
        } else {
            return false;
        }
    }

    public function from_areaid($area_id)
    {
        if(array_key_exists($area_id,$this->mItems))
        {
            $areas = [];

            $area = $this->mItems[$area_id];
            $areas[] = $area;
            $parent_id = intval($area['area_parent_id']);
            while ($parent_id > 0) {
                $area = $this->mItems[$parent_id];
                $areas[] = $area;
                $parent_id = intval($area['area_parent_id']);
            }

            $result = [];
            $count = count($areas);
            for($index = $count - 1 ; $index >= 0; --$index)
            {
                if($index == $count - 1) {
                    $result['province'] = $areas[$index];
                }
                elseif($index == $count - 2) {
                    $result['city'] = $areas[$index];
                }
                elseif($index == $count - 3) {
                    $result['country'] = $areas[$index];
                }
            }

            return $result;

        } else {
            return false;
        }
    }

    public function from_name($prov_name, $city_name, $country_name)
    {
        if(empty($prov_name) || empty($city_name) || empty($country_name)) {
            return false;
        }

        if(array_key_exists($prov_name,$this->mNameToProvid))
        {
            $result = [];

            $prov_id = $this->mNameToProvid[$prov_name];
            $result['province'] = $this->mItems[$prov_id];

            $cities = $this->mAreaIds[$prov_id];
            foreach ($cities as $city_id)
            {
                $city = $this->mItems[$city_id];
                if($city['area_name'] == $city_name)
                {
                    $result['city'] = $city;
                    $countries = $this->mAreaIds[$city_id];

                    foreach ($countries as $country_id)
                    {
                        $country = $this->mItems[$country_id];
                        if ($country['area_name'] == $country_name) {
                            $result['country'] = $country;
                            return $result;
                        }
                    }
                }
            }
        }

        return false;
    }

    private function fill()
    {
        $items = $this->sub_areas(0);
        $provins = [];
        foreach ($items as $item) {
            $area_id = intval($item['area_id']);
            if($area_id >= 32) continue; //海外地区不处理了。
            $provins[] = $area_id;
        }
        foreach ($provins as $area_id) {
            $this->mProvinces[] = intval($area_id);
        }
        sort($this->mProvinces);

        $citys = $this->next($provins);
        foreach ($citys as $area_id) {
            $this->mCities[] = intval($area_id);
        }
        sort($this->mCities);

        $countries = $this->next($citys);
        foreach ($countries as $area_id) {
            $this->mCountries[] = intval($area_id);
        }
        sort($this->mCountries);
    }
    public function nosub_city()
    {
        $result = [];
        foreach ($this->mCities as $city_id) {
            $subs = $this->mAreaIds[$city_id];
            if(empty($subs)) {
                $result[] = $this->mItems[$city_id];
            }
        }
        return $result;
    }
}