<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/7/19
 * Time: 下午3:16
 */

namespace ugc;

use algorithm;

class comments
{
    const max_response = 11;

    private $counter;
    private $users;
    private $comments;
    private $special_id;

    public function __construct($comments,$special_id,$comment_subcounts)
    {
        $this->counter = [];
        $this->users = [];
        $this->comments = [];
        $this->special_id = intval($special_id);

        $this->parse($comments,$comment_subcounts);
    }

    public function users() {
        return $this->users;
    }
    public function comments() {
        return $this->comments;
    }

    private function parse($comments,$comment_subcounts)
    {
        $comment_map = [];
        foreach ($comments as $comment) {
            $comment_id = intval($comment['comment_id']);
            $comment_map[$comment_id] = $comment;
        }

        foreach ($comments as $comment)
        {
            $response_id = intval($comment['response_id']);
            $top_id = intval($comment['top_id']);

            if (!array_key_exists($top_id,$this->counter)) {
                $this->counter[$top_id] = 0;
            }

            if($this->counter[$top_id] < self::max_response)
            {
                if($response_id != 0)
                {
                    if(array_key_exists($response_id,$comment_map)) {
                        $resp_comment = $comment_map[$response_id];
                        $to_user = intval($resp_comment['user_id']);
                    }
                    else
                    {
                        $mod_comment = Model('ugc_comment');
                        $resp_comment = $mod_comment->getCommentByID($response_id);
                        if(!empty($resp_comment)) {
                            $this->add_user($resp_comment);
                            $to_user = intval($resp_comment['user_id']);
                        } else {
                            $to_user = 0;
                        }
                    }
                }
                else {
                    $to_user = 0;
                }

                $comment_id = intval($comment['comment_id']);
                if(array_key_exists($comment_id,$comment_subcounts)) {
                    $sub_count = $comment_subcounts[$comment_id];
                } else {
                    $sub_count = 0;
                }

                $this->counter[$top_id] += 1;
                $this->comments[] = $this->format_comment($comment,$to_user,$sub_count);
                $this->add_user($comment);
            }
        }
    }

    private function add_user($comment)
    {
        $user_id = intval($comment['user_id']);
        if(!algorithm::binary_search($this->users,$user_id)) {
            $pos = algorithm::lower_bonud($this->users,$user_id);
            algorithm::array_insert($this->users,$pos,$user_id);
        }
    }
    private function format_comment($comment,$to_user,$sub_count)
    {
        $result['comment_id'] = $comment['comment_id'];
        $result['to_user'] = $to_user;
        $result['content'] = $comment['content'];
        $result['sub_comments'] = $comment['sub_comments'];
        $result['likes'] = $comment['likes'];
        $result['addtime'] = $comment['addtime'];
        $result['user_id'] = $comment['user_id'];
        $result['response_id'] = $comment['response_id'];
        $result['sub_count'] = $sub_count;

        $comment_id = intval($comment['comment_id']);
        $supporter = new special_support($this->special_id,$comment_id);
        $result['supported'] = $supporter->supported();

        return $result;
    }
}