<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Carbon\Carbon;
use OneSignal;
use App\Models\Suppliers;
use App\Models\Categories;
use App\Models\Postulations;
use App\Models\FinishedContracts;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Grimzy\LaravelMysqlSpatial\Types\Point;

class PostulationController extends Controller
{
    //
    public function index(Request $request)
    {
        //
        $request->session()->put('search', $request
              ->has('search') ? strip_tags($request->get('search')) : ($request->session()
              ->has('search') ? strip_tags($request->session()->get('search')) : ''));

        $request->session()->put('field', $request
                ->has('field') ? strip_tags($request->get('field')) : ($request->session()
                ->has('field') ? strip_tags($request->session()->get('field')) : 'id'));

        $request->session()->put('sort', $request
                ->has('sort') ? strip_tags($request->get('sort')) : ($request->session()
                ->has('sort') ? strip_tags($request->session()->get('sort')) : 'asc'));

        //$headers = $request->get('header') != '' ? $request->get('header') : -1;

        $postulations = new Postulations();
        $postulations = $postulations->where('id', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                              ->orwhere('user_id', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                              ->orwhere('address', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                              ->orwhere('amount', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                        ->orderBy(strip_tags($request->session()->get('field')), strip_tags($request->session()->get('sort')))
                        ->paginate(10);

        if ($request->ajax()) {
            return view('postulations.index', compact('postulations'));
          } else {
            return view('postulations.ajax', compact('postulations'));
          }

    }

    public function map(Request $request)
    {
        //
        $request->session()->put('search', $request
              ->has('search') ? strip_tags($request->get('search')) : ($request->session()
              ->has('search') ? strip_tags($request->session()->get('search')) : ''));

        $request->session()->put('field', $request
                ->has('field') ? strip_tags($request->get('field')) : ($request->session()
                ->has('field') ? strip_tags($request->session()->get('field')) : 'id'));

        $request->session()->put('sort', $request
                ->has('sort') ? strip_tags($request->get('sort')) : ($request->session()
                ->has('sort') ? strip_tags($request->session()->get('sort')) : 'asc'));

        //$headers = $request->get('header') != '' ? $request->get('header') : -1;

        $postulations = new Postulations();
        $postulations = $postulations->where('id', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                              ->orwhere('user_id', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                              ->orwhere('address', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                              ->orwhere('amount', 'LIKE', '%' . strip_tags($request->session()->get('search')) . '%')
                        ->orderBy(strip_tags($request->session()->get('field')), strip_tags($request->session()->get('sort')))
                        ->paginate(10);

      return view('postulations.map', compact('postulations'));

    }

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

      $rules = [
        'category' => 'required|string',
        'address' => 'required|string',
        'int_number' => 'numeric|nullable',
        'references' => 'string|nullable',
        'setdate' => 'required|string',
        'sethour' => 'required|string',
        'details' => 'string|nullable',
        'lat' => 'required|numeric',
        'lng' => 'required|numeric',
      ];

      $validator = Validator::make($request->all(), $rules);
      if ($validator->fails()) {
        return response()->json($validator->messages());
      } else {

        $user = Auth::user();
        $geometry = new Point($request->lat, $request->lng);
        $category = Categories::where('name', strip_tags($request->category))->orwhere('en_name', strip_tags($request->category))->first();

        $distance = 0.5;
        $suppliers = Suppliers::distance('location', $geometry, $distance)->get();

        if ($suppliers != '[]') {

          $postulation = new Postulations();
          $postulation->user_id = $user->id;
          $postulation->category_id = $category->id;
          $postulation->address = strip_tags($request->address);
          $postulation->location = $geometry;
          $postulation->int_number = $request->int_number;
          $postulation->references = preg_replace('/\d+/', '', strip_tags($request->references));
          $timeoffset = str_replace("0", "", substr(substr(strip_tags($request->sethour), 23), 0, 3));
          $postulation->appointment = Carbon::createFromFormat('Y-m-d H:i:s', (substr(strip_tags($request->setdate), 0, 10) . ' ' . substr(substr(strip_tags($request->sethour), 11), 0, 8)), $timeoffset)->tz('UTC');
          $postulation->amount = 5000;
          $postulation->details = preg_replace('/\d+/', '', strip_tags($request->details));
          $postulation->save();

          OneSignal::sendNotificationUsingTags(
             "Coméntele al Ing. que hay una postulación",
             array(
                 ["field" => "tag", "key" => "iChamba_ID", "relation" => "=", "value" => "128"]
             ),
             $url = null,
             $data = null,
             $buttons = null,
             $schedule = null,
             $headings = "Admin: hay nueva postulación"
          );

          foreach ($suppliers as $supplier) {
            if (in_array($category->id, $supplier->categories->pluck('id')->toArray())) {
              OneSignal::sendNotificationUsingTags(
                 "Dirígete a la sección de postulaciones en la app para ver más detalles",
                 array(
                     ["field" => "tag", "key" => "iChamba_ID", "relation" => "=", "value" => $supplier->user_id]
                 ),
                 $url = null,
                 $data = null,
                 $buttons = null,
                 $schedule = null,
                 $headings = "Proveedor: hay nueva postulación"
              );
            }
          }

          $minutes = intval(substr(substr($request->setdate, 14), 0, 2) + 15);
          $hours = intval(substr(substr($request->setdate, 11), 0, 2) + 1);

          if ($minutes > 59) {
            if ($hours > 23){
              $delay_msg = Carbon::now()->addDays(1)->toDateString() . ' ' . ($hours - 24) . ':' . ($minutes - 60) . substr(substr($request->setdate, 16), 0, 3);
            } else {
              $delay_msg = substr($request->sethour, 0, 10) . ' ' . $hours . ':' . ($minutes - 60) . substr(substr($request->setdate, 16), 0, 3);
            }
          } else {
            $delay_msg = substr($request->sethour, 0, 10) . ' ' . $hours . ':' . $minutes . substr(substr($request->setdate, 16), 0, 3);
          }

          $delay_UTC = Carbon::now()->addMinutes(15)->toString();

          OneSignal::sendNotificationUsingTags(
             "Dirígete a la sección de contratos en la app para ver más detalles",
             array(
                 ["field" => "tag", "key" => "iChamba_ID", "relation" => "=", "value" => $user->id]
             ),
             $url = null,
             $data = null,
             $buttons = null,
             $schedule = $delay_UTC,
             $headings = "Búsqueda Finalizada"
          );

          return response()->json([
              'message' => 'Servicio solicitado, espere a que un proveedor se postule'
          ]);
        } else {
          return response()->json([
              'message' => 'No Provider'
          ]);
        }
      }
    }

    public function postulate(Request $request) {

      $rules = [
        'postulation_id' => 'required|numeric',
      ];

      $validator = Validator::make($request->all(), $rules);
      if ($validator->fails()) {
        return redirect()->back()->withInput($request->all())->withErrors($validator);
      } else {

        $user = Auth::user();
        $postulation = Postulations::where('id', $request->postulation_id)->first();
        $time_created = Carbon::parse($postulation->created_at);
        $time_limit = (9900 - Carbon::now()->diffInMinutes($time_created));
        $supplier = Suppliers::where('user_id', $user->id)->first();

        if ($time_limit > 0) {
          if (in_array($postulation->category_id, $supplier->categories->pluck('id')->toArray())) {
            if($supplier->membership == 1) {
              OneSignal::sendNotificationUsingTags(
                 "Dirígete a la sección de contratos en la app para ver más detalles",
                 array(
                     ["field" => "tag", "key" => "iChamba_ID", "relation" => "=", "value" => $postulation->user_id]
                 ),
                 $url = null,
                 $data = null,
                 $buttons = null,
                 $schedule = null,
                 $headings = "Un proveedor certificado se ha postulado"
              );
            }

          $supplier->postulations()->attach($request->postulation_id);
          $supplier->save();

          return response()->json([
              'message' => 'Se ha postulado al servicio exitosamente'
          ]);
        }

      } else {
          return response()->json([
              'message' => 'La postulación ha caducado'
          ]);
        }
      }
    }

    public function getpendingcontracts(Request $request) {
      $user = Auth::user();
      $postulations = Postulations::where('user_id', $user->id)->orderBy('created_at', 'DESC')->get();
      $pendingcontracts  = array();

        foreach($postulations as $postulation) {
          $time_limit = Carbon::parse($postulation->appointment);
          if ($time_limit->diffInMinutes(Carbon::now(), false) <= -60) {
            $category = Categories::where('id', $postulation->category_id)->first();
            $pendingcontractinfo = array(
              'id' => $postulation->id,
              'category' => $category->name,
              'en_category' => $category->en_name,
              'address' => $postulation->address,
              'date' => $postulation->appointment,
              'amount' => $postulation->amount
            );
            $pendingcontracts[] = $pendingcontractinfo;
          }
        }

        return response()->json($pendingcontracts);
    }

    public function getfinishedpostulations(Request $request) {
      $user = Auth::user();
      $postulations = FinishedContracts::where('supplier_id', $user->suppliers->id)->orderBy('created_at', 'DESC')->get();
      $finishedpostulations  = array();

        foreach($postulations as $postulation) {
          $time_limit = Carbon::parse($postulation->appointment);
            $category = Categories::where('id', $postulation->category_id)->first();
            $finishedpostulationinfo = array(
              'id' => $postulation->id,
              'category' => $category->name,
              'en_category' => $category->en_name,
              'address' => $postulation->address,
              'date' => $postulation->appointment,
              'amount' => $postulation->amount
            );
            $finishedpostulations[] = $finishedpostulationinfo;
        }

        return response()->json($finishedpostulations);
    }

    public function getpostulants(Request $request) {

      $rules = [
        'postulation_id' => 'required|numeric',
      ];

      $validator = Validator::make($request->all(), $rules);
      if ($validator->fails()) {
        return redirect()->back()->withInput($request->all())->withErrors($validator);
      } else {

        $user = Auth::user();
        $postulation = Postulations::where('id', $request->postulation_id)->first();

        if ($postulation->user_id == $user->id) {
          $category = Categories::where('id', $postulation->category_id)->first();
          $suppliers = Suppliers::whereHas('postulations', function($q) use ($request) {
            $q->where('postulations_id', $request->postulation_id);
          })->get();

          $pcontractsuppliers = array();

          if ($suppliers != '[]') {
            foreach($suppliers as $supplier) {
              $pcontractsupplier = array(
                'id' => $postulation->id,
                'category' => $category->name,
                'en_category' => $category->en_name,
                'address' => $postulation->address,
                'date' => $postulation->appointment,
                'amount' => $postulation->amount,
                'supplier_id' => $supplier->id,
                'supplier' => $supplier->company_name,
                'tags' => $supplier->tags,
                'cover_photo' => $supplier->cover_photo,
                'membership' => $supplier->membership,
                'fee' => $supplier->minimun_fee,
                'score' => round($supplier->total_score/$supplier->finished_jobs, 1),
              );
              $pcontractsuppliers[] = $pcontractsupplier;
            }

            $pcontractsuppliercollection = collect($pcontractsuppliers)->sortByDesc('membership')->sortByDesc('score');
            $pcontractsupplier = $pcontractsuppliercollection->values()->all();

          } else {
              $pcontractsupplier = array(
                'id' => $postulation->id,
                'category' => $category->name,
                'en_category' => $category->en_name,
                'address' => $postulation->address,
                'date' => $postulation->appointment,
                'amount' => $postulation->amount,
                'supplier_id' => null,
                'supplier' => null,
                'tags' => null,
                'cover_photo' => null,
                'membership' => null,
                'fee' => null,
                'score' => null,
              );

            $pcontractsuppliers[] = $pcontractsupplier;
            $pcontractsupplier = $pcontractsuppliers;
          }
          return response()->json($pcontractsupplier);
        }
      }
    }

    public function deleteexpired()
    {
      $postulations = Postulations::whereDate('appointment', '<', Carbon::now())->delete();
    }

    public function destroy($id)
    {
      Postulations::destroy($id);
      return redirect('postulations');
    }

}
