<?php

use Illuminate\Support\Facades\Request;
use App\Model\Settings;
// use \App\Model\Pages;
use App\Model\Faqs;
use App\Model\Feedbacks;
use App\Model\Orders;
use App\Model\Projects;
use App\Model\Chats;
use Carbon\Carbon;

function dateFormat($d)
{
    return date('d M, Y', strtotime($d));
}

function dateTime($d)
{
    return date('d M, Y h:i A', strtotime($d));
}

function getVersion()
{
    return "?v=1.2178";
}

function calculateDays($d){
    $now = time();
    $d = strtotime($d);
    $datediff = $d - $now;
    $calculated_days =  round($datediff / (60 * 60 * 24));
    return $calculated_days;
}

function calculateTime($d){
    $newd = explode(" ",$d);
    return $newd[1];
}

function currentTime() {
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date( 'h:i:s A', time () );
    $currentTime = date("H:i:s", strtotime($currentTime));
    return $currentTime;
}

function dateFormate($date) {
    $new_date = date('j M Y', strtotime($date));
    return $new_date;
}
function pastDateTime($d){
    $now = time();
    $date = $d;
    $d = strtotime($d);
    $datediff = $d - $now;
    $days = round($datediff / (60 * 60 * 24)) ;
    $dTime =  ($days * -1 .' days ago');
    if ($days == 0) {
        $to = Carbon::createFromFormat('Y-m-d H:s:i', $date);
        $from = Carbon::createFromFormat('Y-m-d H:s:i', date('Y-m-d H:s:i'));
        $timediff = $to->diffInHours($from);
        $dTime = ($timediff == 0) ? 'Just Now' : $timediff . " hour ago";
    }
    return $dTime;
}

// function getPageMeta(){
//     $permalinks = Request::segments();
//     $permalinks = end($permalinks);
//     // dd($permalinks);
//     return Pages::where(['permalinks' => $permalinks,'status'=>1])->get()->first();
//     // dd($pageData);
// }

function getSettings()
{
    if (session('Settings') == null) {
        Session::put('Settings', Settings::get()->first()->toArray());
    }
    return  session('Settings');
}

// function getTopSearches(){
//     if(session('topSearches') == null){
//         Session::put('topSearches', Pages::select(['pagename','permalinks'])->where(['status'=>1,'top_search'=>1])->take(7)->get()->toArray());
//     }
//     return  session('topSearches');   
// }

function getFaqs($permalinks)
{
    $faqs = Faqs::where(['permalinks' => $permalinks, 'status' => 0])->take(10)->get();
    return  view('helpers.faq', compact('faqs'));
}

function getInitials($name)
{
    return  implode('', array_map(function ($part) {
        return strtoupper(@$part['0']);
    }, explode(' ', $name)));
}
function getNotification()
{
        // $response['feedback'] = DB::table('notify')->where(['freelancer_id' => Auth::user()->id, 'projectStatus' => 'Feedback', 'status' => 1])->get();
    $response['feedback'] = DB::table('notify_new')->where(['freelancer_id' => Auth::user()->id, ])->get();
    $response['projects'] = Projects::select('*')
    // ->whereHas('project', function ($q) {
        //     $q->where(['status' => 'Allotted']);})
        ->where(['status' => 'Allotted', 'view' => 0,'freelancer_id'=>Auth::user()->id])->whereNotNull('freelancer_id')->get();
    $response['chats'] = Chats::select('p.*','chats.*','chats.created as chat_created' )->join('projects as p', 'p.id', '=', 'chats.project_id')->where(['chats.view' => 0, 'to_id' => 0,'p.freelancer_id'=>Auth::user()->id,'type'=>'chat'])->get();
    
    $response['feedbackCount']=Count($response['feedback'] );
    $response['ordersCount']=Count($response['projects'] );
    $response['chatsCount']=Count($response['chats'] );
    $response['p_NofityCount'] = $response['ordersCount'] + $response['feedbackCount'];
    $response['total']= ( $response['p_NofityCount']+$response['chatsCount']);
    return ($response);
}

function select_Timezone() {
    $time_zone_list = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
    return  $time_zone_list;
}

function getLocationInfoByIp()
{
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} else {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	// $ip = '121.222.54.141';
	if ($ip == '::1' || $ip == '192.168.1.30') {
		$ip = '121.222.54.141';
        // $ip = '157.47.137.174';
	}

			$ip_data = @json_decode(file_get_contents('http://ip-api.com/json/' . $ip));

			$return_data = $ip_data->timezone;
	
	return $return_data;
}