<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2016/12/28
 * Time: 下午12:31
 */

require_once(BASE_ROOT_PATH . '/helper/img_helper.php');
require_once(BASE_ROOT_PATH . '/helper/special_helper.php');
require_once(BASE_ROOT_PATH . '/helper/time_interval.php');
require_once(BASE_ROOT_PATH . '/helper/util_helper.php');

class brand_helper
{
    static private $stInstance = null;

    private $mBrandPageResult;
    private $mBaseInfo;

    private function __construct()
    {
        $this->mBrandPageResult = [];
        $this->mBaseInfo = [];
    }

    static public function instance()
    {
        if (self::$stInstance == null) {
            self::$stInstance = new brand_helper();
        }
        if(StatesHelper::fetch_state('brands')) {
            Log::record("brand_helper reload data.",Log::DEBUG);
            self::$stInstance->init();
        }

        return self::$stInstance;
    }
    public function name($brand_id)
    {
        $brand_id = intval($brand_id);
        if(array_key_exists($brand_id,$this->mBaseInfo)) {
            return $this->mBaseInfo[$brand_id]['brand_name'];
        } else {
            return '';
        }
    }

    public function country($brand_id)
    {
        $brand_id = intval($brand_id);
        if(array_key_exists($brand_id,$this->mBaseInfo)) {
            return $this->mBaseInfo[$brand_id]['brand_country'];
        } else {
            return '';
        }
    }
    public function country_logo($brand_id)
    {
        $brand_id = intval($brand_id);
        if(array_key_exists($brand_id,$this->mBaseInfo)) {
            return $this->mBaseInfo[$brand_id]['brand_country_logo'];
        } else {
            return '';
        }
    }
    public function author_desc($brand_id)
    {
        $brand_id = intval($brand_id);
        if(array_key_exists($brand_id,$this->mBaseInfo)) {
            return $this->mBaseInfo[$brand_id]['author_desc'];
        } else {
            return '';
        }
    }

    private function init()
    {
        $data = $this->read_cache();

        $this->mBrandPageResult = $data;
        $this->mBaseInfo = $data['base_info'];
    }

    private function read_cache()
    {
        $ret = rcache('brandex', 'mb_');

        if (!empty($ret)) {
            return unserialize($ret['data']);
        }

        global $config;
        $exbrands = $config['exclude_brands'];

        $brands = [];
        $base_info = [];

        $model = Model('brand');
        $items = $model->field('brand_id,brand_logo,brand_name,brand_country,brand_country_logo,author_desc')->where(array('brand_apply' => '1'))->order('brand_sort asc')->limit(false)->select();
        foreach ($items as $item)
        {
            $image = UPLOAD_SITE_URL . $item['brand_logo'];
            $ret = util::imgsize($image);
            if ($ret != false) {
                $item['brand_logo'] = $image;
            } else {
                Log::record("cant get {$item['brand_name']}." ,Log::ERR);
                continue;
            }


            if(empty($item['brand_country_logo'])) {
                $conuntry_logo = '';
                Log::record("cant get {$item['brand_name']} brand_country_logo." ,Log::ERR);
            } else {
                $conuntry_logo = UPLOAD_SITE_URL . $item['brand_country_logo'];
            }

            $item['brand_country_logo'] = $conuntry_logo;
            $brand_id = intval($item['brand_id']);
            $base_info[$brand_id] = $item;

            if(!empty($exbrands) && is_array($exbrands) && in_array($brand_id,$exbrands)) {
                continue;
            } else {
                $brands[] = $item;
            }
        }

        $block = special_formater::format_grid_brands($brands);
        $result = array("brands" => $brands, "block" => $block,'base_info' => $base_info);
        wcache("brandex", array("data" => serialize($result)), 'mb_');

        return $result;
    }

    public function brandex()
    {
        return $this->mBrandPageResult;
    }

    //////////for old interface/////////////////////////////////////////////////////////////////////////////////////////
    static function brands()
    {
        $prefix = 'mb_';
        $ret = rcache('brand', $prefix);
        if (empty($ret)) {
            $model = Model();
            $brand_area_list = $model->table('brand_area')->order('area_sort asc')->limit(false)->select();
            $filed = 'brand_id,brand_name,brand_pic,brand_img_bg,brand_img_logo,brand_area_id';
            $brand_c_list = $model->table('brand')->field($filed)->where(array('brand_apply' => '1'))->order('brand_sort asc')->limit(false)->select();

            $brands = array();
            foreach ($brand_area_list as $brand_area) {
                foreach ($brand_c_list as $brand) {
                    $brand['brand_pic'] = image_helper::format_brand_img_rect($brand['brand_pic']);
                    $brand['brand_img_bg'] = image_helper::format_brand_img_bg($brand['brand_img_bg']);
                    $brand['brand_img_logo'] = image_helper::format_brand_img_circle($brand['brand_img_logo']);
                    if ($brand['brand_area_id'] == $brand_area['area_id']) {
                        if (empty($brand_area['data'])) {
                            $brand_area['data'] = array();
                        }
                        array_push($brand_area['data'], $brand);
                    }
                }
                if (!empty($brand_area['data'])) {
                    array_push($brands, $brand_area);
                }
            }

            wcache("brand", array("brands" => serialize($brands)), $prefix);
        } else {
            $brands = unserialize($ret['brands']);
        }

        return $brands;
    }
}