<?php


namespace Database;

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

class Playerlist extends Model
{
    protected $table = 'playerlist';

    public static function getPlayers()
    {
        return DB::table('playerlist')->get();
    }

    public static function getCountPlayers()
    {
        return DB::table('playerlist')->count();

    }

    public static function getPlayerByMin($min = 1)
    {
        return DB::table('playerlist')->where('create_at', '<', Carbon::now('Asia/Tehran')->subMinutes($min)->toDateTimeString())->get();
    }

    public static function getNotPlayers($chatid)
    {
        return DB::table('playerlist')->where('chatid', '!=', $chatid)->inRandomOrder()->get();
    }

    public static function getPlayer($chatid)
    {
        return DB::table('playerlist')->where('chatid', (string)$chatid)->first();
    }


    public static function addPlayer($chatid, $gender, $ugender)
    {
        return DB::table('playerlist')->insertGetId(['chatid' => (string)$chatid, 'gender' => $gender, 'ugender' => $ugender]);
    }

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


    public static function getMale($male = false)
    {

        if ($male) {
            return DB::table('playerlist')->where(function ($q) {
                $q->where('gender', 'male')->where('ugender', 'male');
            })->orWhere(function ($q) {
                $q->where('gender', 'all')->where('ugender', 'male');
            })->inRandomOrder()->get();
        }
        return DB::table('playerlist')->where(function ($q) {
            $q->where('gender', 'female')->where('ugender', 'male');
        })->orWhere(function ($q) {
            $q->where('gender', 'all')->where('ugender', 'male');
        })->inRandomOrder()->get();
    }

    public static function getMaleNot($chatid, $male = false)
    {
        if ($male) {
            return DB::table('playerlist')->where(function ($q) {
                $q->where('gender', 'male')->where('ugender', 'male');
            })->orWhere(function ($q) {
                $q->where('gender', 'all')->where('ugender', 'male');
            })->where('chatid', '!=', (string)$chatid)->inRandomOrder()->get();
        }
        return DB::table('playerlist')->where(function ($q) {
            $q->where('gender', 'female')->where('ugender', 'male');
        })->orWhere(function ($q) {
            $q->where('gender', 'all')->where('ugender', 'male');
        })->where('chatid', '!=', (string)$chatid)->inRandomOrder()->get();

    }

    public static function getFemale($female = false)
    {
        if ($female) {
            return DB::table('playerlist')->where(function ($q) {
                $q->where('gender', 'female')->where('ugender', 'female');
            })->orWhere(function ($q) {
                $q->where('gender', 'all')->where('ugender', 'female');
            })->inRandomOrder()->get();
        }
        return DB::table('playerlist')->where(function ($q) {
            $q->where('gender', 'male')->where('ugender', 'female');
        })->orWhere(function ($q) {
            $q->where('gender', 'all')->where('ugender', 'female');
        })->inRandomOrder()->get();
    }

    public static function getFemaleNot($chatid, $female = false)
    {
        if ($female) {
            return DB::table('playerlist')->where(function ($q) {
                $q->where('gender', 'female')->where('ugender', 'female');
            })->orWhere(function ($q) {
                $q->where('gender', 'all')->where('ugender', 'female');
            })->where('chatid', '!=', (string)$chatid)->inRandomOrder()->get();
        }
        return DB::table('playerlist')->where(function ($q) {
            $q->where('gender', 'male')->where('ugender', 'female');
        })->orWhere(function ($q) {
            $q->where('gender', 'all')->where('ugender', 'female');
        })->where('chatid', '!=', (string)$chatid)->inRandomOrder()->get();
    }

    public static function getAll($male = false)
    {
        if ($male) {
            return DB::table('playerlist')->where(function ($q) {
                $q->where('gender', 'male')->where('ugender', 'female');
            })->orWhere(function ($q) {
                $q->where('gender', 'all')->where('ugender', 'female');
            })->orWhere(function ($q) {
                $q->where('gender', 'male')->where('ugender', 'male');
            })->orWhere(function ($q) {
                $q->where('gender', 'all')->where('ugender', 'male');
            })->get();
        }
        return DB::table('playerlist')->where(function ($q) {
            $q->where('gender', 'female')->where('ugender', 'female');
        })->orWhere(function ($q) {
            $q->where('gender', 'all')->where('ugender', 'female');
        })->orWhere(function ($q) {
            $q->where('gender', 'female')->where('ugender', 'male');
        })->orWhere(function ($q) {
            $q->where('gender', 'all')->where('ugender', 'male');
        })->get();
    }

    public static function getAllNot($chatid, $male = false)
    {
        if ($male) {
            return DB::table('playerlist')->where(function ($q) {
                $q->where('gender', 'male')->where('ugender', 'female');
            })->orWhere(function ($q) {
                $q->where('gender', 'all')->where('ugender', 'female');
            })->orWhere(function ($q) {
                $q->where('gender', 'male')->where('ugender', 'male');
            })->orWhere(function ($q) {
                $q->where('gender', 'all')->where('ugender', 'male');
            })->where('chatid', '!=', (string)$chatid)->get();
        }
        return DB::table('playerlist')->where(function ($q) {
            $q->where('gender', 'female')->where('ugender', 'female');
        })->orWhere(function ($q) {
            $q->where('gender', 'all')->where('ugender', 'female');
        })->orWhere(function ($q) {
            $q->where('gender', 'female')->where('ugender', 'male');
        })->orWhere(function ($q) {
            $q->where('gender', 'all')->where('ugender', 'male');
        })->where('chatid', '!=', (string)$chatid)->get();
    }

    public static function hasPlayerAll($male = false)
    {
        $a = self::getAll($male);

        return $a->count();
    }

    public static function hasPlayerMale($male = false)
    {
        $a = self::getMale($male);

        return $a->count();
    }

    public static function hasPlayerFemale($female = false)
    {
        $a = self::getFemale($female);

        return $a->count();
    }


    public static function hasPlayers()
    {
        return DB::table('playerlist')->exists();
    }

    public static function Has_PlayersNot($chatid)
    {
        return DB::table('playerlist')->where('chatid', '!=', (string)$chatid)->exists();
    }

    public static function removePlayer($chatid)
    {
        return DB::table('playerlist')->where('chatid', (string)$chatid)->delete();
    }
}
