<?php

namespace App\Http\Controllers\Resources;

use Illuminate\Http\Request;
use App\Http\Requests\CustomerRequest;
use App\Http\Requests\ExportRequest;
use App\Http\Requests\ImportRequest;
use App\Http\ReportGenerator\PdfReport;
use App\Http\ReportGenerator\ExcelReport;
use App\Http\Controllers\Controller;
use App\Http\Filters\QueryBuilder;

use App\Customer;

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $queryBuilder = new QueryBuilder(new Customer, $request);

        return response()->json($queryBuilder->build()->paginate(10),200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  App\Http\Requests\CustomerRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(CustomerRequest $request)
    {
      return response()->json(tap(new Customer($request->all()), function ($customer) {
          $customer->save();
      }), 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      $customer=Customer::find($id);

  		if (!$customer) {
  			return response()->json(['errors'=>array(['code'=>404,'message'=>__('customers.not_find')])],404);
  		}

  		return response()->json(['status'=>'ok','data'=>$customer],200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  App\Http\Requests\CustomerRequest $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CustomerRequest $request, $id)
    {
        $customer=Customer::find($id);

    		if (!$customer) {
    			return response()->json(['errors'=>array(['code'=>404,'message'=>__('customers.not_find')])],404);
    		}

        return tap($customer)->update($request->all());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(CustomerRequest $request, $id)
    {
      $customer=Customer::find($id);

      if (!$customer) {
        return response()->json(['errors'=>array(['code'=>404,'message'=>__('customers.not_find')])],404);
      }

      $customer->delete();

      return response()->json(['code'=>204,'message'=>__('strings.deleted_successfully')],204);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  array  $id
     * @return \Illuminate\Http\Response
     */
    public function destroyMany(CustomerRequest $request)
    {
      foreach ($request->input('ids') as $id) {
        $customer=Customer::find($id);

        if (!$customer) {
          return response()->json(['errors'=>array(['code'=>404,'message'=>__('customers.not_find')])],404);
        }
      }

      foreach ($request->input('ids') as $id) {
        $customer=Customer::find($id);

        $customer->delete();
      }

      return response()->json(['code'=>204,'message'=>__('strings.deleted_successfully')],204);
    }

    /**
     * Display report from a listing of the resource.
     *
     * @param  App\Http\Requests\ExportRequest $request
     * @return PDF CSV or XLS
     */
    public function export(ExportRequest $request)
    {
      $format = $request->input('format');
      $orderBy = $request->input('order_by');
      $betweenDates = ($request->input('between_dates')) ? explode(',', $request->input('between_dates')): ['',''];

      $queryBuilder = new QueryBuilder(new Customer, $request);

      $meta = [ // For displaying filters description on header
          __('strings.registered_on') => $betweenDates[0].' - '.$betweenDates[1],
          __('strings.order_by') => $orderBy,
      ];

      $columns = [ // Set Column to be displayed
          __('customers.contact') => 'contact',
          __('customers.state') => 'state',
          __('customers.email') => 'email',
      ];

      switch ($format) {
        case 'pdf':
          $report = new PdfReport();
          return $report->of(__('customers.report_title'), $meta, $queryBuilder->build()->get(), $columns)->download(__('customers.report_title'));
        case 'excel':
          $report = new ExcelReport();
          return $report->of(__('customers.report_title'), $meta, $queryBuilder->build()->get(), $columns)->setView('reports.list-excel-template')->download(__('customers.report_title'));
        case 'csv':
          $report = new ExcelReport();
          return $report->of(__('customers.report_title'), $meta, $queryBuilder->build()->get(), $columns)->simple()->setFormat('csv')->download(__('customers.report_title'));
        default:
          $report = new PdfReport();
          return $report->of(__('customers.report_title'), $meta, $queryBuilder->build()->get(), $columns)->stream();
      }
    }

    /**
     * Display report from a listing of the resource.
     *
     * @param  App\Http\Requests\ImportRequest $request
     * @return \Illuminate\Http\Response
     */
    public function import(ImportRequest $request)
    {

    }
}
