<?php

namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Carbon\Carbon;
use getID3;
use DB;

use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;

class ActionrapideController extends Controller
{
    
    //--------------------------------------------------------------------------  API DE POUR DEMANDER UNE PRIERE D'URGENCE --------------------------------------------------------------------------------
    
    public function demande_prayer_urgence(Request $request)
    {
        $userid=$request->get('id_user');
        $message=$request->get('message');
        
        $listgroupapartenance = DB::table('ebm_rejoindre_groupe')
        ->where('id_user', $userid)
        ->get();
        
        $usersdata = DB::table('ebm_users')->where('id_user', $userid)->first();
        if(isset($usersdata->id_user)){$senderName=$usersdata->nom.' '.$usersdata->prenom;}
        
        if ($listgroupapartenance->isNotEmpty())
        {
            foreach ($listgroupapartenance as $rowlistgroupapartenance)
            {
                DB::table('ebm_groupe_posts')->insert(
                    [
                        'id_groupe' => $rowlistgroupapartenance->id_groupe,
                        'id_user' => $userid,
                        'content' => $message, 
                        'date_publication' => DB::raw('NOW()')
                    ]
                );
                
                
                $datanotifygroupe = DB::table('ebm_rejoindre_groupe') 
                ->join('ebm_users', 'ebm_rejoindre_groupe.id_user', '=', 'ebm_users.id_user') 
                ->join('ebm_groupe', 'ebm_rejoindre_groupe.id_groupe', '=', 'ebm_groupe.id_groupe')
                ->select('ebm_rejoindre_groupe.*','titregroupe','email','fcm_token','nom','prenom') 
                ->where('ebm_rejoindre_groupe.id_groupe', '=', $rowlistgroupapartenance->id_groupe)
                ->where('ebm_users.id_user', '!=', $userid)
                //->orderBy('id', 'desc')
                ->get();
                
                $firebase = (new Factory)->withServiceAccount(base_path('ebmmission-1b301-firebase-adminsdk-fbsvc-639a650b4d.json'));   
                
                foreach ($datanotifygroupe as $rowdatanotifygroupe) 
                {
                    //--------------------------------------------------- NOUVEAU CONTENUS -----------------------------------------------------------
                    
                    $varemail= $rowdatanotifygroupe->email; 
                    $varfcmtoken= $rowdatanotifygroupe->fcm_token;
                    //$senderName=$rowdatanotifygroupe->nom.' '.$rowdatanotifygroupe->prenom;
                    $groupName= $rowdatanotifygroupe->titregroupe;
                    
                    $subject="Nouveau message (demande d'une prière d'urgence) dans le groupe $groupName";
                    //$html = "Votre hébergement vient d'être réservé par $valnameusersx ,nombre de chambre réservé : $valnbofroom";
                    $html = "$senderName : $message";
                    
                    if($rowdatanotifygroupe->notifymsgpush==1)
                    {
                        try
                        {
                            $messaging = $firebase->createMessaging();     
                            $messagex = CloudMessage::fromArray([ 
                                'token' => $varfcmtoken,
                                'notification' => [
                                    'title' => $subject,
                                    'body' => $html
                                ],
                                //'topic' => 'global'
                            ]);
                            $messaging->send($messagex);
                            
                        }catch(\Exception $ex){}
                        
                    }
                    if($rowdatanotifygroupe->notifymsgemail==1)
                    {
                        
                        
                    }
                }
            }
        }
        
        DB::table('ebm_forum_posts')->insert(
            [
                'id_user' => $userid,
                'content' => $message, 
                'date_post' => DB::raw('NOW()')
            ]
        );
        
        $datanotify = DB::table('ebm_user_notify_preferences') 
        ->join('ebm_users', 'ebm_user_notify_preferences.id_user', '=', 'ebm_users.id_user')
        ->select('ebm_user_notify_preferences.*','email','fcm_token','nom','prenom') 
        ->where('ebm_users.id_user', '!=', $userid)
        //->orderBy('id', 'desc')
        ->get();
        
        $firebase = (new Factory)->withServiceAccount(base_path('ebmmission-1b301-firebase-adminsdk-fbsvc-639a650b4d.json'));   
        
        foreach ($datanotify as $rowdatanotify) 
        {
            $varemail= $rowdatanotify->email; 
            $varfcmtoken= $rowdatanotify->fcm_token;
            //$senderName=$rowdatanotify->nom.' '.$rowdatanotify->prenom;
            
            $subject="Nouveau message du forum (demande d'une prière d'urgence)";
            //$html = "Votre hébergement vient d'être réservé par $valnameusersx ,nombre de chambre réservé : $valnbofroom";
            $html = "$senderName : $message";
            
            if($rowdatanotify->notifypush==1)
            {
                try
                {
                    $messaging = $firebase->createMessaging();     
                    $messagex = CloudMessage::fromArray([ 
                        'token' => $varfcmtoken,
                        'notification' => [
                            'title' => $subject,
                            'body' => $html
                        ],
                        //'topic' => 'global'
                    ]);
                    $messaging->send($messagex);
                    
                }catch(\Exception $ex){}
                
            }
        }
        
        return response()->json([
            'success' => 'true'
        ]);
            
    }
    
