<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/7/25
 * Time: 下午8:48
 */

namespace ugc;


use bonus\type;
use session_helper;
use bonus_helper;
use predeposit_helper;

class bonus_builder
{
    private $rate;
    private $amount;
    private $num;
    private $sendtype;
    private $bless;
    private $deadline;

    public function __construct($content)
    {
        $this->rate = intval($content['rate']);
        $this->amount = $content['amount'];
        $this->num = $content['num'];
        $this->sendtype = intval($content['bonus_type']);
        $this->bless = $content['bless'];
        $this->deadline = $content['send_end_date'];
    }

    private function fill_param()
    {
        $param['make_type']  = type::MakeVoteType;
        $param['type_name']  = $this->bless;
        $param['type_bless'] = $this->bless;
        $param['send_type']  = $this->sendtype;
        $param['grab_type']  = type::GrabType_All;
        $param['usable_days']  = type::def_usable_days;
        $param['rate_money'][] = ['amount' => $this->amount,'num' => $this->num, 'rate' => $this->rate];
        $param['sender_id']     = $_SESSION['member_id'];
        $param['sender_mobile'] = $_SESSION['member_mobile'];
        $param['sender_name']   = session_helper::nickname();
        $param['bonus_rate']    = $this->rate;
        $param['share_id']      = $_SESSION['cur_share_id'];
        $param['send_end_date'] = $this->deadline;

        return $param;
    }


    public function make()
    {
        $param = $this->fill_param();
        $type = bonus_helper::create_type_input($param);
        $pred = new predeposit_helper($_SESSION['member_id']);

        if(!$pred->person_enough($type->getTotal_amount(),$this->rate)) {
            return false;
        }

        $rate_moneys = [];
        if($type->isFixedAmount()) {
            $item['amount'] = $type->fixed_money();
        } else {
            $item['amount'] = $type->getTotal_amount();
        }
        $item['hold_amount'] = $type->getTotal_amount();
        $item['num'] = $type->getTotal_num();
        $item['rate'] = $this->rate;
        $rate_moneys[] = $item;

        $ret = $pred->make_vote_type($type->get_param(),$rate_moneys);
        if($ret === false) {
            return false;
        }
        else
        {
            $type_sn = $ret['type_sn'];
            return $type_sn;
        }
    }
}

class setting
{
    const pub_type = 0;
    const friend_type = 1;
    const mine_type = 2;

    const day_seconds = 86400;
    private $mSetting;
    private $mSpecial;

    public function __construct($content, special $special)
    {
        $this->mSetting = $content;
        $this->mSpecial = $special;
    }

    private function has_bonus() {
        return ($this->mSetting['bonus'] == true);
    }
    
    private function bonus_param()
    {
        $result['amount'] = $this->mSetting['total_amount'];
        $result['rate']   = $this->mSetting['rate'];
        $result['num']   = 1;
        $result['bonus_type']   = \bonus\type::SendType_Fixed;
        $result['bless'] = $this->mSpecial->bless();

        $deadline = $this->mSpecial->deadline();
        if($deadline <= time()) {
            $deadline = time() + self::day_seconds * 30;
        }
        $result['send_end_date'] = $deadline;

        return $result;
    }
    private function per_amount() {
        return $this->mSetting['per_amount'];
    }

    private function make_bonus()
    {
        if($this->has_bonus())
        {
            $param = $this->bonus_param();
            $builder = new bonus_builder($param);
            $result = $builder->make();
            return $result;
        }
        return false;
    }

    private function submit_rule()
    {
        $type_sn = $this->make_bonus();
        if($type_sn == false) {
            $result['bonus'] = false;
        }
        else {
            $result['bonus'] = true;
            $result['type_sn'] = $type_sn;
            $result['per_amount'] = $this->per_amount();
        }
        return serialize($result);
    }

    public function reader_type()
    {
        if(is_bool($this->mSetting['ispub'])) {
            $type = $this->mSetting['ispub'] ? 0 : 1;
        } else {
            $type = intval($this->mSetting['ispub']);
        }

        return $type;
    }

    public function appreciate() {
        return $this->mSetting['appreciate'] === true ? 1 : 0;
    }
    public function category_id() {
        return intval($this->mSetting['category_id']);
    }

    public function format()
    {
        $result['submit_rule'] = $this->submit_rule();
        $result['reader_type'] = $this->reader_type();
        $result['appreciate']  = $this->appreciate();
        $result['category_id'] = $this->category_id();

        return $result;
    }
}