<?php

namespace App\Http\Controllers;

use App\FireBase;
use App\NativeAddress;
use App\Notification;
use App\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Mattbrown\Laracurl\Facades\Laracurl;

class NotificationController extends Controller
{
    public function add()
    {
        $client = new \GuzzleHttp\Client();
        $response = $client->request('GET', 'http://www.ifm.co.in/webservice/listcountry');
        $country = json_decode($response->getBody()->getContents())->countryList;
        $client = new \GuzzleHttp\Client();
        $response = $client->request('GET', 'http://www.ifm.co.in/webservice/liststate?country_id=101');
        $state = json_decode($response->getBody()->getContents())->stateList;
        return view("notification_add", compact('country', 'state'));
    }

    public function getStates(Request $request)
    {
        $countryId = $request->countries;
        $client = new \GuzzleHttp\Client();
        $response = $client->request('GET', 'http://www.ifm.co.in/webservice/liststate?country_id=' . $countryId);
        $state = json_decode($response->getBody()->getContents())->stateList;
        return $state;
    }

    public function showAll()
    {
        //     $response = Laracurl::get('http://www.ifm.co.in/webservice/listcountry');


        $notification = Notification::orderby('id', 'desc')->get();
        $data['notification'] = $notification;

        return view("notification_list", compact('notification'));
    }

    public function index(Request $request)
    {
        $userid = $request->userid;
        if ($request->id == null) {
            $district = NativeAddress::where('user_id', '=', $userid)->pluck('district');
            $notification = Notification::where('district', '=', strval($district[0]))->take(50)->get();
            $arr = [];
            $d = [];
            foreach ($notification as $key => $not) {

                if (in_array($not->notification_date, $arr)) {

                } else {
                    $arr[] = $not->notification_date;
                    $d[$key]['date'] = $not->notification_date;
                    $d[$key]['notification'] = Notification::where('notification_date', $not->notification_date)
                        ->where('district', '=', strval($district[0]))->get();
                }

            }

            $data['notification'] = $d;
            $data['status'] = 1;
            $data['message'] = "Notifications retrieved successfully";
            return response($data, 200);
        } else {
            $data['notification'] = $this->show($request->id);
            return response($data, 200);
        }

    }

    public function show($id)
    {
        return Notification::with(['NotificationType', 'NotificationType.id'])->find($id);
    }

    public function store(Request $request)
    {
        $input = $request->all();
        $rules = [
            'notification_type_id' => 'required',
            'title' => 'required',
            'description' => 'required',
            'notification_date' => 'required',

        ];
        $messages = [
            'notification_type_id.required' => 'Notification type required',
            'title.required' => 'Title required',
            'description.required' => 'Description required',
            'notification_date.required' => 'Notification date required'
        ];
        $validator = Validator::make($input, $rules, $messages);

        if ($validator->passes()) {
            $not = new Notification();
            $not->notification_type_id = $input['notification_type_id'];
            $not->title = $input['title'];
            $not->description = $input['description'];
            $not->notification_date = date("Y-m-d h:i:s", strtotime($request->notification_date));
            $not->save();
            $category = $input['category'];
            $country = $input['country'];
            $state = $input['state'];
            if ($not) {
                $data['status'] = 1;
                $data['notification_id'] = $not->id;
                $data['message'] = "Notification added successfully";
                $devicetokens = DB::table('device as dv')
                    ->join('users as us', 'dv.user_id', '=', 'us.id')
                    ->select('dv.device_token')
                    ->get();

                if ($input['notification_type_id'] == 1) {
                    if ($category == "country") {
                        FireBase::pushTopic([
                            'notification' => [
                                'title' => substr($not->title, 0, 100),
                                'text' => substr(strip_tags($not->description), 0, 200),
                                'sound' => 'default',
                                'click_action' => 'FCM_PLUGIN_ACTIVITY'
                            ],
                            'data' => [
                                'type' => 'notification'
                            ]
                        ], $country);
                    } else if ($category == "state") {
                        FireBase::pushTopic([
                            'notification' => [
                                'title' => substr($not->title, 0, 100),
                                'text' => substr(strip_tags($not->description), 0, 200),
                                'sound' => 'default',
                                'click_action' => 'FCM_PLUGIN_ACTIVITY'
                            ],
                            'data' => [
                                'type' => 'notification'
                            ]
                        ], $state);
                    } else {
                    }
//                    FireBase::pushToAll([
//                        'notification' => [
//                            'title' => substr($not->title, 0, 100),
//                            'text' => substr(strip_tags($not->description), 0, 200),
//                            'sound' =>'default',
//                            'click_action' =>'FCM_PLUGIN_ACTIVITY'
//                        ],
//                        'data' => [
//                            'type' => 'notification'
//                        ]
//                    ],$devicetokens);
                }
                return response($data, 200);

            }
        } else {
            $data['message'] = $validator->errors(0);
            $data['status'] = 0;
            return response($data, 400);
        }

    }
}
