<?php

use Carbon\Carbon;
use Database\Profile;

if ($data == 'searchUser') {
    $Telegram->EditMessageText($chatid, $MessageID, msg('def'), 'html', '', true, $button_searchUsers);
}

if ($data == 'OnlineUsers') {
    $list = Profile::getOnlineUsers();
    if (count($list) == 0) {
        $Telegram->sendMessage($chatid, msg('notfounduser'), 'html', false);
        die();
    }
    $txt = '';
    foreach ($list as $it) {
        $getUpdateTime = Carbon::createFromTimestamp($it->lastonline, 'Asia/Tehran')->diffForHumans();

        $txt .= str_replace(['#name', '#cod', '#time'], [$it->name ?? '?', '/Profile_' . $it->cod, $getUpdateTime], msg('listUsers')) . "\n\n";
    }
    if (count($list) == 10) {
        $txt .= "ادامه : \n" . '/nextsearchOnlineUsers_2';
    }
    $Telegram->sendMessage($chatid, $txt, 'html', false);
} elseif (preg_match('/^\/([Nn]extsearchOnlineUsers_)(.*)/', $text)) {
    $dta = str_replace(['/nextsearchOnlineUsers_'], '', $text);
    $list = Profile::getOnlineUsers(true, $dta);
    $txt = '';
    foreach ($list as $it) {
        $getUpdateTime = Carbon::createFromTimestamp($it->lastonline, 'Asia/Tehran')->diffForHumans();

        $txt .= str_replace(['#name', '#cod', '#time'], [$it->name ?? '?', '/Profile_' . $it->cod, $getUpdateTime], msg('listUsers')) . "\n\n";
    }
    if (count($list) == 10) {
        $ploffset = $dta + 1;
        $txt .= "ادامه : \n" . '/nextsearchOnlineUsers_' . $ploffset;
    }
    $Telegram->sendMessage($chatid, $txt, 'html', false);

} elseif ($data == 'searchbyAge') {

    $list = Profile::getGroupByAge();
    if (count($list) == 0) {
        $Telegram->sendMessage($chatid, msg('notfounduser'), 'html', false);
        die();
    }
    $btn = [];
    $col = [];

    $i = 1;

    foreach ($list as $it) {
        if (!is_null($it->age)) {
            if ($i % 2) {
                $col[] = ['text' => $it->age, 'callback_data' => 'selectSearchUserByAge_' . $it->age];
                $btn[] = $col;
                $col = [];
            } else {

                $col[] = ['text' => $it->age, 'callback_data' => 'selectSearchUserByAge_' . $it->age];
            }
            $i++;
        }
    }
    if ($col != []) {
        $btn[] = $col;
        $col = [];

    }
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, [
        'inline_keyboard' =>
            $btn
        ,
        'resize_keyboard' => true
    ]);
} elseif (preg_match('/^([Ss]electSearchUserByAge_)(.*)/', $data)) {
    $dta = str_replace(['selectSearchUserByAge_'], '', $data);

    $list = Profile::getByAge($dta, true);
    $txt = '';
    foreach ($list as $it) {
        $getUpdateTime = Carbon::createFromTimestamp($it->lastonline, 'Asia/Tehran')->diffForHumans();

        $txt .= str_replace(['#name', '#cod', '#time'], [$it->name ?? '?', '/Profile_' . $it->cod, $getUpdateTime], msg('listUsers')) . "\n\n";
    }
    if (count($list) == 10) {
        $txt .= "ادامه : \n" . '/nextsearchAge_' . $dta . '_2';
    }
    $Telegram->sendMessage($chatid, $txt, 'html', false);

} elseif (preg_match('/^\/([Nn]extsearchAge_)(.*)/', $text)) {
    $dta = str_replace(['/nextsearchAge_'], '', $text);
    list($age, $page) = explode('_', $dta);
    $list = Profile::getByAge($age, true, $page);
    foreach ($list as $it) {
        $getUpdateTime = Carbon::createFromTimestamp($it->lastonline, 'Asia/Tehran')->diffForHumans();

        $txt .= str_replace(['#name', '#cod', '#time'], [$it->name ?? '?', '/Profile_' . $it->cod, $getUpdateTime], msg('listUsers')) . "\n\n";
    }
    if (count($list) == 10) {
        $ploffset = $page + 1;
        $txt .= "ادامه : \n" . '/nextsearchAge_' . $age . '_' . $ploffset;
    }
    $Telegram->sendMessage($chatid, $txt, 'html', false);

}
if ($data == 'searchbyState') {
    $list = Profile::getGroupBystate();
    if (count($list) == 0) {
        $Telegram->sendMessage($chatid, msg('notfounduser'), 'html', false);
        die();
    }
    $btn = [];
    $col = [];

    $i = 1;

    foreach ($list as $it) {
        if (!is_null($it->state)) {

            if ($i % 2) {

                $col[] = ['text' => $it->state, 'callback_data' => 'selectSearchUserByState_' . array_search($it->state, $ListState)];
                $btn[] = $col;
                $col = [];
            } else {

                $col[] = ['text' => $it->state, 'callback_data' => 'selectSearchUserByState_' . array_search($it->state, $ListState)];
            }

            $i++;
        }
    }
    if ($col != []) {
        $btn[] = $col;
        $col = [];

    }
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, [
        'inline_keyboard' =>
            $btn
        ,
        'resize_keyboard' => true
    ]);
} elseif (preg_match('/^([Ss]electSearchUserByState_)(.*)/', $data)) {
    $dta = str_replace(['selectSearchUserByState_'], '', $data);
    $list = Profile::getBystate($ListState[$dta]);
    $txt = '';
    foreach ($list as $it) {
        $getUpdateTime = Carbon::createFromTimestamp($it->lastonline, 'Asia/Tehran')->diffForHumans();

        $txt .= str_replace(['#name', '#cod', '#time'], [$it->name ?? '?', '/Profile_' . $it->cod, $getUpdateTime], msg('listUsers')) . "\n\n";
    }
    if (count($list) == 10) {
        $txt .= "ادامه : \n" . '/nextsearchState_' . $dta . '_10';
    }
    $Telegram->sendMessage($chatid, $txt, 'html', false);

} elseif (preg_match('/^\/([Nn]extsearchState_)(.*)/', $text)) {
    $dta = str_replace(['/nextsearchState_'], '', $text);
    list($state, $page) = explode('_', $dta);
    $list = Profile::getBystate($ListState[$state], true, $page);
    foreach ($list as $it) {
        $getUpdateTime = Carbon::createFromTimestamp($it->lastonline, 'Asia/Tehran')->diffForHumans();

        $txt .= str_replace(['#name', '#cod', '#time'], [$it->name ?? '?', '/Profile_' . $it->cod, $getUpdateTime], msg('listUsers')) . "\n\n";
    }
    if (count($list) == 10) {
        $ploffset = $page + 1;
        $txt .= "ادامه : \n" . '/nextsearchState_' . $state . '_' . $ploffset;
    }
    $Telegram->sendMessage($chatid, $txt, 'html', false);

}
