<?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: /');
    }

    $showForm = true;

    $chat_id = 0;
    $user_id = 0;

    $chat_info = array("messages" => array());
    $user_info = array();
    $profile_info = array();

    $profile = new profile($dbo, auth::getCurrentUserId());
    $profile_info = $profile->get();

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

    if (!isset($_GET['chat_id']) && !isset($_GET['user_id'])) {

        header('Location: /');
        exit;

    } else {

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

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

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

        if ($user_info['error'] === true) {

            header('Location: /');
            exit;
        }

        $chat_id_test = $messages->getChatId(auth::getCurrentUserId(), $user_id);

        if ($chat_id != 0 && $chat_id_test != $chat_id) {

            header('Location: /');
            exit;
        }

        if ($chat_id == 0) {

            $chat_id = $messages->getChatId(auth::getCurrentUserId(), $user_id);

            if ($chat_id != 0) {

                header('Location: /account/chat/?chat_id='.$chat_id.'&user_id='.$user_id);
                exit;
            }
        }

        if ($chat_id != 0) {

            $chat_info = $messages->get($chat_id, 0);
        }
    }

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

        $showForm = false;
    }

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

        $showForm = false;
    }

    $blacklist = new blacklist($dbo);
    $blacklist->setRequestFrom($user_info['id']);

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

        $showForm = false;
    }

    $items_all = $messages->messagesCountByChat($chat_id);
    $items_loaded = 0;

    $page_id = "chat";

    $css_files = array("main.css", "my.css");
    $page_title = $LANG['page-messages']." | ".APP_TITLE;

    include_once("../html/common/header.inc.php");

?>

<body class="">


	<?php
		include_once("../html/common/topbar.inc.php");
	?>


	<div class="wrap content-page">

		<div class="main-column">

			<div class="main-content">

				<div class="content-list-page">

                    <header class="top-banner">

                        <div class="info">
                            <h1><?php echo $user_info['fullname']; ?></h1>
                        </div>

                    </header>

                    <?php

                        if ($items_all > 20) {

                            ?>

                            <header class="top-banner loading-banner" style="padding-top: 0;">

                                <div class="prompt">
                                    <button onclick="Messages.more('<?php echo $chat_id ?>', '<?php echo $user_id ?>'); return false;" class="button green loading-button"><?php echo $LANG['action-more']; ?></button>
                                </div>

                            </header>

                            <?php
                            }

                        ?>

                        <ul class="items-list content-list">

                        <?php

                            $result = $chat_info;

                            $items_loaded = count($result['messages']);

                            if ($items_loaded != 0) {

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

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

                        ?>

                        </ul>

                        <?php

                            if ($items_loaded == 0) {

                                ?>
                                    <header class="top-banner info-banner">

                                        <div class="info">
                                            <h1><?php echo $LANG['label-empty-list']; ?></h1>
                                        </div>

                                    </header>
                                <?php
                            }
                        ?>

                        <?php

                            if ($showForm) {

                                ?>

                                    <div class="comment_form comment-form standard-page">

                                        <form class="" onsubmit="Messages.create('<?php echo $chat_id; ?>', '<?php echo $user_id; ?>', '<?php echo auth::getAccessToken(); ?>'); return false;">
                                            <input type="hidden" name="message_image" value="">
                                            <input class="comment_text" name="message_text" maxlength="340" placeholder="<?php echo $LANG['label-placeholder-message']; ?>" type="text" value="">
                                            <button style="display: inline-block; padding: 7px 16px;" class="primary_btn blue comment_send"><?php echo $LANG['action-send']; ?></button>
                                            <a onclick="Messages.changeChatImg('<?php echo $LANG['action-change-image']; ?>'); return false;" class="add_image_to_post" style="">
                                                <img style="width: 26px; height: 26px;" class="msg_img_preview" src="/img/camera.png">
                                            </a>
                                        </form>

                                    </div>

                                <?php
                            }
                        ?>


				</div>

			</div>
		</div>

		<?php

			include_once("../html/common/sidebar.inc.php");
		?>

	</div>

	<?php

		include_once("../html/common/footer.inc.php");
	?>

        <script type="text/javascript">

            var items_all = <?php echo $items_all; ?>;
            var items_loaded = <?php echo $items_loaded; ?>;

            App.chatInit('<?php echo $chat_id; ?>', '<?php echo $user_id; ?>', '<?php echo auth::getAccessToken(); ?>');

        </script>

        <script type="text/javascript" src="/js/jquery.ocupload-1.1.2.js"></script>


</body
</html>

<?php

    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
    }
