<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/4/11
 * Time: 上午12:51
 */

require_once (BASE_ROOT_PATH . '/helper/field_helper.php');
require_once (BASE_ROOT_PATH . '/helper/predeposit_helper.php');
require_once (BASE_ROOT_PATH . '/helper/ranklist_helper.php');
require_once (BASE_ROOT_PATH . '/helper/text_filter.php');

require_once (BASE_ROOT_PATH . '/helper/bonus/util.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/type.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/user_bonus.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/generator.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/grab.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/factory.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/manager.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/shaker.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/allocator.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/witholder.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/open_sharer.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/scaler.php');
require_once (BASE_ROOT_PATH . '/helper/bonus/thief_vilator.php');


class bonus_helper
{
    const def_bless = '熊猫美妆与你分享美丽的秘密～';
    const send_bonus_withold = 1;
    const pay_order_withold  = 2;

    static public function filter_type($type_info)
    {
        $type = \bonus\type::create_by_paramer($type_info);
        $ret = $type->get_param();
        $ret['time_out'] = \bonus\manager::grab_period_timeout;
        if($type->binded_over()) {
            $ret['binded_over'] = 1;
        } else {
            $ret['binded_over'] = 0;
        }
        if($type->isEnd()) {
            $ret['is_end'] = 1;
        } else {
            $ret['is_end'] = 0;
        }

        $type_sn = $type->getType_sn();
        $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
        $ret['url'] = $url;

        $detail_url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=detail&client_type=wap&type_sn={$type_sn}";
        $ret['detail_url'] = $detail_url;

        return $ret;
    }

    static public function filter_bonus($bonus_info)
    {
        if(empty($bonus_info['user_name'])) {
            $bonus_info['user_name'] = substr_replace($bonus_info['user_mobile'], '****', 3, 4);
        }
        return $bonus_info;
    }

    static public function isFixed($send_type) {
        return ($send_type == \bonus\type::SendType_Fixed);
    }
    static public function isRandom($send_type) {
        return ($send_type == \bonus\type::SendType_Random);
    }

    static public function create_type_input($param) {
        return \bonus\type::crate_by_input($param);
    }
    static public function create_by_paramer($param) {
        return \bonus\type::create_by_paramer($param);
    }
    static public function create_type_sn($type_sn) {
        return \bonus\type::create_by_sn($type_sn);
    }

    static public function make_bonus($param, $rate_moneys)
    {
        $ret = \bonus\factory::make_bonus($param,$rate_moneys);
        return $ret;
    }
    static public function make_vote_type($param, $rate_moneys)
    {
        $ret = \bonus\factory::make_vote_type($param,$rate_moneys);
        return $ret;    }

    static public function last_invite_type($member_id)
    {
        $mod = Model('bonus_type');
        $items = $mod->get(array('relayer_id' => $member_id, 'make_type' => \bonus\type::MakeInviteType));
        if(empty($items)) {
            return null;
        } else {
            return $items[0];
        }
    }

    static public function get_typeinfo($type_sn)
    {
        $manager = new \bonus\manager();
        return $manager->get_typeinfo($type_sn);
    }

    static public function get_mine_by_typesn($type_sn) {
        $manager = new \bonus\manager();
        return $manager->get_mine_by_typesn($type_sn);
    }

    static public function get_mine_by_bonussn($bonus_sn) {
        $manager = new \bonus\manager();
        return $manager->get_mine_by_bonussn($bonus_sn);
    }

    static public function grab_bonus($type_sn)
    {
        $manager = new \bonus\manager();
        return $manager->grab_bonus($type_sn);
    }

    static public function bind_bonus($bonus_sn,$session_id,$mobile,&$new_bonus_sn)
    {
        try
        {
            $manager = new \bonus\manager();
            return $manager->bind_bonus($bonus_sn,$session_id,$mobile,$new_bonus_sn);
        } catch (Exception $ex) {
            return array($ex->getCode(),$ex->getMessage());
        }
    }
    static public function shake($bonus_sn,$strength,$direction)
    {
        $manager = new \bonus\manager();
        return $manager->shake($bonus_sn,$strength,$direction);
    }

    static public function comment($bonus_sn,$comment)
    {
        try
        {
            $manager = new \bonus\manager();
            return $manager->comment($bonus_sn,$comment);
        } catch (Exception $ex) {
            return false;
        }
    }

    static public function topup_bonus(predeposit_helper $pred,$mobile)
    {
        $mod_bonus = Model('user_bonus');
        $bind_bonus = $mod_bonus->getBinded($mobile);
        if(empty($bind_bonus)) {
            return false;
        }

        $manager = new \bonus\manager();
        $bonusex = array();
        foreach($bind_bonus as $val)
        {
            $type_id = $val['type_id'];
            $type  = \bonus\type::create_by_id($type_id);
            $bonus = \bonus\user_bonus::create_by_param($val);

            try
            {
                if($manager->topup($type,$mod_bonus,$val) == true) {
                    array_push($bonusex,$val);
                    ranklist_helper::add_money($_SESSION['member_id'],$bonus->bonus_value());
                    $pred->add_bonus($bonus,$type);
                }
            } catch (Exception $ex) {
            }
        }

        if(empty($bonusex)) {
            return false;
        } else {
            return $bonusex;
        }
    }

