<?php

namespace App\Http\Controllers\API;

use App\Models\Notice;
use App\Models\LockyNo;
use App\Models\Banner;
use App\Models\GameWinner;
use App\Models\NewGameWinner;
use App\Models\NewStoreGame;
use App\Models\Day;

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

class NoticeController extends BaseController
{
    

     #Bind Day model
    protected $day;

     #Bind Notice model
    protected $notice;
    
         #Bind LockyNo model
    protected $lockyNo;
    
    #Bind Banner model
    protected $banner;
    
    #Bind GameWinner model
    protected $gameWinner;
    
    #Bind NewGameWinner model
    protected $newGameWinner;
    
    #Bind NewStoreGame model
    protected $newStoreGame;



    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                Day $day,
                                Notice $notice,
                                Banner $banner,
                                GameWinner $gameWinner,
                                NewGameWinner $newGameWinner,
                                NewStoreGame $newStoreGame,
                                LockyNo $lockyNo
                            )
    {
        $this->notice           = $notice;
        $this->day           = $day;
        $this->gameWinner       = $gameWinner;
        $this->newGameWinner    = $newGameWinner;
        $this->newStoreGame           = $newStoreGame;
        $this->banner           = $banner;
        $this->lockyNo          = $lockyNo;
        
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
         try
        {
            #Stat get 
            $success['appdetail'] = $this->notice->first();

            #return response with active state
            return $this->sendResponse($success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }
     public function gameChart(Request $request)
    {
         try
        {
            $redjodi = array("00","11","22","33","44","55","66","77","88","99","05","16","27","38","49","50","61","72","83","94","xx");
            $weeks = [];
            
            $currentDate = new \DateTime();
            for ($i = 0; $i < 26; $i++) {
                $weekStart = clone $currentDate;
                $weekStart->modify('last Monday');
        
                $weekEnd = clone $weekStart;
                $weekEnd->modify('next Sunday');
            
                // Format the dates and add to the array
                $weekDates = getWeekDates($weekStart->format('Y-m-d'), $weekEnd->format('Y-m-d'));
                  $day = [];
                foreach($weekDates as $key=>$weekDate){
                     $k=0;
                    foreach($weekDate as $key1=>$weekDatename){
                        $date = $weekDatename;
                         $gamewinner = $this->gameWinner->where('stores_id',$request->stores_id)->where('created_at','like',$date.'%')->first();
                         
                              
                         $bit_open = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->open_time))  : '')  : '';
                         $bit_close = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->close_time))  : '')  : '';
                         
                       
                        
                         $day[ $k]["result"] =   isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                         $jodi =  isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                          $day[ $k]["colour"] =     in_array($jodi, $redjodi) ? 2 : 1 ;
                          
                           $day[ $k]["sdate"]  =    $weekStart->format('Y-m-d');
                           $day[ $k]["edate"]  =    $weekEnd->format('Y-m-d');
                          $k++;
                    }
                    
                }
             
               $weeks[$i]["week"] =  $day ;
                $currentDate->modify('-1 week');
            }
          $weeks = array_reverse($weeks);

