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

namespace async;

use member_info;
use predeposit_helper;

abstract class filter
{
    public function __construct()
    {
    }

    public abstract function filtrate(member_info $minfo);
    public static function create($params)
    {
        $result = [];
        foreach ($params as $param)
        {
            $type = $param['type'];

            if($type == 'bonus') {
                $filter = new bonus_filter($param);
            }
            elseif($type == 'regtime') {
                $filter = new regtime_filter($param);
            }
            elseif($type == 'mobile') {
                $filter = new mobile_filter();
            }
            else {
                return null;
            }
            $result[] = $filter;
        }

        return $result;
    }

    protected function usable_bonus($member_id)
    {
        $pred = new predeposit_helper($member_id);
        return $pred->total_bonus();
    }
}

class regtime_filter extends filter
{
    private $mIncludeTime;
    public function __construct($param)
    {
        $days = intval($param['days']);
        if($days < 0) {
            $this->mIncludeTime = 0;
        } else {
            $this->mIncludeTime = time() - $days * 86400;
        }
        parent::__construct();
    }

    public function filtrate(member_info $minfo)
    {
        return $minfo->reg_time() > $this->mIncludeTime;
    }
}

class bonus_filter extends filter
{
    private $mMaxAmount;

    public function __construct($param)
    {
        $this->mMaxAmount = intval($param['amount']);
        parent::__construct();
    }

    public function filtrate(member_info $minfo)
    {
        $member_id = $minfo->member_id();
        $bonus_amount = $this->usable_bonus($member_id);
        return $bonus_amount < $this->mMaxAmount;
    }
}

class app_filter extends filter
{
    public function __construct()
    {
        parent::__construct();
    }

    public function filtrate(member_info $minfo)
    {
        return $minfo->is_app();
    }
}

class notapp_filter extends filter
{
    public function __construct()
    {
        parent::__construct();
    }

    public function filtrate(member_info $minfo)
    {
        return !$minfo->is_app();
    }
}

class mobile_filter extends filter
{
    private $mIncludes;
    public function __construct()
    {
        $this->mIncludes = [];
        $this->mIncludes[] = '13911129867';
        parent::__construct();
    }

    public function filtrate(member_info $minfo)
    {
        $mobile = $minfo->mobile();
        return in_array($mobile,$this->mIncludes);
    }
}

class order_filter extends filter
{
    private $mod_order;
    public function __construct()
    {
        $this->mod_order = Model('order');
        parent::__construct();
    }

    public function filtrate(member_info $minfo)
    {
        $mobile = $minfo->mobile();
        return in_array($mobile,$this->mIncludes);
    }
}
