<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/2/26
 * Time: 下午7:36
 */

class MsgStates extends Stackable
{
    public function run() {
    }
}

class StatesHelper
{
    static public function onIndex($states,$msg)
    {
        $type = $msg['type'];
        if($type == 'tags') {
            $states['tags'] = true;
        }
        elseif($type == 'special') {
            $states['special'] = true;
        }
        elseif($type == 'brands') {
            $states['brands'] = true;
        }
        elseif($type == 'fcode') {
            $states['fcode'] = true;
        }
        elseif($type == 'pay_fcode') {
            $states['pay_fcode'] = true;
        }
        elseif($type == 'present_fcode') {
            $states['present_fcode'] = true;
        }
    }
    static public function onGoods($states,$msg)
    {
        $type = $msg['type'];
        if($type == 'common_info') {
            $states['common_info'] = true;
        }
    }

    static public function onActivity($states,$msg)
    {
        $type = $msg['type'];
        if($type == 'full_sent') {
            $states['full_sent'] = true;
        }
        elseif($type == 'groupbuy') {
            $states['groupbuy'] = true;
        }
        elseif($type == 'limit') {
            $states['limit'] = true;
        }
        elseif($type == 'recommend_goods') {
            $states['recommend_goods'] = true;
        }
        elseif($type == 'bundling') {
            $states['bundling'] = true;
        }
        elseif($type == 'recommend_combo') {
            $states['recommend_combo'] = true;
        }
        elseif($type == 'goods_gift') {
            $states['goods_gift'] = true;
        }
        elseif($type == 'optional_goods') {
            $states['optional_goods'] = true;
        }
        elseif($type == 'goods_sampler') {
            $states['goods_sampler'] = true;
        }
    }

    static public function onSearcher($states,$msg)
    {
        $type = $msg['type'];
        if($type == 'init') {
            $states['init'] = true;
        }
    }

    static public function fetch_state($tag)
    {
        global $gMessageStates;
        global $gMessageLock;

        if(empty($gMessageLock) || empty($gMessageStates)) {
            return true;
        }

        $finded = false;
        Mutex::lock($gMessageLock);
        foreach ($gMessageStates as $key => $val)
        {
            if($key == $tag)
            {
                $finded = true;
                $state = $gMessageStates[$tag];
                if($state == true) {
                    $gMessageStates[$tag] = false;
                    break;
                }
            }
        }

        if($finded == false) {
            $gMessageStates[$tag] = false;
            $state = true;
        }
        Mutex::unlock($gMessageLock);

        return $state;
    }

    static public function init()
    {
        global $gMessageStates;
        global $gMessageLock;

        Mutex::lock($gMessageLock);
        $gMessageStates['tags'] = true;
        Mutex::unlock($gMessageLock);
    }
}

function handler_redis($redis, $chan, $msg)
{
    Log::record("message: {$chan} -- {$msg}",Log::DEBUG);

    if (defined('UGC_SUBSCRIBER_PROC') && UGC_SUBSCRIBER_PROC == true)
    {
        if($chan == 'special') {
            UgcHandler::onSpecial($msg);
        }
        elseif($chan == 'follow') {
            UgcHandler::onFellow($msg);
        }
        elseif ($chan == 'inviter') {
            UgcHandler::onInviter($msg);
        }
        else {
            Log::record("message: {$chan} -- {$msg}",Log::DEBUG);
        }
    }
    else
    {
        $cur_trd = Thread::getCurrentThread();
        if(!is_null($cur_trd)) {
            $cur_trd->dispatch($chan,$msg);
        }
    }
}