<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Comment extends Api_Controller{

    //禁言用户组ID
    public $groupids_banned = array(4, 5, 6);
    public function __construct()
    {
        parent::__construct();
        $this->load->model('sharecircle/comment_model', 'comment');
    }

    //发评论/回复
    public function send_comment() {
        $user_groupid = $this->user->checkLogin($this->get_param('auth'));
        if (!$user_groupid) {
            $this->odz_error('User is not login');
        } elseif (in_array($user_groupid, $this->groupids_banned)) {
            $this->odz_error('您已被禁言');
        }
        /*if(!$this->user->checkLogin($this->get_param('auth'))){
            $this->odz_error('User is not login ');
        }*/
        $uid = $this->user->uid;
        $username = $this->user->username;
        $comment = $this->get_param('comment');
        $postid = $this->get_param('postid');
        $to_uid = $this->get_param('to_uid');
        $to_username = $this->get_param('to_username');

        //过滤评论内容
        $this->security =& load_class('Security', 'core');
        $comment = $this->security->xss_clean($comment);

//        $uid = 'xxx';
//        $username = 'xxxxxxx';
//        $comment = 'xxxxxxxxxxxxxxxxx';
//        $postid = 21;
//        $to_uid = 'vvvv';
//        $to_username = 'vvvvvvvvv';

        $ret = array();
        if (empty($comment) || empty($postid)) {
            $this->odz_error('params error');
        }
        $param = array(
            'uid' => $uid,
            'username' => $username,
            'content' => $comment,
            'postid' => $postid,
            'to_uid' => $to_uid,
            'to_username' => $to_username,
            'createtime' => time()
        );
        $new_id = $this->comment->insert($param);

        //添加话题参与记录表，相关话题参与人数+1，页显人数大于0（已使用）时+1
        $this->update_part($postid, $uid, $username);

        //获取设备token用来推送
        $result = array();
        $result['new_id'] = $new_id;
        $device_info = array();
        $this->load->model('user_model');
        $device_info = $this->user_model->get_devicetoken($to_uid);
        if (!empty($device_info)) {
            $result['pushstatus'] = 1;
            $result['devicelist'] = base64_encode(json_encode($device_info));
        }
        $this->odz_result($result, '评论成功');
    }

    //添加话题参与记录表，相关话题人气+1，页显人数+1,参与人数（第一次参与）+1
    //和post.php帖子点赞后调用的update_part方法相同
    public function update_part($postid, $uid, $username) {
        //先获取跟此贴相关的所有话题ID
        $ret = array();
        $tids = array();
        $topic_show = array();
        $this->load->model('sharecircle/topic_post_model', 'topic_post');
        $ret = $this->topic_post->get_tids_by_pid($postid);
        if (!empty($ret)) {
            foreach ($ret as $value) {
                $tids[] = $value['tid'];
            }
            //获取参与过的话题ID
            $this->load->model('sharecircle/topic_participation_model', 'participation');
            $tids_part = $this->participation->check_by_tids($tids, $uid);
            /*//获取这些话题的页显人数
            $topic_show = $this->topic->get_by_tids($tids, 'id, extracount');
            if (!empty($topic_show)) {
                foreach ($topic_show as $value) {
                    $tids_show[$value['id']] = $value['extracount'];
                }
            }*/

            $this->load->model('sharecircle/topic_model', 'topic');
            foreach ($tids as $tid) {
                //人气+1, 页显人数+1
                $param = array(
                    'buzzcount' => 1,
                    'extracount' => 1
                );
                //没参与过的话题，添加用户参与话题记录，话题参与人数+1
                if (!in_array($tid, $tids_part)) {
                    $param['involcount'] = 1;
                    $this->participation->insert(array(
                        'tid' => $tid,
                        'uid' => $uid,
                        'username' => $username,
                        'createtime' => time()
                    ));
                }
                $this->topic->increase_batch($param, $tid);
            }
        }
    }

    public function like() {
        $commentid = $this->input->post('commentid');
        $postid = $this->input->post('postid');
        $uid = $this->input->post('uid');
        $username = $this->input->post('username');
        $to_uid = $this->input->post('to_uid');

        if (empty($commentid) || empty($postid) || empty($uid) || empty($username) || empty($to_uid)) {
            echo $this->json(0, 'params error');
        }

        $this->load->model('sharecircle/comment_like_model', 'comment_like');
        $this->comment_like->like($commentid, $uid, $username);
        $this->comment->increase('likecount', $commentid, 1);

        //添加话题参与记录表，相关话题参与人数+1，页显人数大于0（已使用）时+1
        $this->update_part($postid, $uid, $username);

        //获取设备token用来推送
        $result = array();
        $device_info = array();
        $this->load->model('user_model');
        $device_info = $this->user_model->get_devicetoken($to_uid);
        if (!empty($device_info)) {
            $result['devicelist'] = base64_encode(json_encode($device_info));
        } else {
            $result['devicelist'] = '';
        }
        echo $this->json(1, '成功', $result);
    }



}
