<?php

namespace App\Repositories;

use Carbon\Carbon;
use App\Models\SiteSettings;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;


/**
 * Class SiteSettingsRepository.
 */
class SiteSettingsRepository extends BaseRepository
{
    /**
     * @return string
     */
    public function model()
    {
        return SiteSettings::class;
    }

    
    /**
     * @param array $where
     *
     * @return array|bool
     * @throws GeneralException
     */
    public function findByCondition($where='')
    {
        if($where){
            $user = $this->model
                ->where($where)
                ->orderBy('id','DESC')
                ->get();
        } else {
            $user = $this->model
                ->orderBy('id','DESC')
                ->get();
        } 
        return $user;
    }

    /**
     * @param array $where
     *
     * @return array|bool
     * @throws GeneralException
     */
    public function getBankDetails($where='')
    {
        $user = $this->model
                ->where('keyword','like','%bank%')
                ->orderBy('id','DESC')
                ->get();
        return $user;
    }

    /**
     * @param array $where
     *
     * @return array|bool
     * @throws GeneralException
     */
    public function getAboutDetails()
    {
        $user = $this->model
                ->where('keyword','like','%about_us%')
                ->orderBy('id','DESC')
                ->get();
        return $user;
    }


    /**
     * @param string $device_token
     * @param int $mobile_number
     * @param string $circle_code
     *
     * @return array|bool
     * @throws GeneralException
     */
    public function notification($device_tokens='', $circle_code='', $content='')
    {
        if($device_tokens==''){
            $device_tokens='64a105c1-296f-42ba-a4fe-db59541a979f';
        }
        if($content=='') {
            $content = array(
                "en" => 'DANT Notification d\'adhésion au cercle pour le code de cercle:'.$circle_code
            );
        }
        
        $details = SiteSettings::where('keyword', 'one_signal_app_id')->first();
        $one_signal_app_id = $details ? $details->value : 'a7be7046-5ab6-447c-bc1e-0d95d1217191';
        $fields = array(
            'app_id' => $one_signal_app_id,
            'include_player_ids' => array($device_tokens),
            'data' => array("notification_type" => "joining_notification", "circle_code" => $circle_code),
            'contents' => $content
        );
        
        $fields = json_encode($fields);
        //print("\nJSON sent:\n");
        //print_r($fields);
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                       'Authorization: Basic NDExNGIwY2UtM2JiYS00MjZmLTg1MzAtYzlmZmY4MjA5YzI0'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $response = curl_exec($ch);
        curl_close($ch);
        
        $output = (array)json_decode($response);
        //print_r($output);die;
        if($output){
            if(isset($output['recipients'])){
                return $output['recipients'];
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }


    /**
     * @param string $device_token
     * @param int $mobile_number
     * @param string $circle_code
     *
     * @return array|bool
     * @throws GeneralException
     */
    public function notificationV2($device_tokens='', $circle_code='', $content='')
    {
        $device_tokens='';
        /*if($device_tokens==''){
            $device_tokens='64a105c1-296f-42ba-a4fe-db59541a979f';
        }*/
        if($content=='') {
            $content = array(
                "en" => 'DANT Notification d\'adhésion au cercle pour le code de cercle:'.$circle_code
            );
        }
        
        $details = SiteSettings::where('keyword', 'one_signal_app_id')->first();
        $one_signal_app_id = $details ? $details->value : 'a7be7046-5ab6-447c-bc1e-0d95d1217191';
        $fields = array(
            'app_id' => $one_signal_app_id,
            'include_player_ids' => array($device_tokens),
            'data' => array("notification_type" => "joining_notification", "circle_code" => $circle_code),
            'contents' => $content
        );
        
        $fields = json_encode($fields);
        print("\nJSON sent:\n");
        //print_r($fields);
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                       'Authorization: Basic NDExNGIwY2UtM2JiYS00MjZmLTg1MzAtYzlmZmY4MjA5YzI0'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $response = curl_exec($ch);
        curl_close($ch);
        
        $output = (array)json_decode($response);
        //print_r($output);
        if($output){
            if(isset($output['recipients'])){
                return $output['recipients'];
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }
}
