<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/5/9
 * Time: 下午9:06
 */
require_once(BASE_ROOT_PATH . '/helper/img_helper.php');
require_once(BASE_ROOT_PATH . '/helper/special_helper.php');

class category_helper
{
    const img_path = '/mobile/category';
    private $mCategories = NULL;
    private $mCids = NULL;

    static private $stInstance = NULL;
    static public function instance()
    {
        if(self::$stInstance == NULL) {
            self::$stInstance = new category_helper();
        }
        return self::$stInstance;
    }

    private function __construct()
    {
        $this->mCids = array();
        self::load_xml();
    }

    public function name($hot_id)
    {
        foreach ($this->mCategories as $item)
        {
            if($item['hot_id'] == $hot_id) {
                return $item['name'];
            }
            foreach ($item['subitem'] as $subitem)
            {
                if($subitem['hot_id'] == $hot_id) {
                    return $subitem['name'];
                }
            }
        }

        return '';
    }

    private function load_xml()
    {
        libxml_use_internal_errors(true);
        $xml = simplexml_load_string($this->content());
        if($xml === false) {
            $err_msg = json_encode(libxml_get_errors(),JSON_UNESCAPED_UNICODE);
            Log::record("{$err_msg}", Log::ERR);
            libxml_clear_errors();
            throw new Exception("无法解析XML.");
        }

        $url = RESOURCE_SITE_URL . self::img_path;

        $this->mCategories = array();
        foreach($xml->children() as $xitem)
        {
            $item = array();
            $attrs = $xitem->attributes();

            $title = $attrs['title']->__toString();
            $pid = $attrs['id']->__toString();
            $cids = $attrs['cids']->__toString();
            $img = $attrs['img']->__toString();

            $item['name'] = $title;
            $item['hot_id'] = $pid;
            $item['img'] = $url . '/' . $img;

            $item['subitem'] = array();
            $this->add_cids($pid,$cids);

            foreach($xitem->children() as $xsubitem) {
                $subitem = array();
                $sattrs = $xsubitem->attributes();

                $title = $sattrs['title']->__toString();
                $sid = $sattrs['id']->__toString();
                $cids = $sattrs['cids']->__toString();
                $img = $sattrs['img']->__toString();

                $subitem['name'] = $title;
                $subitem['hot_id'] = $sid;
                $subitem['img'] = $url . '/' . $img;

                array_push($item['subitem'],$subitem);

                $this->add_cids($sid,$cids);
                $this->add_cids($pid,$cids);
            }

            array_push($this->mCategories,$item);
        }
    }

    private function add_cids($id,$cids)
    {
        if(!isset($this->mCids[$id])) {
            $this->mCids[$id] = [];
        }
        $cids = explode(',',$cids);
        foreach($cids as $val)
        {
            $cur_cids = &$this->mCids[$id];
            if(!empty($val) && !in_array($val,$cur_cids)) {
                array_push($cur_cids,$val);
            }
        }
    }

    public function categories()
    {
        try {
            return $this->mCategories;
        } catch (Exception $ex) {
            return array();
        }
    }

    public function cids($id)
    {
        try
        {
            if(!isset($this->mCids[$id])) {
                return array();
            } else {
                return $this->mCids[$id];
            }
        } catch (Exception $ex) {
            return array();
        }
    }
    private function content()
    {
        $content = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<category>
  <item title="护肤" id ="1" cids="" img="hf.png">
    <subitem title="洁面" id ="10" cids="50024975,50011977,50448025" img="jm.png" />
    <subitem title="化妆水" id ="11" cids="50012002,50011978,121368014" img="hzs.png" />
    <subitem title="面部精华" id ="12" cids="50011979,121756003" img="mbjh.png" />
    <subitem title="乳液/面霜" id ="13" cids="50011996,50011980,121410035" img="ry.png" />
    <subitem title="眼霜" id ="14" cids="121410013,121408040,121422013,121454013,121472009,121484013,121848006" img="ys.png" />
    <subitem title="面膜" id ="15" cids="121390006,121366015,121426033,121454014,121460030,121468012,121474010" img="mm.png" />
    <subitem title="防晒/隔离" id ="16" cids="121426007,50011982,121434025,121458034" img="fs.png" />
    <subitem title="去角质" id ="17" cids="121384013,50011997,121408009" img="qjz.png" />
  </item>
  <item title="彩妆" id ="2" cids="" img="cz.png">
    <subitem title="卸妆" id ="20" cids="50011990" img="xz.png" />
    <subitem title="BB霜" id ="21" cids="50013794" img="bbs.png" />
    <subitem title="粉底液" id ="22" cids="50010789" img="fdy.png" />
    <subitem title="散粉" id ="23" cids="50010792" img="sf.png" />
    <subitem title="粉饼" id ="24" cids="5001079,50010792" img="fb.png" />
    <subitem title="腮红" id ="25" cids="50010805" img="sh.png" />
    <subitem title="遮瑕" id ="26" cids="50010803" img="zx.png" />
    <subitem title="睫毛膏" id ="27" cids="50010794" img="jmg.png" />
    <subitem title="眼线" id ="28" cids="50010797" img="yx.png" />
    <subitem title="眼影" id ="29" cids="50010796" img="yy.png" />
    <subitem title="眉笔/眉粉" id ="290" cids="50010798" img="mb.png" />
    <subitem title="唇膏/唇彩" id ="291" cids="50010801,50010807,50010808,121392016,121418013,121448009,121456011,121466011,121476007" img="cg.png" />
  </item>
  <item title="个人护理" id ="3" cids="" img="grhl.png">
    <subitem title="洗发水" id ="30" cids="121396029,213202,121404024,121426020,121476023" img="xfs.png" />
    <subitem title="美发护发" id ="31" cids="50023282,50016883,50024999,121410029" img="mfhf.png" />
    <subitem title="头发造型" id ="32" cids="50023294,50023293,50023326" img="tfzx.png" />
    <subitem title="身体护理" id ="33" cids="50012000,50011999,50012001,50012006,50014257,121396013,121412016,121422012,121466009,121478012,121484012" img="sthl.png" />
    <subitem title="手足护理" id ="34" cids="121402008,121390007,121424012,121452007,121456010,121472008,122430002" img="szhl.png" />
    <subitem title="止汗露" id ="35" cids="121392036,121392015" img="zhl.png" />
    <subitem title="沐浴乳/露" id ="36" cids="50014254,213205,50023724,50014248,50014259,50014260,50458018,121386011" img="myr.png" />
    <subitem title="香氛" id ="37" cids="50010815" img="xf.png" />
  </item>
  <item title="男士护肤" id ="4" cids="" img="nshf.png">
    <subitem title="男士洁面" id ="40" cids="121452027" img="nsjm.png" />
    <subitem title="男士爽肤水" id ="41" cids="121392037" img="nssfs.png" />
    <subitem title="男士乳霜" id ="42" cids="121410035" img="nsrs.png" />
    <subitem title="男士护理" id ="43" cids="121366036,50022680,121368010,121386035,121392036,121398029,121404031,121416019,121450036,121470041,121482026" img="nshl.png" />
    <subitem title="男士其它" id ="44" cids="121408040" img="nsqt.png" />
  </item>
</category>
EOD;
        return $content;
    }
}