$success['list'] = $weeks;
            // #return response with active state
            return $this->sendResponse( $success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }
    public function gamePanelChart(Request $request)
    {
         try
        {
            
            $redjodi = array("00","11","22","33","44","55","66","77","88","99","05","16","27","38","49","50","61","72","83","94","xx");
            $weeks = [];
            
            $currentDate = new \DateTime();
            for ($i = 0; $i < 26; $i++) {
                $weekStart = clone $currentDate;
                $weekStart->modify('last Monday');
        
                $weekEnd = clone $weekStart;
                $weekEnd->modify('next Sunday');
            
                // Format the dates and add to the array
                $weekDates = getWeekDates($weekStart->format('Y-m-d'), $weekEnd->format('Y-m-d'));
                  $day = [];
                foreach($weekDates as $key=>$weekDate){
                     $k=0;
                    foreach($weekDate as $key1=>$weekDatename){
                        $date = $weekDatename;
                        
                         $gamewinner = $this->gameWinner->where('stores_id',$request->stores_id)->where('created_at','like',$date.'%')->first();
                         $bit_open = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->open_time))  : '')  : '';
                         $bit_close = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->close_time))  : '')  : '';
                         $jodi =  isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                         $day[ $k]["colour"] =     in_array($jodi, $redjodi) ? 2 : 1 ;
                         $day[ $k]["sdate"]  =    $weekStart->format('Y-m-d');
                         $day[ $k]["edate"]  =    $weekEnd->format('Y-m-d');
                         $day[ $k]["open1"]  =    isset($bit_open[0]) ? $bit_open[0] : 'x';
                         $day[ $k]["open2"]  =    isset($bit_open[1]) ? $bit_open[1] : 'x';
                         $day[ $k]["open3"]  =    isset($bit_open[2]) ? $bit_open[2] : 'x';
                         $day[ $k]["close1"] =    isset($bit_close[1]) ? $bit_close[1] : 'x';
                         $day[ $k]["close2"] =    isset($bit_close[2]) ? $bit_close[2] : 'x';
                         $day[ $k]["close3"] =    isset($bit_close[3]) ? $bit_close[3] : 'x';
                         $day[ $k]["jodi"]   =    $jodi;
                         $day[ $k]["result"] =   isset($gamewinner) ? ( $gamewinner->close_time!= null ?  $gamewinner->open_time. $gamewinner->close_time   : 'xx')  : 'xxx-xx-xxx';
                         $k++;
                    }
                    
                }
             
               $weeks[$i]["week"] =  $day ;
                $currentDate->modify('-1 week');
            }
           $weeks = array_reverse($weeks);

$success['list'] = $weeks;
            // #return response with active state
            return $this->sendResponse( $success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }
    
    
         public function gameStarChart(Request $request)
    {
         try
        {
            $redjodi = array("00","11","22","33","44","55","66","77","88","99","05","16","27","38","49","50","61","72","83","94","xx");
            $weeks = [];
            
            $currentDate = new \DateTime();
            for ($i = 0; $i < 26; $i++) {
                $weekStart = clone $currentDate;
                $weekStart->modify('last Monday');
        
                $weekEnd = clone $weekStart;
                $weekEnd->modify('next Sunday');
            
                // Format the dates and add to the array
                $weekDates = getWeekDates($weekStart->format('Y-m-d'), $weekEnd->format('Y-m-d'));
                  $day = [];
                foreach($weekDates as $key=>$weekDate){
                     $k=0;
                    foreach($weekDate as $key1=>$weekDatename){
                        $date = $weekDatename;
                         $gamewinner = $this->newGameWinner->where('stores_id',$request->stores_id)->where('created_at','like',$date.'%')->first();
                         
                              
                         $bit_open = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->open_time))  : '')  : '';
                         $bit_close = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->close_time))  : '')  : '';
                         
                       
                        
                         $day[ $k]["result"] =   isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                         $jodi =  isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                          $day[ $k]["colour"] =     in_array($jodi, $redjodi) ? 2 : 1 ;
                          
                           $day[ $k]["sdate"]  =    $weekStart->format('Y-m-d');
                           $day[ $k]["edate"]  =    $weekEnd->format('Y-m-d');
                          $k++;
                    }
                    
                }
             
               $weeks[$i]["week"] =  $day ;
                $currentDate->modify('-1 week');
            }
           $weeks = array_reverse($weeks);

