<?php
	namespace App;

	use Illuminate\Http\Request;
	use \App\User;
	use Illuminate\Support\Facades\DB;

	class Helper {
		public static function getUserAktif() {
			if(auth()->check()){
				$allow = ['admin', 'member', 'reseller', 'priority'];
				$level = auth()->user()->level;
				if(!in_array($level, $allow)){
					return abort(404);
				}
				$user = User::where('expired_date', '>=', date('Y-m-d'))->count();
				return $user;
			} else {
				return abort(404);
			}
		}
		public static function panelName() {
			if(auth()->check()){
				$allow = ['admin', 'member', 'reseller', 'priority'];
				$level = auth()->user()->level;
				if(!in_array($level, $allow)){
					return abort(404);
				}
				$user = User::where('username', auth()->user()->register_by)->first();
				if($user !== NULL){
					if ($user->level == 'admin') {
						return IVMEDIA;
					} else {
						$text = '';
						switch ($user->username) {
							case 'alv':
								$text = ALV;
								break;
							case 'aydapanel':
								$text = AYDA;
								break;
							case 'ifato':
								$text = IFATO;
								break;
							case 'dian':
								$text = DIAN;
								break;
							default:
								$text = IVMEDIA;
								break;
						}
						return $text;
					}
				}
			} else {
				return abort(404);
			}
		}

		public static function isUserRegisteredByAdmin() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->level == 'admin') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}

		public static function isUserRegisteredByAlv() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->username == 'alv') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}

		public static function isUserRegisteredByAyda() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->username == 'aydapanel') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}

		public static function isUserRegisteredByIfato() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->username == 'ifato') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}
		public static function isUserRegisteredByDian() {
			if(auth()->check()){

					//SET PERMISSION WHO CAN VISIT THIS PAGE
					$allow = ['admin', 'member', 'reseller', 'priority'];

					$level = auth()->user()->level;
					if(!in_array($level, $allow)){
			      return abort(404);
					}

					$user = User::where('username', auth()->user()->register_by)->first();
					if($user !== NULL){
						if ($user->username == 'dian') {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
			} else {
				return false;
			}
  	}

    public static function subtractSaldo($id, $price){
			if(auth()->check()){
				Helper::checkLevel(['admin','reseller','member', 'priority']);
				DB::table('users')->where('id', '=', $id)->decrement('saldo', $price);
			} else {
				return false;
			}
    }

    public static function addSaldo($id, $price){
			if(auth()->check()){
				Helper::checkLevel(['admin','reseller','member', 'priority']);
				DB::table('users')->where('id', '=', $id)->increment('saldo', $price);
			} else {
				return false;
			}
    }

		public static function saveUserBalanceHistory($id, $price, $type, $note, $status){
		    $uniqueid_prefix = $id.rand(0,9).rand(0,9);
		    $uniqueid = uniqid($uniqueid_prefix, FALSE);
        DB::table('users_balance')->insert([
            'id_user' => $id,
            'code' => $uniqueid,
            'value' => $price,
            'type' => $type,
            'note' => $note,
            'status' => $status,
            'updated_at' => date('Y-m-d H:i:s'),
            'created_at' => date('Y-m-d H:i:s')
        ]);
    }

		public static function checkLevel($allow){
	      $level = auth()->user()->level;
	      if(!in_array($level, $allow)){
	          return abort(505);
	      }
		}
	}
