<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2016/10/19
 * Time: 下午4:45
 */

require_once(BASE_ROOT_PATH . '/helper/activity_helper.php');
require_once(BASE_ROOT_PATH . '/helper/goods_helper.php');
require_once(BASE_ROOT_PATH . '/helper/special_helper.php');

class act_formater
{
    private $mActivities;
    private $mGoodsActContainer;

    public function __construct($activities)
    {
        $this->mActivities = $activities;
        $this->mGoodsActContainer = [];
    }

    public function format_acting()
    {
        $specials = [];
        $goods_ids = [];

        $cur_type = -1;
        foreach ($this->mActivities as $act)
        {
            $gids =[];
            $block = false;

            if(array_key_exists('xianshi_id',$act))
            {
                $block = $this->format_limit($act,$gids);
                if($block != false) {
                    $time_type = intval($act['xianshi_type']);
                }
            }
            elseif(array_key_exists('groupbuy_id',$act))
            {
                $block = $this->format_groupbuy($act,$gids);
                if($block != false) {
                    $time_type = intval($act['groupbuy_type']);
                }
            }
            else {
                $time_type = false;
                Log::record("an error activity.",Log::ERR);
            }

            if($block != false)
            {
                if($time_type !== false)
                {
                    if($cur_type != $time_type)
                    {
                        $banners = $this->type_banner($time_type);
                        if(!empty($banners))
                        {
                            foreach ($banners as $banner) {
                                $specials[] = $banner;
                            }
                        }
                        $cur_type = $time_type;
                    }
                }

                $specials[]= $block;
                self::array_copy($goods_ids,$gids);
            }
        }

        $goods_ids = array_unique($goods_ids);
        if(!empty($goods_ids))
        {
            $helper = new goods_helper();
            $ret = $helper->online_summary($goods_ids,$related);

            return array('special_list' => $specials,
                'summary' => $ret['summary'],
                'groupbuy' => $ret['groupbuy'],
                'limitime' => $ret['limitime'],
                'bundling' => $ret['bundling'],
                'mobile_page' => mobile_page(1));
        }
        else {
            return array('special_list' => null,
                'summary' => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page(1));
        }
    }

    public function format_unstart()
    {
        $specials = [];
        $goods_ids = [];

        $group_ids = [];
        $limit_ids = [];

        $cur_type = -1;
        foreach ($this->mActivities as $act)
        {
            $gids = [];
            $block = false;
            if(array_key_exists('xianshi_id',$act))
            {
                $block = $this->format_limit($act,$gids);
                if($block != false) {
                    $limit_ids[] = intval($act['xianshi_id']);
                    $time_type = intval($act['xianshi_type']);
                }
            }
            elseif(array_key_exists('groupbuy_id',$act))
            {
                $block = $this->format_groupbuy($act,$gids);
                if($block != false) {
                    $group_ids[] = intval($act['groupbuy_id']);
                    $time_type = intval($act['groupbuy_type']);
                }
            }
            else {
                $time_type = false;
                Log::record("an error activity.",Log::ERR);
            }

            if($block != false)
            {
                if($time_type !== false)
                {
                    if($cur_type != $time_type)
                    {
                        $banners = $this->type_banner($time_type);
                        if(!empty($banners))
                        {
                            foreach ($banners as $banner) {
                                $specials[] = $banner;
                            }
                        }
                        $cur_type = $time_type;
                    }
                }

                $specials[]= $block;
                self::array_copy($goods_ids,$gids);
            }
        }

        $goods_ids = array_unique($goods_ids);
        if(!empty($goods_ids))
        {
            $helper = new goods_helper();
            $ret = $helper->online_summary($goods_ids,$related);

            $summarys = $ret['summary'];
            foreach ($summarys as &$summary)
            {
                $goods_id = $summary['goods_id'];
                $act = $this->mGoodsActContainer[$goods_id];
                $summary['act_id'] = $act['act_id'];
                $summary['act_type'] = $act['act_type'];
                if($act['act_type'] == activity_helper::ACTIVITY_GROUPBUY) {
                    $ret = activity_helper::groupbuy_price($act['act_id'],$price);
                } else {
                    $ret = activity_helper::limit_price($act['act_id'],$goods_id,$price);
                }

                if($ret == true) {
                    $summary['goods_promotion_price'] = $price;
                } else {
                    Log::record("can not get : goods_promotion_price",Log::ERR);
                }
            }

            $groups = [];
            foreach ($group_ids as $group_id)
            {
                $item = activity\groupbuy::instance()->get_info($group_id);
                if($item != false) {
                    $groups[] = $item;
                } else {
                    Log::record("cannot get group id={$group_id}",Log::ERR);
                }
            }

            $limits = [];
            foreach ($limit_ids as $limit_id)
            {
                $item = activity\limitime::instance()->get_info($limit_id);
                if($item != false) {
                    $limits[] = $item;
                } else {
                    Log::record("cannot get group id={$limit_id}",Log::ERR);
                }
            }

            return array('special_list' => $specials,
                'summary' => $summarys,
                'groupbuy' => $groups,
                'limitime' => $limits,
                'bundling' => $ret['bundling'],
                'mobile_page' => mobile_page(1));
        }
        else
        {
            return array('special_list' => null,
                'summary' => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page(1));
        }
    }

    private function type_banner($time_type)
    {
        static $stTimeBanner = array(12 => 345,20 => 347,24 => 348);
        //static $stTimeBanner = array(12 => 60);

        if(array_key_exists($time_type,$stTimeBanner))
        {
            $banners = special_manager::instance()->special($stTimeBanner[$time_type],$gids);
            if(empty($banners))
                return null;
            else
                return $banners;
        }
        return null;
    }

