<?php

class Comment extends MY_Controller
{
    private $policy;

    public function __construct()
    {
        parent::__construct();
        $this->policy = [
            'anyone' => true
        ];
    }

    public function index()
    {
        if ($this->authorization()) {
            $this->load->model('comment_model');
            $this->{$this->uri->segment(2)}();
        } else {
            $this->response(array('code' => 405, 'error' => '로그인 하세요'));
        }
    }

    private function authorization()
    {
        $resource = $this->uri->segment(2);
        $comment = $this->input->post('comment');
        if ($resource == 'new' || $resource == 'reply') {
            return (mb_strlen($comment, 'UTF-8') <= 10 && $this->policy['anyone']) || getSessionUser()->is;
        } else if ($resource == 'delete') {
            return getSessionUser()->is;
        }
        return true;
    }

    private function replies()
    {
        $commentId = $this->uri->segment(3);
        $replies = $this->comment_model->getRepliesByCommentId($commentId)
            ->join('cm_user', 'cm_comment.userID=cm_user.userID', 'left')
            ->select('*, 0 like, 0 bad, 0 replies')->get()->result();
        $this->comment_model->getRepliesAndRankings($replies);
        $this->response(array('code' => 200, 'replies' => $replies));
    }

    private function new()
    {
        $data = [
            'postID' => $this->input->post('postId'),
            'userID' => getSessionUser()->is ? getSessionUser()->userID : 0,
            'comment' => $this->input->post('comment'),
            'datePosted' => date('Y-m-d H:i:s')
        ];
        if ($insertId = $this->comment_model->save($data)) {
            $this->response(array('code' => 200, 'commentId' => $insertId, 'datePosted' => $data['datePosted'], 'nickname' => getSessionUser()->is ? getSessionUser()->userName : ''));
        } else {
            $this->response(array('code' => 500, 'error' => '서버오류가 발생했습니다'));
        }
    }

    private function reply()
    {
        $data = [
            'postID' => $this->input->post('postId'),
            'userID' => getSessionUser()->is ? getSessionUser()->userID : 0,
            'parentID' => $this->input->post('parentId'),
            'comment' => $this->input->post('comment'),
            'datePosted' => date('Y-m-d H:i:s')
        ];
        if ($insertId = $this->comment_model->save($data)) {
            $this->response(array('code' => 200, 'commentId' => $insertId, 'datePosted' => $data['datePosted'], 'nickname' => getSessionUser()->is ? getSessionUser()->userName : ''));
        } else {
            $this->response(array('code' => 500, 'error' => '서버오류가 발생했습니다'));
        }
    }

    private function rating()
    {
        $data = [
            'userID' => getSessionUser()->is ? getSessionUser()->userID : 0,
            'commentID' => $this->input->post('commentId'),
            'title' => $this->input->post('title')
        ];
        if ($this->comment_model->rating($data)) {
            $this->response(array('code' => 200, 'ranking' => $this->comment_model->getCountRatingByCommentIdAndTitle($data['commentID'], $data['title'])));
        } else {
            lq();
            $this->response(array('code' => 500, 'error' => '서버오류가 발생했습니다'));
        }
    }

    private function delete()
    {
        if (getSessionUser()->userID == $this->comment_model->getOne($this->input->post('commentId'))->userID) {
            if ($this->comment_model->delete($this->input->post('commentId'))) {
                $this->response(array('code' => 200));
            } else {
                $this->response(array('code' => 500, 'error' => '서버오류가 발생했습니다'));
            }
        } else {
            $this->response(array('code' => 405, 'error' => '잘못된 요청입니다.'));
        }
    }

    private function update()
    {
        if (getSessionUser()->userID == $this->comment_model->getOne($this->input->post('commentId'))->userID) {
            if ($this->comment_model->update([
                'commentID' => $this->input->post('commentID'),
                'comment' => $this->input->post('comment')
            ])) {
                $this->response(array('code' => 200));
            } else {
                $this->response(array('code' => 500, 'error' => '서버오류가 발생했습니다'));
            }
        } else {
            $this->response(array('code' => 405, 'error' => '잘못된 요청입니다.'));
        }
    }
}