    static public function direct_asc() {
        return \bonus\shaker::direct_asc;
    }
    static public function direct_dec() {
        return \bonus\shaker::direct_dec;
    }

    static public function get_direction($first)
    {
        if($first) {
            return self::direct_asc();
        }
        else
        {
            $direct = mt_rand(1,100);
            if($direct % 2 == 0) {
                return self::direct_asc();
            } else {
                return self::direct_dec();
            }
        }
    }

    static public function check_shake($bonus_sn,&$first)
    {
        static $shake_expire   = 5;
        static $shake_maxcount = 5;

        if(!isset($_SESSION['bonus_shake'])) {
            $_SESSION['bonus_shake'] = [];
        }

        if(isset($_SESSION['bonus_shake'][$bonus_sn]))
        {
            $first = false;
            $bonus_shake = &$_SESSION['bonus_shake'][$bonus_sn];

            $tmout = time() - $bonus_shake['time'];
            if($tmout <= $shake_expire) {
                return array('code' => errcode::ErrBonus, 'msg' => '请不要摇得太快~');
            } elseif($bonus_shake['count'] > $shake_maxcount) {
                ++$bonus_shake['count'];
                return array('code' => errcode::ErrBonus, 'msg' => '该红包只允许被摇' . $shake_maxcount .'次~');
            } else {
                return true;
            }
        }
        else
        {
            $_SESSION['bonus_shake'][$bonus_sn] = [];
            $bonus_shake = &$_SESSION['bonus_shake'][$bonus_sn];
            $bonus_shake['count'] = 1;
            $bonus_shake['time'] = time();
            $first = true;

            return true;
        }
    }

    static public function withold($member_id,$rates,$used_type)
    {
        $fsuccess =  true;
        $total = 0.00;
        $holder = new \bonus\witholder($member_id,$used_type);
        foreach ($rates as $rate => $money)
        {
            if($holder->withold($rate,$money) == true) {
                $total += $money;
            } else {
                $fsuccess = false;
            }
        }

        if($fsuccess) {
            return true;
        }
        else {
            return $total;
        }
    }

    static public function withold_money($member_id,$rate,$money,$used_type)
    {
        $holder = new \bonus\witholder($member_id,$used_type);
        if($holder->withold($rate,$money) == false) {
            Log::record("withold user member_id={$member_id} money={$money}.",Log::ERR);
        }
        return true;
    }

    static public function withold_bonus($member_id,$bonus_sn,$rate,$money,$used_type)
    {
        $holder = new \bonus\witholder($member_id,$used_type);
        return $holder->withold_bonus($rate,$bonus_sn,$money);
    }

    static public function send($type_sn,$member_ids)
    {
        try
        {
            $manager = new \bonus\manager();
            return $manager->send($type_sn,$member_ids);
        } catch (Exception $ex) {
            return false;
        }
    }
    static public function send_mobile($type_sn, $mobiles,$status)
    {
        try
        {
            $manager = new \bonus\manager();
            return $manager->send_mobile($type_sn,$mobiles,$status);
        } catch (Exception $ex) {
            return false;
        }
    }
    static public function get_share($share_id = 0) {
        return \bonus\open_sharer::instance()->get($share_id);
    }

    static public function can_grab(\bonus\type $type,$member_id,$mobile)
    {
        $grab_type = $type->grab_type();
        if($grab_type == \bonus\type::GrabType_All) {
            return true;
        }
        elseif($grab_type == \bonus\type::GrabType_EXSENDER)
        {
            if($type->sender_id() == $member_id) {
                return false;
            }
            if($type->sender_mobile() == $mobile) {
                return false;
            }
            return true;
        }
        else
        {
            $member_id = intval($member_id);
            if($member_id > 0 && !empty($mobile)) {
                $cond['member_id|member_mobile'] = array('_multi'=>true,$member_id,$mobile);
            }
            elseif($member_id > 0) {
                $cond['member_id'] = $member_id;
            }
            elseif(!empty($mobile)) {
                $cond['member_mobile'] = $mobile;
            }
            else {
                return true;
            }

            $items = Model('member')->getMemberInfo($cond);
            return (empty($items) == true);
        }
    }

    static public function match_price($rate_moneys)
    {
        if(empty($rate_moneys)) return false;
        $scaler = new \bonus\scaler($rate_moneys);
        return $scaler->calc();
    }

}