$success['list'] = $weeks;
            // #return response with active state
            return $this->sendResponse( $success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }
    public function gameStarPanelChart(Request $request)
    {
         try
        {
            
            $redjodi = array("00","11","22","33","44","55","66","77","88","99","05","16","27","38","49","50","61","72","83","94","xx");
            $weeks = [];
            
            $currentDate = new \DateTime();
            for ($i = 0; $i < 26; $i++) {
                $weekStart = clone $currentDate;
                $weekStart->modify('last Monday');
        
                $weekEnd = clone $weekStart;
                $weekEnd->modify('next Sunday');
            
                // Format the dates and add to the array
                $weekDates = getWeekDates($weekStart->format('Y-m-d'), $weekEnd->format('Y-m-d'));
                  $day = [];
                foreach($weekDates as $key=>$weekDate){
                     $k=0;
                    foreach($weekDate as $key1=>$weekDatename){
                        $date = $weekDatename;
                        
                         $gamewinner = $this->newGameWinner->where('stores_id',$request->stores_id)->where('created_at','like',$date.'%')->first();
                         $bit_open = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->open_time))  : '')  : '';
                         $bit_close = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->close_time))  : '')  : '';
                         $jodi =  isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                         $day[ $k]["colour"] =     in_array($jodi, $redjodi) ? 2 : 1 ;
                         $day[ $k]["sdate"]  =    $weekStart->format('Y-m-d');
                         $day[ $k]["edate"]  =    $weekEnd->format('Y-m-d');
                         $day[ $k]["open1"]  =    isset($bit_open[0]) ? $bit_open[0] : 'x';
                         $day[ $k]["open2"]  =    isset($bit_open[1]) ? $bit_open[1] : 'x';
                         $day[ $k]["open3"]  =    isset($bit_open[2]) ? $bit_open[2] : 'x';
                         $day[ $k]["close1"] =    isset($bit_close[1]) ? $bit_close[1] : 'x';
                         $day[ $k]["close2"] =    isset($bit_close[2]) ? $bit_close[2] : 'x';
                         $day[ $k]["close3"] =    isset($bit_close[3]) ? $bit_close[3] : 'x';
                         $day[ $k]["jodi"]   =    $jodi;
                         $day[ $k]["result"] =   isset($gamewinner) ? ( $gamewinner->close_time!= null ?  $gamewinner->open_time. $gamewinner->close_time   : 'xx')  : 'xxx-xx-xxx';
                         $k++;
                    }
                    
                }
             
               $weeks[$i]["week"] =  $day ;
                $currentDate->modify('-1 week');
            }
           $weeks = array_reverse($weeks);

$success['list'] = $weeks;
            // #return response with active state
            return $this->sendResponse( $success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }
    
    
    public function gameStarLineChart(Request $request){
        try{
            
           $redjodi = array("00","11","22","33","44","55","66","77","88","99","05","16","27","38","49","50","61","72","83","94","xx");
            $weeks = [];
            
            $currentDate = new \DateTime();
            for ($i = 0; $i < 26; $i++) {
                $weekStart = clone $currentDate;
                $weekStart->modify('last Monday');
        
                $weekEnd = clone $weekStart;
                $weekEnd->modify('next Sunday');
            
                // Format the dates and add to the array
                $weekDates = getWeekDates($weekStart->format('Y-m-d'), $weekEnd->format('Y-m-d'));
                
                  $day = [];
                foreach($weekDates as $key=>$weekDate){
                     $k=0;
                    foreach($weekDate as $key1=>$weekDatename){
                        $date = $weekDatename;
                        $dayOfWeek = date('l', strtotime($weekDatename));
                        
                        
                        $day_id = $this->day->where('name',$dayOfWeek)->first();
                        
                       $newstoregame = $this->newStoreGame->where('stores_day_id', $day_id->id)->where('stores_id', $request->stores_id)
                                   ->skip($request->index) 
                                   ->take(1) 
                                   ->first();

                        
                         $gamewinner = $this->newGameWinner->where('time_id',$newstoregame->id)->where('created_at','like',$date.'%')->first();
                         
                              
                         $bit_open = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->open_time))  : '')  : '';
                         $bit_close = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->close_time))  : '')  : '';
                         
                       
                        
                         $day[ $k]["result"] =   isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                         $jodi =  isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]. $bit_close[0]   : 'xx')  : 'xx';
                          $day[ $k]["colour"] =     in_array($jodi, $redjodi) ? 2 : 1 ;
                          
                           $day[ $k]["sdate"]  =    $weekStart->format('Y-m-d');
                           $day[ $k]["edate"]  =    $weekEnd->format('Y-m-d');
                          $k++;
                    }
                    
                }
             
               $weeks[$i]["week"] =  $day ;
                $currentDate->modify('-1 week');
            }
           $weeks = array_reverse($weeks);