    //--------------------------------------------------------------------------  API DE POUR FAIRE UNE PARTAGE RAPIDE --------------------------------------------------------------------------------
    
    public function partage_rapide(Request $request)
    {
        $userid=$request->get('id_user');
        $message=$request->get('message');
        
        $usersdata = DB::table('ebm_users')->where('id_user', $userid)->first();
        if(isset($usersdata->id_user)){$senderName=$usersdata->nom.' '.$usersdata->prenom;}
        
        /*
        $listgroupapartenance = DB::table('ebm_rejoindre_groupe')
        ->where('id_user', $userid)
        ->get();
        
        if ($listgroupapartenance->isNotEmpty())
        {
            foreach ($listgroupapartenance as $rowlistgroupapartenance)
            {
                DB::table('ebm_groupe_posts')->insert(
                    [
                        'id_groupe' => $rowlistgroupapartenance->id_groupe,
                        'id_user' => $userid,
                        'content' => $message, 
                        'date_publication' => DB::raw('NOW()')
                    ]
                );
            }
        }
        */
        
        DB::table('ebm_forum_posts')->insert(
            [
                'id_user' => $userid,
                'content' => $message, 
                'date_post' => DB::raw('NOW()')
            ]
        );
        
        $datanotify = DB::table('ebm_user_notify_preferences') 
        ->join('ebm_users', 'ebm_user_notify_preferences.id_user', '=', 'ebm_users.id_user')
        ->select('ebm_user_notify_preferences.*','email','fcm_token','nom','prenom') 
        ->where('ebm_users.id_user', '!=', $userid)
        //->orderBy('id', 'desc')
        ->get();
        
        $firebase = (new Factory)->withServiceAccount(base_path('ebmmission-1b301-firebase-adminsdk-fbsvc-639a650b4d.json'));   
        
        foreach ($datanotify as $rowdatanotify) 
        {
            $varemail= $rowdatanotify->email; 
            $varfcmtoken= $rowdatanotify->fcm_token;
            //$senderName=$rowdatanotify->nom.' '.$rowdatanotify->prenom;
            
            $subject="Nouveau message du forum (demande d'une prière d'urgence)";
            //$html = "Votre hébergement vient d'être réservé par $valnameusersx ,nombre de chambre réservé : $valnbofroom";
            $html = "$senderName : $message";
            
            if($rowdatanotify->notifypush==1)
            {
                try
                {
                    $messaging = $firebase->createMessaging();     
                    $messagex = CloudMessage::fromArray([ 
                        'token' => $varfcmtoken,
                        'notification' => [
                            'title' => $subject,
                            'body' => $html
                        ],
                        //'topic' => 'global'
                    ]);
                    $messaging->send($messagex);
                    
                }catch(\Exception $ex){}
                
            }
        }
        
        return response()->json([
            'success' => 'true'
        ]);
            
    }
    
}
