<?php

namespace App\Http\Controllers;

use App\FireBase;
use App\News;
use App\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class NewsController extends Controller
{
    public function add()
    {
        return view("news_add");
    }

    public function index()
    {
        $news = News::orderby('id','desc')->get();
        foreach ($news as $ns)
        {
            $ns['description'] =strip_tags($ns->description);
            $ns['photo_url'] ='http://45.33.43.51/norka/public'.$ns->photo_url;
        }
        $data['news'] = $news;
        $data['photo_url'] = '45.33.43.51/norka/public'.$news;
        $data['status'] = 1;
        $data['message'] = "News retrieved successfully";
        return response($data, 200);

    }

    public function show()
    {
        $news = News::orderby('id','desc')->get();
        $data['news'] = $news;
        return view("news_list", compact('news'));

    }

    public function view($id)
    {
        $news = News::find($id);
        return view("news_view", compact('news'));
    }

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

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

        if ($validator->passes()) {
            $news = new News();
            $news->notification_type_id = $input['notification_type_id'];
            $news->title = $input['title'];
            $news->description = $input['description'];
            $news->news_date = date("Y-m-d h:i:s", strtotime($request->news_date));
            $news->photo_url = $news::creatNew($input['news_photo'], 800, '');
            $news->save();
            if ($news) {
                $data['status'] = 1;
                $data['news_id'] = $news->id;
                $data['message'] = "News 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) {
                    FireBase::pushToAll([
                        'notification' => [
                            'title' => substr($news->title, 0, 100),
                            'text' => substr(strip_tags($news->description), 0, 200),
                            'sound' =>'default',
                            'click_action' =>'FCM_PLUGIN_ACTIVITY'
                        ],
                        'data' => [
                            'type' => 'news'
                        ]
                    ],$devicetokens);
                }
                return response($data, 200);

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

    }

}