$success['list'] = $weeks;
            // #return response with active state
            return $this->sendResponse( $success, 'walet.');
            
            
            
            
            
          return $this->sendResponse( $success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }
    
    
    
    public function gameStarLinePanelChart(Request $request){
         try
        {
            
            $redjodi = array("00","11","22","33","44","55","66","77","88","99","05","16","27","38","49","50","61","72","83","94","xx");
            $weeks = [];
            
            $currentDate = new \DateTime();
            for ($i = 0; $i < 26; $i++) {
                $weekStart = clone $currentDate;
                $weekStart->modify('last Monday');
        
                $weekEnd = clone $weekStart;
                $weekEnd->modify('next Sunday');
            
                // Format the dates and add to the array
                $weekDates = getWeekDates($weekStart->format('Y-m-d'), $weekEnd->format('Y-m-d'));
                  $day = [];
                foreach($weekDates as $key=>$weekDate){
                     $k=0;
                    foreach($weekDate as $key1=>$weekDatename){
                        $date = $weekDatename;
                        
                         $dayOfWeek = date('l', strtotime($weekDatename));
                        
                        
                        $day_id = $this->day->where('name',$dayOfWeek)->first();
                        
                       $newstoregame = $this->newStoreGame->where('stores_day_id', $day_id->id)->where('stores_id', $request->stores_id)
                                   ->skip($request->index) 
                                   ->take(1) 
                                   ->first();
                                   
        
                        
                         $gamewinner = $this->newGameWinner->where('time_id',$newstoregame->id)->where('created_at','like',$date.'%')->first();
                         
                         
                         $bit_open = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->open_time))  : '')  : '';
                         $bit_close = isset($gamewinner) ? ( $gamewinner->close_time!= null ?    (str_replace('-','',$gamewinner->close_time))  : '')  : '';
                         $jodi =  isset($gamewinner) ? ( $gamewinner->close_time!= null ?   $bit_open[3]   : 'x')  : 'x';
                         $day[ $k]["colour"] =     in_array($jodi, $redjodi) ? 2 : 1 ;
                         $day[ $k]["sdate"]  =    $weekStart->format('Y-m-d');
                         $day[ $k]["edate"]  =    $weekEnd->format('Y-m-d');
                         $day[ $k]["open1"]  =    isset($bit_open[0]) ? $bit_open[0] : 'x';
                         $day[ $k]["open2"]  =    isset($bit_open[1]) ? $bit_open[1] : 'x';
                         $day[ $k]["open3"]  =    isset($bit_open[2]) ? $bit_open[2] : 'x';
                         $day[ $k]["close1"] =    isset($bit_close[1]) ? $bit_close[1] : 'x';
                         $day[ $k]["close2"] =    isset($bit_close[2]) ? $bit_close[2] : 'x';
                         $day[ $k]["close3"] =    isset($bit_close[3]) ? $bit_close[3] : 'x';
                         $day[ $k]["jodi"]   =    $jodi;
                         $day[ $k]["result"] =   isset($gamewinner) ? ( $gamewinner->close_time!= null ?  $gamewinner->open_time. $gamewinner->close_time   : 'xx')  : 'xxx-xx-xxx';
                         $k++;
                    }
                    
                }
             
               $weeks[$i]["week"] =  $day ;
                $currentDate->modify('-1 week');
            }
           $weeks = array_reverse($weeks);

$success['list'] = $weeks;
            // #return response with active state
            return $this->sendResponse( $success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }
    
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function BannerList()
    {
         try
        {
            #Stat get 
            $success['appdetail'] = $this->banner->get();
            #return response with active state
            return $this->sendResponse($success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }
    
         /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function todayLucky()
    {
         try
        {
            #Stat get 
            $success['appdetail'] = $this->lockyNo->first();
            #return response with active state
            return $this->sendResponse($success, 'walet.');

        }catch(\Exception $ex)
        {
            return $this->sendError('Validation Error.', $ex->getMessage()); 
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

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