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

namespace async;

use search;
use push_helper;
use bonus;
use bonus_helper;
use member_info;
use Log;
use Exception;

class register extends IAsync
{
    private $cur_user;
    private $cur_info;
    private $last_inviter;

    public function __construct($user,$inviter)
    {
        $this->cur_user = $user;
        $this->cur_info = new member_info($user);
        if($this->cur_info->wxauthored() == false) {
            throw new Exception('为防止欺诈，需要微信授权登录');
        }

        $this->last_inviter = $inviter;
        $this->add_invitee($inviter);
    }

    private function inviters()
    {
        if($this->last_inviter <= 0){
            return [];
        }
        else
        {
            $inviters = [];
            $parent_inviters = search\relation_client::instance()->fetch_inviters(['user_id' => $this->last_inviter]);
            $inviters[] = $this->last_inviter;
            foreach ($parent_inviters as $inviter) {
                $inviters[] = $inviter;
            }
            return $inviters;
        }
    }

    public function run()
    {
        $this->reward_user();
        $this->tag_rewarded($this->cur_user);

        $inviters = $this->inviters();
        return $this->notify_inviter($inviters);
    }

    private function notify_inviter($inviters)
    {
        if(empty($inviters)) return false;

        $level = 0;
//        $ex_invitee = $this->cur_user;
        foreach ($inviters as $inviter)
        {
            $this->reward_inviter($inviter,$level);
//            $invitees = search\relation_client::instance()->fetch_invitees(['user_id' => $inviter]);
//            $invitees = $this->ex_user($invitees,$ex_invitee);
//            $this->reward_invitees($inviter,$invitees,$level);
//            $ex_invitee = $inviter;
            $level += 1;
        }
        return true;
    }

    //给Level 级的邀请者发奖励
    private function reward_inviter($inviter, $level)
    {
        $params = bonus\parameters::guide_inviter($this->cur_info->nickname(),$level);
        foreach ($params as $param)
        {
            $type = bonus_helper::create_type_input($param);
            $money = $type->getTotal_amount();
            $rate  = $type->bonus_rate();
            $ret = bonus_helper::make_bonus($param,$param['rate_money']);

            if($ret != false)
            {
                $type_sn = $ret['type_sn'];
                $fSuccess = bonus_helper::send($type_sn,[$inviter]);
                if($fSuccess == false) {
                    Log::record("send bonus error: type_sn={$type_sn}",Log::DEBUG);
                }
                else {
                    push_helper::inviter_bonus($inviter,$this->cur_user,$level + 1,$money,$rate,$type_sn);
                    $this->add_reward($inviter,$money);
                }
            }
            else {
                return false;
            }
        }
        return true;
    }

    private function reward_invitees($inviter, $invitees, $level)
    {
        if(empty($invitees)) return false;
        $inviter_info = new member_info($inviter);

        $num = count($invitees);
        $params = bonus\parameters::guide_invitee($inviter_info->nickname(),$this->cur_info->nickname(),$level,$num);
        foreach ($params as $param)
        {
            $type = bonus_helper::create_type_input($param);
            $money = $type->getTotal_amount();
            $rate  = $type->bonus_rate();
            $ret = bonus_helper::make_bonus($param,$param['rate_money']);

            if($ret != false)
            {
                $type_sn = $ret['type_sn'];
                bonus_helper::send($type_sn,$invitees);

                $i = 0;
                foreach ($invitees as $invitee)
                {
                    $amount = $money / $num;
                    $this->add_reward($invitee,$amount);
                    if($i < self::max_level_pushs) {
                        push_helper::invitee_bonus($inviter, $invitee, $this->cur_info, $level + 1, $amount, $rate, $type_sn);
                    }
                    $i++;
                }
            } else {
                return false;
            }
        }
        return true;
    }

    private function reward_user()
    {
        $params = bonus\parameters::user_reigster_fixed();
        foreach ($params as $param)
        {
            $type = bonus_helper::create_type_input($param);
            $money = $type->getTotal_amount();
            $ret = bonus_helper::make_bonus($param,$param['rate_money']);

            if($ret != false) {
                $type_sn = $ret['type_sn'];
                bonus_helper::send($type_sn,[$this->cur_user]);
                push_helper::register_bonus($this->cur_user,$money,$type_sn);
            } else {
                return false;
            }
        }

        return true;
    }
}