<?php

namespace App\Traits;

use Carbon\Carbon;

trait VesselLineUpGraphTrait
{
    protected function getDateFromFormat($dateFormat, $fromDate, $toDate)
    {
        if ($dateFormat == 'quaterly') {
            $fromDateArray = explode(",", trim($fromDate));
            $toDateArray = explode(",", trim($toDate));
            return $this->getQuaterStartAndDate($fromDateArray, $toDateArray);
        } elseif ($dateFormat == 'monthly') {
            return $this->getConvertedDate('M Y', $fromDate, $toDate);
        } elseif ($dateFormat == 'yearly') {
            return $this->getConvertedDate('Y', $fromDate, $toDate);
        } else if ($dateFormat == 'date') {
            return $this->getConvertedDate('Y-m-d', $fromDate, $toDate);
        }
    }
    
    protected function getDateFromFormatWithOrder($dateFormat, $fDate)
    {
        if ($dateFormat == 'quaterly') {
            $fromDateArray = explode(" ", trim($fDate));
            return $this->getQuaterStartAndDateWithOrder($fromDateArray);
        } elseif ($dateFormat == 'monthly') {
            return $this->getConvertedDateWithOrder('M-Y', $fDate);
        } elseif ($dateFormat == 'yearly') {
            return $this->getConvertedDateWithOrder('Y', $fDate);
        } else if ($dateFormat == 'date') {
            return $this->getConvertedDateWithOrder('Y-m-d', $fDate);
        }
    }

    protected function getQuaterStartAndDate($fromDateArray, $toDateArray)
    {
        if ($fromDateArray[0] == 'Q1') {
            $data['fromDate'] = "{$fromDateArray[1]}-01-01";
        } elseif ($fromDateArray[0] == 'Q2') {
            $data['fromDate'] = "{$fromDateArray[1]}-04-01";
        } elseif ($fromDateArray[0] == 'Q3') {
            $data['fromDate'] = "{$fromDateArray[1]}-07-01";
        } elseif ($fromDateArray[0] == 'Q4') {
            $data['fromDate'] = "{$fromDateArray[1]}-10-01";
        }

        if ($toDateArray[0] == 'Q1') {
            $data['toDate'] = "{$toDateArray[1]}-03-31";
        } elseif ($toDateArray[0] == 'Q2') {
            $data['toDate'] = "{$toDateArray[1]}-06-30";
        } elseif ($toDateArray[0] == 'Q3') {
            $data['toDate'] = "{$toDateArray[1]}-09-30";
        } elseif ($toDateArray[0] == 'Q4') {
            $data['toDate'] = "{$toDateArray[1]}-12-31";
        }
        return $data;
    }
    
    protected function getQuaterStartAndDateWithOrder($fromDateArray)
    {
        if ($fromDateArray[0] == 'Q1') {
            $data['fromDate'] = "{$fromDateArray[1]}-01-01";
        } elseif ($fromDateArray[0] == 'Q2') {
            $data['fromDate'] = "{$fromDateArray[1]}-04-01";
        } elseif ($fromDateArray[0] == 'Q3') {
            $data['fromDate'] = "{$fromDateArray[1]}-07-01";
        } elseif ($fromDateArray[0] == 'Q4') {
            $data['fromDate'] = "{$fromDateArray[1]}-10-01";
        }
        return $data;
    }

    protected function getConvertedDate($format, $fromDate, $toDate)
    {
        return [
            'fromDate' => Carbon::createFromFormat($format, $fromDate)->startOfMonth()->toDateString(),
            'toDate' => Carbon::createFromFormat($format, $toDate)->endOfMonth()->toDateString()
        ];
    }
    protected function getConvertedDateWithOrder($format, $fromDate)
    {
        return [
            'fromDate' => Carbon::createFromFormat($format, $fromDate)->toDateString()
        ];
    }
    protected function combineAllTypeWiseData($getData, $typeWise)
    {
        $getData = collect($getData->get());
        $groupBy = config('data-mapping.vessel_line_up.common_groupby.' . $typeWise);
        $groupData = $getData->groupBy($groupBy);
        $getData = $groupData->take(10);
        return $getData->put('others', $groupData->slice(10)->collapse()->map(function ($val) use ($groupBy) {
            $val[$groupBy] = 'Others';
            return $val;
        }))->collapse();
    }

    protected function getDataByDateFormat($data, $getData, $typeWises, $typeWise)
    {
        switch ($data['date_formate']) {
            case 'quaterly':
                $response = in_array($typeWise, $typeWises) ? $this->getDataByGroupByDateFormat($getData, 'qtrYear') : $this->saildDataForNonTypeWise($getData, 'qtrYear');
                break;
            case 'monthly':
                $response = in_array($typeWise, $typeWises) ? $this->getDataByGroupByDateFormat($getData, 'formated_date', true, 'M-Y') : $this->saildDataForNonTypeWise($getData, 'formated_date', true, 'M-Y');
                break;
            case 'yearly':
                $response = in_array($typeWise, $typeWises) ? $this->getDataByGroupByDateFormat($getData, 'f_year') : $this->saildDataForNonTypeWise($getData, 'f_year');
                break;
            default:
                $response = in_array($typeWise, $typeWises) ? $this->getDataByGroupByDateFormat($getData, 'formated_date') : $this->saildDataForNonTypeWise($getData, 'formated_date');
                break;
        }
        return $response;
    }

    protected function getDataByGroupByDateFormat($getData, $groupBy, $isCallable = false, $format = '')
    {
        if (!$isCallable) {
            $getData = $getData->groupBy($groupBy);
        } else {
            $getData = $getData->groupBy(function ($val) use ($groupBy, $format) {
                return Carbon::parse($val[$groupBy])->format($format);
            });
        }
        return $getData;
    }

    protected function saildDataForNonTypeWise($getData, $groupBy, $isCallable = false, $format = '')
    {
        $response = [];
        $getData = $this->getDataByGroupByDateFormat($getData, $groupBy, $isCallable, $format);
        foreach ($getData as $key => $singleKey) {
            $response[$key] = $singleKey->sum('qty');
        }
        return $response;
    }

    protected function getTypeWiseSortedData($getData, $typeWise,$data=array())
    {
        $response = [];
        $type_arr = array('quaterly','monthly','yearly','date');
        foreach ($getData as $key => $singleData) {
            $groupBy = config('data-mapping.vessel_line_up.common_groupby.' . $typeWise);
            $groupData = $singleData->groupBy($groupBy);
            $f_date = $key;
            if(in_array($data['date_formate'],$type_arr)){
                $f_date_arr =  $this->getDateFromFormatWithOrder($data['date_formate'], $key);
                $f_date = $f_date_arr['fromDate'];
            }
            $singleResponseData = [
                'name' => $key,
                'f_date'=>$f_date,
                'data' => []
            ];
            foreach ($groupData as $objKey => $singleObj) {
                $singleResponseData['data'][] = [
                    'name' => $objKey,
                    'value' => $singleObj->sum('qty')
                ];
            }
            $response[] = $singleResponseData;
        }         
        /*if($data['show_old']=='Y'){           
            return $response;
        }*/
        if(in_array($data['date_formate'],$type_arr)){
            usort($response, function($a, $b) {
                return strtotime($a['f_date']) - strtotime($b['f_date']);
            });
        }

        return $response;
    }
}
