<?php


namespace Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;
use Carbon\Carbon;

class Profile extends Model
{
    protected $table = 'profile';


    public static function RandomText($length = 8)
    {
        $vmstext = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $vmsString = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

        return substr(str_shuffle($vmstext), 0, 2) . substr(str_shuffle($vmsString), 0, $length - 2);
    }

    public static function checkUser($chatid)
    {
        return DB::table('profile')->where('chatid', $chatid)->exists();
    }

    public static function checkUserByCode($cod)
    {
        return DB::table('profile')->where('cod', $cod)->exists();
    }

    public static function getUser($chatid)
    {

        return DB::table('profile')->where('chatid', $chatid)->first();
    }

    public static function getUserByCode($cod)
    {
        return DB::table('profile')->where('cod', $cod)->first();
    }

    public static function getRealName($chatid)
    {
        return self::getUser($chatid)->real_name;
    }

    public static function getUserSex($chatid)
    {
        return self::getUser($chatid)->gender;
    }

    public static function getGroupByAge()
    {
        return DB::table('profile')->select('age')->groupBy('age')->get();
    }

    public static function getGroupBystate()
    {
        return DB::table('profile')->select('state')->groupBy('state')->get();
    }

    public static function getBystate($state, $limit = true, $page = 1, $per = 10)
    {
        if (!$limit) {
            return DB::table('profile')->where('state', $state)->get();
        }
        $pageNumber = ($page - 1) * $per;
        return DB::table('profile')->where('state', $state)->limit($per)->offset($pageNumber)->orderBy('id', 'DESC')->get();

    }

    public static function getByAge($age, $limit = true, $page = 1, $per = 10)
    {
        if (!$limit) {
            return DB::table('profile')->where('age', $age)->get();
        }

        $pageNumber = ($page - 1) * $per;
        return DB::table('profile')->where('age', $age)->limit($per)->offset($pageNumber)->orderBy('id', 'DESC')->get();

    }

    public static function getOnlineUsersCount()
    {
        return DB::table('profile')->select('id')->where('lastonline', '>', Carbon::now()->subMinutes('1')->unix())->count();
    }

    public static function getOnlineUsers($limit = true, $page = 1, $per = 10)
    {
        if (!$limit) {
            return DB::table('profile')->where('lastonline', '>', Carbon::now()->subMinutes('5')->unix())->get();

        }

        $pageNumber = ($page - 1) * $per;
        return DB::table('profile')->where('lastonline', '>', Carbon::now()->subMinutes('5')->unix())->limit($per)->offset($pageNumber)->orderBy('lastonline', 'DESC')->get();

    }

    public static function getUserByMin($min = 1)
    {
        return DB::table('profile')->select('id', 'chatid')->where('lastonline', '>', Carbon::now()->subMinutes($min)->unix())->get();
    }

    public static function CheckAndInsertProfile($chatid)
    {
        $c = self::checkUser($chatid);

        $cod = self::RandomText(8);
        $out = false;
        if (!$c) {
            $out = DB::table('profile')->insertGetId(['chatid' => $chatid, 'cod' => $cod]);

        } else {
            $out = self::getUser($chatid);
        }

        return $out;
    }

    public static function updateName($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['name' => $values]);
        return true;
    }

    public static function updateAge($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['age' => $values]);
        return true;
    }

    public static function updateState($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['state' => $values]);
        return true;

    }

    public static function updateCity($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['city' => $values]);
        return true;
    }

    public static function updateGender($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['gender' => $values]);
        return true;
    }

    public static function updatePhoto($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['photo' => $values]);
        return true;
    }

    public static function updateIngame($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['ingame' => $values]);
        return true;
    }

    public static function updateLastOnline($cahtid)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['lastonline' => Carbon::now('Asia/Tehran')->unix()]);
        return true;
    }

    public static function updateCoin($cahtid, $values)
    {
        $values = $values < 0 ? 0 : $values;

        DB::table('profile')->where('chatid', $cahtid)->update(['coin' => $values]);
        return true;
    }

    public static function updateInvite($cahtid, $values)
    {
        $values = $values < 0 ? 0 : $values;
        DB::table('profile')->where('chatid', $cahtid)->update(['invite' => $values]);
        return true;
    }

    public static function updateBackbug($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['backbug' => $values]);
        return true;
    }

    public static function updateRealname($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['real_name' => $values]);
        return true;
    }
    public static function updateProtectContent($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['protectContent' => $values]);
        return true;
    }
    public static function updateReduced($cahtid, $values)
    {
        DB::table('profile')->where('chatid', (string)$cahtid)->update(['reduced' => $values]);
        return true;
    }

    public static function addCoin($cahtid, $values)
    {
        DB::table('profile')->where('chatid', $cahtid)->update(['coin' =>  DB::raw('coin + ' . $values)]);
        return true;
    }
    public static function giveCoin($coin)
    {
        DB::table('profile')->update(['coin' => DB::raw('coin + ' . $coin)]);
        return true;
    }
}
