<?php

    /*!
     * ifsoft.co.uk v1.1
     *
     * http://ifsoft.com.ua, http://ifsoft.co.uk
     * qascript@mail.ru
     *
     * Copyright 2012-2017 Demyanchuk Dmitry (https://vk.com/dmitry.demyanchuk)
     */

    if (!$auth->authorize(auth::getCurrentUserId(), auth::getAccessToken())) {

        header('Location: /');
    }

    $chat_id = 0;
    $user_id = 0;

    if (!empty($_POST)) {

        $access_token = isset($_POST['access_token']) ? $_POST['access_token'] : '';

        $user_id = isset($_POST['user_id']) ? $_POST['user_id'] : 0;
        $chat_id = isset($_POST['chat_id']) ? $_POST['chat_id'] : 0;
        $message_id = isset($_POST['message_id']) ? $_POST['message_id'] : 0;

        $user_id = helper::clearInt($user_id);
        $chat_id = helper::clearInt($chat_id);
        $message_id = helper::clearInt($message_id);

        $result = array("error" => true,
                        "error_code" => ERROR_UNKNOWN);

        if ($access_token != auth::getAccessToken()) {

            api::printError(ERROR_ACCESS_TOKEN, "Error authorization.");
        }

        $profile = new profile($dbo, $user_id);
        $profile->setRequestFrom(auth::getCurrentUserId());

        $profileInfo = $profile->get();

        if ($profileInfo['state'] != ACCOUNT_STATE_ENABLED) {

            echo json_encode($result);
            exit;
        }

        if ($profileInfo['allowMessages'] == 0 && $profileInfo['follower'] === false) {

            echo json_encode($result);
            exit;
        }

        $blacklist = new blacklist($dbo);
        $blacklist->setRequestFrom($user_id);

        if (!$blacklist->isExists(auth::getCurrentUserId())) {

            $messages = new messages($dbo);
            $messages->setRequestFrom(auth::getCurrentUserId());

            $result = $messages->getNextMessages($chat_id, $message_id);

            ob_start();

            foreach ($result['messages'] as $key => $value) {

                draw($value, $LANG, $helper);
            }

            $result['html'] = ob_get_clean();
            $result['items_all'] = $messages->messagesCountByChat($chat_id);
        }

        echo json_encode($result);
        exit;
    }

    function draw($message, $LANG, $helper)
    {

        $profilePhotoUrl = "/img/profile_default_photo.png";

        if (strlen($message['fromUserPhotoUrl']) != 0) {

            $profilePhotoUrl = $message['fromUserPhotoUrl'];
        }

        $time = new language(NULL, $LANG['lang-code']);

        ?>

        <li class="custom-list-item message-item" data-id="<?php echo $message['id']; ?>">

            <a href="/<?php echo $message['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

            <a href="/<?php echo $message['fromUserUsername']; ?>" class="custom-item-link"><?php echo $message['fromUserFullname']; ?></a>

            <div class="item-meta">

                <span class="post-date"><span class="time"><?php echo $time->timeAgo($message['createAt']); ?></span></span>

                <?php

                if (strlen($message['message']) > 0) {

                    ?>
                    <p class="post-text"><?php echo $message['message']; ?></p>
                    <?php
                }

                if (strlen($message['imgUrl']) > 0) {

                    ?>

                    <img class="post-img" style="" alt="post-img" src="<?php echo $message['imgUrl']; ?>">
                    <?php
                }
                ?>
            </div>

        </li>

        <?php
    }