<?php

namespace App\Http\Controllers\Admin;

# Models
use App\Models\Day;
use App\Models\RateType;
use App\Models\Game;
use App\Models\Store;
use App\Models\GameRate;
use App\Models\GameGroup;
use App\Models\StoreGame;
use App\Models\Users;

# Other Classes
use DB;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

class GameRateController extends Controller
{
     # Define Base view
    protected $view = 'admin.game-rate.';

    #Bind GameGroup model
    protected $gameGroup;
     
     #Bind StoreGame model
    protected $storeGame;

     #Bind Game model
    protected $game;

     #Bind Store model
    protected $store;

    #Bind Day model
    protected $day;

    #Bind GameRate model
    protected $gameRate;
    
    #Bind RateType model
    protected $rateType;
    
    #Bind Users model
    protected $Users;

      /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                GameGroup $gameGroup,
                                Game $game,
                                RateType $rateType,
                                Day $day,
                                Store $store,
                                StoreGame $storeGame,
                                Users $Users,
                                GameRate $gameRate
                            )
    {
        $this->gameGroup  = $gameGroup;
        $this->rateType  = $rateType;
        $this->game       = $game;
        $this->store      = $store;
        $this->day        = $day;
        $this->storeGame  = $storeGame;
        $this->gameRate   = $gameRate;
        $this->Users   = $Users;
    }
   /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
         try
        {
         

           $relation = ['Stores','GameGroupName','GameName','Day'];
       
            #get all paymentUser resources

            $details['lists'] = $this->gameRate->with($relation)->where('created_at','!=',null)->paginate(10);
             
            #render view
            return view($this->view.'index',$details);
        }catch(\Exception $ex)
        {
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
         try{
             
             $user_id = $request->user_id ?? 1;
            $details['rateType']  = $this->rateType->where('status',1)->get();
            $details['gameGroup'] = $this->gameGroup->get();
            $realtion = ['gameRate' => function ($q) use ($user_id){
                $q->where('user_id',$user_id);
            }];
            $details['game']      = $this->game->with($realtion)->whereIn('id',[1,2,5,6,7,9,10,12])->where('status',1)->get();
                 $details['user'] = $this->Users->where('id', $request->user_id ?? 1)->first();
            $details['day'] = $this->day->get();
            #render view
            return view($this->view.'create',$details);
         }catch(\Exception $ex)
        {
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         try{
             #validate the request
            
            $validate = Validator::make($request->all(),[
                                                       
                                                         'game_id'           => 'required',
                                                         'rate'              => 'required',
                                                         'user_id'              => 'required',
                                                        ]);
            if($validate->fails()){

                return redirect()->back()->withErrors($validate);
            }

            #update data
       
            foreach($request->game_id as $key=>$value){
       
                  $check =  $this->gameRate->where('game_id',$key)->where('rate_type',$request->rate_type)->where('user_id',$request->user_id)->first();
                    $data = [
                            'game_id'          => $key,
                             'rate_type'        => $request->rate_type,
                            'rate'             => $request->rate[$key]*10,
                            'user_id'          => $request->user_id,
                            ];
                    #store the newly creted resoures
                    if($check){
                         $this->gameRate->whereId($check->id)->update($data);
                    }else{
                         $this->gameRate->create($data);
                    }
            }
                 #get the requested data in Data variable
              
           
               
               
            
           
            // #return redirect back to view
             toastr()->success('Rate Create Sucess fully ...');
            return  back();
         }catch(\Exception $ex)
        {
             toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
           try{
            # Fetch records
           $details['store'] = $this->store->get();
            $details['gameGroup'] = $this->gameGroup->get();
            $details['game']      = $this->game->where('status',1)->get();
            $details['day'] = $this->day->get();
            $details['detail'] = $this->gameRate->where('id', $id)->first();
            
            #return to view
            return view($this->view.'edit', $details);
         }catch(\Exception $ex)
        {
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         try{
             #validate the request
            
            $validate = Validator::make($request->all(),[
                                                       
                                                         'game_id'           => 'required',
                                                         'rate'              => 'required',
                                                        ]);
            if($validate->fails()){

                return redirect()->back()->withErrors($validate);
            }

            #update data
       
           
                 #get the requested data in Data variable
                $check =  $this->gameRate->where('game_id',$request->game_id)->first();
            $data = [
                    'game_id'          => $request->game_id,
                    'rate'             => $request->rate,
                    'user_id'          => Auth::user()->id ,
                    ];
            #store the newly creted resoures
            if($check){
                 $this->gameRate->whereId($check->id)->update($data);
            }else{
                 $this->gameRate->create($data);
            }
           
               
               
            
           
            // #return redirect back to view
             toastr()->success('Rate Update Sucess fully ...');
            return redirect('/admin/game-rate');
         }catch(\Exception $ex)
        {
             toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
