<?php


namespace Database;

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


class Games extends Model
{
    protected $table = 'games';
    public $timestamps = false;

    public static function createGame($rand_id, $bot, $sender, $type, $chatid, $users, $status = null)
    {
        $users = implode('|', [$users]);
        return DB::connection('sqlite')->table('games')->insertGetId(['rand_id' => $rand_id, 'bot' => $bot, 'sender' => $sender, 'type' => $type, 'chatid' => $chatid, 'users' => $users, 'status' => $status, 'update_at' => Carbon::now('Asia/Tehran')->toDateTimeString(), 'create_at' => Carbon::now('Asia/Tehran')->toDateTimeString()]);

    }

    public static function hasGame($rand_id)
    {
        return DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->exists();
    }

    public static function hasganes()
    {
        return DB::connection('sqlite')->table('games')->exists();
    }

    public static function hasGameByType($type)
    {
        return DB::connection('sqlite')->table('games')->where('type', $type)->exists();
    }

    public static function getGame($rand_id)
    {
        return DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->first();
    }


    public static function getGameByDay($day = 1)
    {
        return DB::connection('sqlite')->table('games')->select('id')->where('update_at', '<', Carbon::now('Asia/Tehran')->subDays($day)->toDateTimeString())->get()->toArray();

    }

    public static function getGameByhoursViaType($hours = 1)
    {
        return DB::connection('sqlite')->table('games')->where(function ($q) {
            $q->where('type', 'Normal')->orWhere('type', 'plus')->orWhere('type', 'via');
        })->where('update_at', '<', Carbon::now('Asia/Tehran')->subHours($hours)->toDateTimeString())->get()->toArray();
    }
    public static function getGameByhoursUnType($hours = 1)
    {
        return DB::connection('sqlite')->table('games')->where(function ($q) {
            $q->where('type', 'Un');
        })->where('update_at', '<', Carbon::now('Asia/Tehran')->subHours($hours)->toDateTimeString())->get()->toArray();
    }

    public static function getGameByhours($hours = 1)
    {
        return DB::connection('sqlite')->table('games')->where('update_at', '<', Carbon::now('Asia/Tehran')->subHours($hours)->toDateTimeString())->get()->toArray();
    }

    public static function getGameByMin($min = 1)
    {
        return DB::connection('sqlite')->table('games')->where('update_at', '<', Carbon::now('Asia/Tehran')->subMinutes($min)->toDateTimeString())->get()->toArray();
    }

    public static function getGameTodayCount()
    {
        return DB::connection('sqlite')->table('games')->select('id')->where('create_at', '>=', Carbon::today('Asia/Tehran'))->count();
    }

    public static function getGameYesterdayCount()
    {
        return DB::connection('sqlite')->table('games')->select('id')->where('create_at', '>=', Carbon::yesterday('Asia/Tehran'))->count();
    }

    public static function getActiveCount()
    {
        return DB::connection('sqlite')->table('games')->select('id')->where('status', 'start')->count();
    }

    public static function getCountType($type)
    {
        return DB::connection('sqlite')->table('games')->select('id')->where('status', 'start')->where('type', $type)->count();
    }

    public static function getGameUn()
    {
        return self::getCountType('Un');
    }

    public static function getGameNormal()
    {
        return self::getCountType('Normal');
    }

    public static function getGamePlus()
    {
        return self::getCountType('plus');
    }

    public static function getGameFriends()
    {
        return self::getCountType('friends');
    }

    public static function getCountGameByBot($bot)
    {
        return DB::connection('sqlite')->table('games')->select('id')->where('bot', '$bot')->whereIn('type', ['un', 'friends'])->where('status', 'start')->count();
    }

    public static function getCountPlayer($rand_id)
    {
        return count(explode('|', self::getGame($rand_id)->users));
    }

    public static function hasUsers($rand_id, $userid)
    {
        $games = self::getGame($rand_id);
        $users = explode('|', $games->users);
        return in_array($userid, $users);
    }

    public static function addUser($rand_id, $userid)
    {
        if (self::hasUsers($rand_id, $userid) == false) {
            $games = self::getGame($rand_id);
            $users = explode('|', $games->users);
            if (!is_array($users)) {
                $users = [$users];
            }

            self::updateUsers($rand_id, implode('|', array_merge($users, [$userid])));

            return true;
        }

        return false;
    }

    public static function updateUsers($rand_id, $values)
    {
        DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->update(['users' => $values]);
        return true;
    }

    public static function updateType($rand_id, $values)
    {
        DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->update(['type' => $values]);
    }

    public static function updateAt($rand_id)
    {
        DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->update(['update_at' => Carbon::now('Asia/Tehran')->toDateTimeString()]);
    }

    public static function updateStatus($rand_id, $values)
    {
        DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->update(['status' => $values]);
    }

    public static function updateChatid($rand_id, $values)
    {
        DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->update(['chatid' => $values]);
    }

    public static function updateMsgId($rand_id, $values)
    {
        DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->update(['msg_id' => $values]);
    }

    public static function removeGame($rand_id)
    {
        return DB::connection('sqlite')->table('games')->where('rand_id', $rand_id)->delete();
    }
}
