<?php

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Table\Table;
use Joomla\Registry\Registry;

class GuruControllerGuruReview extends BaseController
{
    public function reportReview()
    {
        ob_clean();
        if (!$this->checkToken('post', false)) {
            die(json_encode([
                'error' => Text::_('JINVALID_TOKEN_NOTICE'),
            ]));
        }

        $user = Factory::getUser();
        $input = $this->input;
        $courseId = $input->getInt('courseId');
        $reportType = $input->getInt('reportType');
        $reviewId = $input->getInt('reviewId');

        if (!$user->id || !$courseId || !$reviewId || !in_array($reportType, [1,2,3])) {
            die(json_encode([
                'error' => 'data error',
            ]));
        }

        require_once JPATH_ROOT . '/components/com_guru/tables/gurureview.php';
        $reviewTable = Table::getInstance('GuruReview', 'Table');
        $reviewTable->load([
            'course_id' => $courseId,
            'id' => $reviewId,
        ]);

        if (!$reviewTable->id) {
            die(json_encode([
                'error' => Text::_('GURU_REVIEW_IS_NOT_EXISTED'),
            ]));
        }

        require_once JPATH_ROOT . '/components/com_guru/tables/gurureviewreport.php';
        $reportTable = Table::getInstance('GuruReviewReport', 'Table');
        $reportTable->load([
            'user_id' => $user->id,
            'review_id' => $reviewId,
        ]);

        if (!$reportTable->id) {
            $reportTable->user_id = $user->id;
            $reportTable->review_id = $reviewId;
        }

        $reportTable->report_type = $reportType;
        $reportTable->resolved = 0;

        $config = Factory::getConfig();
        $now = Factory::getDate('now', $config->get('offset'));
        $reportTable->report_date = $now->format('Y-m-d H:i:s', true);

        if ($reportTable->store()) {
            die(json_encode([
                'success' => true,
            ]));
        } else {
            die(json_encode([
                'error' => $reportTable->getError(),
            ]));
        }
    }

    public function doReview()
    {
        ob_clean();
        if (!$this->checkToken('post', false)) {
            die(json_encode([
                'error' => Text::_('JINVALID_TOKEN_NOTICE'),
            ]));
        }

        $user = Factory::getUser();
        if (!$user->id) {
            die(json_encode([
                'error' => Text::_('JERROR_ALERTNOAUTHOR'),
            ]));
        }

        $input = Factory::getApplication()->input;
        $course_id = $input->getInt('course_id');
        $buyedCourse = guruHelper::buyedCourse($course_id, $user->id);

        if ($buyedCourse) {
            require_once JPATH_ROOT . '/components/com_guru/tables/gurureview.php';
            $table = Table::getInstance('GuruReview', 'Table');
            $table->load([
                'user_id' => $user->id,
                'course_id' => $course_id,
            ]);

            $config = Factory::getConfig();
            $now = Factory::getDate('now', $config->get('offset'));
            if ($table->id) {
                $table->modifed_time = $now->format('Y-m-d H:i:s', true);
            } else {
                $table->user_id = $user->id;
                $table->course_id = $course_id;
                $table->review_time = $now->format('Y-m-d H:i:s', true);
            }

            $rating = $input->getInt('rating', 1);
            $rating = $rating >= 1 && $rating <= 5 ? $rating : 1;
            $table->rating = $rating;

            $guruConfig = guruHelper::getConfig();
            $reviewConfig = new Registry($guruConfig->get('review'));
            if ($reviewConfig->get('review_moderate')) {
                $table->state = 2;
            }

            $content = $input->getString('content', '');
            if (!$content) {
                die(json_encode([
                    'error' => 'review content should not empty',
                ]));
            }

            if (mb_strlen($content) > 2000) {
                die(json_encode([
                    'error' => Text::_('GURU_YOUR_REVIEW_MAX_CHAR_2000'),
                ]));
            }

            $table->content = $content;

            if ($table->store()) {
                die(json_encode([
                    'success' => true,
                    'content' => nl2br(htmlspecialchars($table->content)),
                ]));
            } else {
                die(json_encode([
                    'error' => $table->getError(),
                ]));
            }
        } else {
            die(json_encode([
                'error' => 'Kindly buy course before giving any review.',
            ]));
        }
    }

    public function deleteReview()
    {
        ob_clean();
        if (!$this->checkToken('post', false)) {
            die(json_encode([
                'error' => Text::_('JINVALID_TOKEN_NOTICE'),
            ]));
        }

        $user = Factory::getUser();
        if (!$user->id) {
            die(json_encode([
                'error' => Text::_('JERROR_ALERTNOAUTHOR'),
            ]));
        }

        $input = Factory::getApplication()->input;
        $course_id = $input->getInt('course_id');
        $buyedCourse = guruHelper::buyedCourse($course_id, $user->id);

        if ($buyedCourse) {
            require_once JPATH_ROOT . '/components/com_guru/tables/gurureview.php';
            $table = Table::getInstance('GuruReview', 'Table');
            $table->load([
                'user_id' => $user->id,
                'course_id' => $course_id,
            ]);

            if (!$table->id) {
                die(json_encode([
                    'error' => 'you haven\'t review this course yet',
                ]));
            }

            if ($table->delete()) {
                die(json_encode([
                    'success' => true,
                    'content' => 'deleted',
                ]));
            } else {
                die(json_encode([
                    'error' => $table->getError(),
                ]));
            }
        }
    }
}