    private function format_limit($limt, &$goods_ids)
    {
        $result =[];

        $limt_id = intval($limt['xianshi_id']);
        $result['item_title'] = strval($limt_id);
        $result['item_type'] = 'home_limit';

        $goods_ids = activity_helper::limit_goods($limt_id);
        if(empty($goods_ids)) return false;
        foreach ($goods_ids as $gid) {
            $item['type'] = 'goods';
            $item['data'] = strval($gid);
            $item['image'] = '';
            $item['title'] = '';
            $result['items'][] = $item;

            $this->mGoodsActContainer[$gid] = array('act_id' => $limt_id,
                'act_type' => activity_helper::ACTIVITY_LIMITTIME);
        }

        return $result;
    }

    private function format_groupbuy($group, &$goods_ids)
    {
        $result =[];

        $act_id = intval($group['groupbuy_id']);
        $result['item_title'] = strval($act_id);
        $result['item_type'] = 'home_group';

        $goods_ids = activity_helper::groupbuy_goods($act_id);
        if(empty($goods_ids)) return false;
        foreach ($goods_ids as $gid) {
            $item['type'] = 'goods';
            $item['data'] = strval($gid);
            $item['image'] = '';
            $item['title'] = '';

            $result['items'][] = $item;
            $this->mGoodsActContainer[$gid] = array('act_id' => $act_id,
                'act_type' => activity_helper::ACTIVITY_GROUPBUY,
                'goods_promotion_price' => $group['promotion_price']);
        }

        return $result;
    }

    private static function array_copy(&$dest,$src)
    {
        if(empty($src)) return $dest;
        foreach ($src as $value) {
            array_push($dest,$value);
        }
        return $dest;
    }
}


class limit_outer
{
    const WAIT_START = 0;
    const WAIT_END = 1;

    private $mAct;
    private $mType;
    public function __construct($type,$act)
    {
        $this->mAct = $act;
        $this->mType = $type;
    }

    public function format()
    {
        if(array_key_exists('xianshi_id',$this->mAct)) {
            $result = $this->format_limit();
        }
        elseif(array_key_exists('groupbuy_id',$this->mAct)) {
            $result = $this->format_group();
        }
        else {
            $result = false;
        }

        return $result;
    }
    private function format_limit()
    {
        $result = [];

        $act = $this->mAct;

        $act_id = $act['xianshi_id'];
        $gids = activity_helper::limit_goods($act_id);
        if(empty($gids)) return false;

        $goods_ids = array($gids[0]);
        $helper = new goods_helper();
        $ret = $helper->online_summary($goods_ids,$related);

        if(!empty($ret['summary']))
        {
            $summary = $ret['summary'][0];
            if($this->mType == limit_outer::WAIT_START) {
                $result['act_time'] = $act['start_time'];
            } else {
                $result['act_time'] = $act['end_time'];
            }

            $result['goods_name'] = $summary['goods_mobile_name'];
            $result['goods_price'] = $summary['goods_price'];
            $result['goods_promotion_price'] = $summary['goods_promotion_price'];
            $result['goods_image_url'] = $summary['goods_image_url'];
            $result['local_time'] = time();

            return $result;
        }
        else {
            return false;
        }

    }
    private function format_group()
    {
        $result = [];

        $act = $this->mAct;
        $act_id = $act['groupbuy_id'];
        $gids = activity_helper::groupbuy_goods($act_id);
        if(empty($gids)) return false;

        $goods_ids = array($gids[0]);
        $helper = new goods_helper();
        $ret = $helper->online_summary($goods_ids,$related);

        if(!empty($ret['summary']))
        {
            $summary = $ret['summary'][0];
            if($this->mType == limit_outer::WAIT_START) {
                $result['act_time'] = $act['start_time'];
            } else {
                $result['act_time'] = $act['end_time'];
            }

            $result['goods_name'] = $summary['goods_mobile_name'];
            $result['goods_price'] = $summary['goods_price'];
            $result['goods_promotion_price'] = $act['promotion_price'];
            $result['goods_image_url'] = $summary['goods_image_url'];
            $result['local_time'] = time();

            return $result;
        }
        else {
            return false;
        }
    }
}

class activityControl extends mobileControl
{
    public function __construct()
    {
        parent::__construct();
    }

    public function actingOp()
    {
        $actings = activity_helper::acting();

        $formater = new act_formater($actings);
        $result = $formater->format_acting();

        return self::outsuccess($result);
    }
    public function unstartOp()
    {
        $actings = activity_helper::unstart();
        $formater = new act_formater($actings);
        $result = $formater->format_unstart();

        return self::outsuccess($result);
    }

    public function limit_entraOp()
    {
        $_SESSION['client_type'] = "wap";

        $acts = activity_helper::acting();
        if(!empty($acts)) {
            $act = $acts[0];
            $outer = new limit_outer(limit_outer::WAIT_END,$act);
            $result = $outer->format();
            if($result != false) {
                return self::outsuccess($result,"activity/limit_entra");
            }
        }

        $unstarts = activity_helper::unstart();
        if(!empty($unstarts)) {
            $act = $unstarts[0];
            $outer = new limit_outer(limit_outer::WAIT_START,$act);
            $result = $outer->format();
            if($result != false) {
                return self::outsuccess($result,"activity/limit_entra");
            }
        }

        return self::outsuccess(null,"activity/limit_entra");
    }
}
