<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/8/15
 * Time: 下午4:04
 */

namespace activity;

use commonid_helper;
use StatesHelper;
use Log;

class group_item
{
    private $mParam;
    public function __construct($param)
    {
        $this->mParam = null;
        if(isset($param) && is_array($param))
        {
            $this->mParam = $param;
        }
    }
    static private function img_url($value,$store_id)
    {
        return cthumb($value, 360, $store_id);
    }

    public function format()
    {
        $info['groupbuy_id']     = intval($this->mParam['groupbuy_id']);
        $info['groupbuy_name']   = $this->mParam['groupbuy_name'];
        $info['groupbuy_type']   = intval($this->mParam['groupbuy_type']);
        $info['remark']          = $this->mParam['remark'];
        $info['start_time']      = intval($this->mParam['start_time']);
        $info['end_time']        = intval($this->mParam['end_time']);
        $info['goods_id']        = intval($this->mParam['goods_id']);
        $info['promotion_price'] = doubleval($this->mParam['groupbuy_price']);
        $info['rebate']          = doubleval($this->mParam['groupbuy_rebate']);
        $info['quantity']        = intval($this->mParam['virtual_quantity']);
        $info['upper_limit']     = intval($this->mParam['upper_limit']);
        $info['buyer_count']     = intval($this->mParam['buyer_count']);
        $info['groupbuy_intro']  = $this->mParam['groupbuy_intro'];
        $info['groupbuy_image']  = self::img_url($this->mParam['groupbuy_image'],$this->mParam['store_id']);

        return $info;
    }
}

class groupbuy
{
    const GROUPBUY_STATE_REVIEW = 10;
    const GROUPBUY_STATE_NORMAL = 20;
    const GROUPBUY_STATE_REVIEW_FAIL = 30;
    const GROUPBUY_STATE_CANCEL = 31;
    const GROUPBUY_STATE_CLOSE = 32;
    const STORE_ID = 6;
    const cache_name = 'all_groupbuy';

    static private $stInstance = null;
    private $group_list;
    private $mGoodsidGroupIDS;

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

        return self::$stInstance;
    }

    private function init()
    {
        $data = $this->read_cache();
        $this->group_list = $data['list'];
        $this->mGoodsidGroupIDS = $data['ids'];
    }

    private function read_cache()
    {
        $prefix = 'mb_activity';
        $items = rcache(self::cache_name, $prefix);

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

        $group_list = [];
        $goodsidGroupIDS = [];

        $mod = Model('groupbuy');
        $condition = [];
        $condition['store_id'] = self::STORE_ID;
        $condition['state'] = array('in', array(self::GROUPBUY_STATE_CLOSE, self::GROUPBUY_STATE_NORMAL));
        $list = $mod->getGroupbuyList($condition,null,'state asc,groupbuy_type asc');

        foreach ($list as $val)
        {
            $goods_id = intval($val['goods_id']);
            $gid = intval($val['groupbuy_id']);
            $group_list[$gid] = self::filter($val);

            $common_id = commonid_helper::instance()->common_id($goods_id);
            $goods_ids = commonid_helper::instance()->goods_ids($common_id);
            foreach ($goods_ids as $tmp_id) {
                self::add_goods_group($goodsidGroupIDS,$tmp_id,$gid);
            }
        }

        $data = array('list' => $group_list,'ids' => $goodsidGroupIDS);
        wcache(self::cache_name, array('data' => serialize($data)), $prefix);

        return $data;
    }

    private static function add_goods_group(&$goodsidGroupIDS,$goodsid,$gropupid)
    {
        if(isset($goodsidGroupIDS[$goodsid])) {
            $goodsidGroupIDS[$goodsid][] = $gropupid;
        } else {
            $goodsidGroupIDS[$goodsid] = [];
            $goodsidGroupIDS[$goodsid][] = $gropupid;
        }
    }

    static private function filter($groupbuy)
    {
        if(!empty($groupbuy)) {
            unset($groupbuy['class_id']);
            unset($groupbuy['s_class_id']);
            unset($groupbuy['is_vr']);
            unset($groupbuy['vr_city_id']);
            unset($groupbuy['is_vr']);
            unset($groupbuy['vr_area_id']);
            unset($groupbuy['vr_mall_id']);
            unset($groupbuy['vr_class_id']);
            unset($groupbuy['vr_s_class_id']);
        }
        return $groupbuy;
    }

    public function acting()
    {
        $groups = [];
        $curtm = time();
        foreach ($this->group_list as $group)
        {
            $start = intval($group['start_time']);
            $end = intval($group['end_time']);
            if($curtm >= $start && $curtm < $end) {
                $groups[] = $group;
            }
        }

        $result = [];
        foreach ($groups as $val) {
            $item = new group_item($val);
            $result[] = $item->format();
        }

        return $result;
    }

    public function unstart()
    {
        $groups = [];
        $curtm = time();
        foreach ($this->group_list as $group)
        {
            $start = intval($group['start_time']);
            if($curtm < $start) {
                $groups[] = $group;
            }
        }

        $result = [];
        foreach ($groups as $val) {
            $item = new group_item($val);
            $result[] = $item->format();
        }

        return $result;
    }

    public function goods($group_id)
    {
        if(isset($this->group_list[$group_id])) {
            $goods_id = intval($this->group_list[$group_id]['goods_id']);
            return array($goods_id);
        } else {
            return array();
        }
    }

    public function promotion_price($act_id,&$price)
    {
        $act_id = intval($act_id);
        if(isset($this->group_list[$act_id])) {
            $param = $this->group_list[$act_id];
            $price = doubleval($param['groupbuy_price']);
            return true;
        } else {
            return false;
        }
    }

    public function isTakepart($goods_id,&$act_id)
    {
        if(isset($this->mGoodsidGroupIDS[intval($goods_id)]))
        {
            $group_ids = $this->mGoodsidGroupIDS[$goods_id];
            foreach ($group_ids as $gid)
            {
                $start  = intval($this->group_list[$gid]['start_time']);
                $end    =  intval($this->group_list[$gid]['end_time']);

                $cur = time();
                if($cur >= intval($start) && $cur < intval($end)) {
                    $act_id = $gid;
                    return true;
                }
            }
            return false;
        }
        else {
            return false;
        }
    }

    public function get_info($act_id)
    {
        $act_id = intval($act_id);
        if(isset($act_id,$this->group_list[$act_id])) {
            $param = $this->group_list[$act_id];
            $item = new group_item($param);
            return $item->format();
        } else {
            return false;
        }
    }
}