<?php

namespace App\Services;

use App\Traits\VesselLineUpGraphTrait;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class VesselLineUpGraphService
{
    use VesselLineUpGraphTrait;

    public function applyFilters($dataMapping, $data, $getData)
    {
        foreach ($dataMapping as $key => $mappingKey) {
            $value = $data[$key];
            if ($value != "") {
                $conditionalFunction = $mappingKey['condition'];
                $values = $mappingKey['shouldExplode'] ? explode(',', $value) : $value;
                if (!empty($mappingKey['statement']['key'])) {
                    $stateMentValue = $data[$mappingKey['statement']['key']];
                    if (in_array($stateMentValue, $mappingKey['statement'][$mappingKey['statement']['key']]['shouldContain'])) {
                        $column = $mappingKey['statement'][$mappingKey['statement']['key']]['column'];
                    } else {
                        $column = $mappingKey['column'];
                    }
                } else {
                    $column = $mappingKey['column'];
                }
				if(!($column=='commodity' && $values == 'All'))
					$getData = $getData->$conditionalFunction($column, $values);
            }
        }
        return $getData;
    }

    public function prepareWorkingData($data, $getData)
    {
        $typeWises = ["sub-commodity", "vessel-size", "shipper", "country", "port"];
        $typeWise = $data['type_wise'];

        if (in_array($typeWise, $typeWises)) {
            $getData = $this->combineAllTypeWiseData($getData, $typeWise);
            $getData = $getData->groupBy('status');

            $response = $this->getTypeWiseSortedData($getData, $typeWise);
        } else {
            $response = $getData->selectRaw("sum((CASE WHEN status = 'Anchorage' THEN qty END)) AS Anchorage")
                ->selectRaw("sum((CASE WHEN status = 'Berth' THEN qty END)) AS Berth")
                ->selectRaw("sum((CASE WHEN status = 'Expected' THEN qty END)) AS Expected")
                ->first();
        }
        return $response;
    }

    public function returnGraphResult($response, $data)
    {
        if (!empty($response)) {
            return response()->json(['status' => 1001, 'message' => 'Data Found successfully', 'data' => [
                'graph_type' => empty($data['type_wise']) ? 'Parent graph' : 'child graph (' . $data['type_wise'] . ' wise)',
                'graph_points' => $response
            ]]);
        } else {
            return response()->json(['status' => 401, 'message' => 'No Data Found'], 401);
        }
    }

    public function applySelectFilter($dataMapping, $data, $getData, $type = 'working')
    {
        $isMatched = false;
        foreach ($dataMapping as $key => $mappingKey) {
            if ($data['type_wise'] == $key) {
                if (!empty($mappingKey['statement']['key'])) {
                    $stateMentValue = $data[$mappingKey['statement']['key']];
                    if (in_array($stateMentValue, $mappingKey['statement'][$mappingKey['statement']['key']]['shouldContain'])) {
                        $columns = $mappingKey['statement'][$mappingKey['statement']['key']]['select'];
                    } else {
                        $columns = $mappingKey['select'];
                    }
                } else {
                    $columns = $mappingKey['select'];
                }
                $columns[] = DB::raw("REPLACE(`qty`,',','') as qty");
                $getData = $getData->select($columns);
                $isMatched = true;
            }
        }

        if (!$isMatched && $type == 'sailed')
            $getData->select('id', 'f_year', 'f_month', 'qtrYear', 'formated_date', 'qty', DB::raw("REPLACE(`qty`,',','') as qty"));

        return $getData;
    }

    public function prepareSaildData($data, $getData)
    {
        $typeWises = ["sub-commodity", "vessel-size", "shipper", "country", "port"];
        $typeWise = $data['type_wise'];

        if (in_array($typeWise, $typeWises)) {
            $getData = $this->combineAllTypeWiseData($getData, $typeWise);            
            $getData = $this->getDataByDateFormat($data, $getData, $typeWises, $typeWise);
            $response = $this->getTypeWiseSortedData($getData, $typeWise,$data);
        } else {
            $getData = collect($getData->get());
            $response = $this->getDataByDateFormat($data, $getData, $typeWises, $typeWise);
        }

        return $response;
    }
}
