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

namespace async;

use message;
use search;
use ugc;
use ugc_helper;
use push_helper;

class qugc extends IAsync
{
    private $special;
    private $user;
    private $special_id;

    public function __construct($special_id)
    {
        $mod_special = Model('mb_special');
        $info = $mod_special->getMbSpecialByID($special_id,'*',true);
        $this->special = new ugc\special($info);
        $this->user = $this->special->memberid();
        $this->special_id = $special_id;
    }

    public function onPublish()
    {
        $read_type = $this->special->reader_type();
        if($read_type != ugc_helper::private_self) {
            $publisher = new message\publisher();
            $publisher->add_special($this->user,array(['type' => $read_type,'spid' => $this->special_id]));
            $follows = search\relation_client::instance()->fetch_follow(['user_id' => $this->user]);
            $this->nofity_pub($follows);
        }
    }

    private function nofity_pub($follows)
    {
        if(empty($follows)) return false;

        $title = $this->special->share_title();
        $this->ex_user($follows,$this->user);

        $i = 0;
        foreach ($follows as $follow)
        {
            if($i < self::max_level_pushs) {
                push_helper::nofity_ugc_pub($follow, $this->user, $this->special_id, $title);
            }
            $i++;
        }
    }

    public function onDel()
    {
        $read_type = $this->special->reader_type();
        $publisher = new message\publisher();
        $publisher->del_special($this->user,array(['type' => $read_type,'spid' => $this->special_id]));
    }

    public function onSubmit($reader)
    {
        $reader = intval($reader);
        if($reader > 0 && $this->user != $reader) {
            $title = $this->special->share_title();
            push_helper::nofity_ugc_submit($this->user,$reader,$this->special_id,$title);
        }
    }

    public function onSupport($comment_id,$user)
    {
        $content = null;
        if($comment_id > 0)
        {
            $mod = Model('ugc_comment');
            $comments = $mod->getCommentList(['comment_id' => $comment_id],'*',0);
            if(!empty($comments)) {
                $author = intval($comments[0]['user_id']);
                $content = $comments[0]['content'];
                $fcomment = true;
            }
            else {
                return false;
            }
        }
        else {
            $author = $this->user;
            $fcomment = false;
        }

        if($author != $user) {
            push_helper::nofity_ugc_support($author,$user,$fcomment,$this->special_id,$content,$this->special->share_title());
        }

        return true;
    }

    public function onComment($comment_id,$user)
    {
        if($user <= 0) return false;

        $content = null;
        if($comment_id > 0)
        {
            $mod = Model('ugc_comment');
            $comments = $mod->getCommentList(['comment_id' => $comment_id],'*',0);
            if(!empty($comments)) {
                $author = intval($comments[0]['user_id']);
                $content = $comments[0]['content'];
                $fcomment = true;
            }
            else {
                return false;
            }
        }
        else {
            $author = $this->user;
            $fcomment = false;
        }

        if($author != $user) {
            push_helper::nofity_ugc_comment($author,$user,$fcomment,$this->special_id,$content,$this->special->share_title());
        }

        return true;
    }

    public function onAppreciate($user,$rate,$amount)
    {
        if($user <= 0) return false;
        $author = $this->special->memberid();
        if($author != $user) {
            push_helper::nofity_ugc_appreciate($author,$user,$this->special_id,$this->special->share_title(),$rate,$amount);
        }
        return true;
    }

    public function run()
    {
    }
}