<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Validator;
use App\Traits\Apitraits;
use App\Locater;
use App\Page;
use App\City;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
//use App\Exports\UsersExport;
use App\Imports\FeedsImport;
use Maatwebsite\Excel\Facades\Excel;



class CronController extends Controller
{
    use Apitraits;

    public function getLocaterListData(Request $request){
        $api_url = config('app.api_list');
        
        $getLocaterListMetaData = $this->cronList($api_url);
        
        for($i=$getLocaterListMetaData['current_page']; $i<=$getLocaterListMetaData['last_page']; $i++){
            $getLocaterListDataNew = $this->call_api_page($api_url, '', '', '', '', '', $i);
            
            $finalData = array_map(function ($data) use ($getLocaterListDataNew) {

                $checkLocaterData = Locater::find($data['id']);
                
                if(empty($checkLocaterData)){
                    
                    $locater = new Locater;
                    $locater->locater_identity = $data['id'];
                    $locater->locater_code = $data['code'];
                    $locater->name = $data['name'];
                    $locater->created_by = 1;
                    $locater->save();
                    
                    $page = new Page;
                    $page->locater_id = $locater->id;
                    $page->title = $data['name'];
                    $page->slug = str_slug($data['name']);
                    $page->created_by = 1;
                    $page->save();
                
                }
                
            }, $getLocaterListDataNew['data']);
        }
        return response()->json([
            'status' => 'success',
            'data' => 'Updated Sucessfully'
        ]);
    }


    public function getState(){
        $file = Storage::disk('public')->exists('city.json');
        $body = Storage::get('city.json');
        $cityData = json_decode($body, true);
        $arrayDetail = array();
        foreach($cityData as $data){
            
            $arrayDetail[]= $data['state'];
            
        }
        $stateName = array_unique($arrayDetail);
        foreach($stateName as $data){
            $state = new City;
            $state->name = $data;
            $state->parent_id = 1;
            $state->type = 2;
            $state->save();
        }
        return response()->json([
            'status' => 'success',
            'data' => 'Updated Sucessfully'
        ]);
    }

    public function getCity(){
        $file = Storage::disk('public')->exists('city.json');
        $body = Storage::get('city.json');
        $cityData = json_decode($body, true);
        
        $getStateData = City::where('cities.name', '!=', '')->where('cities.type', '=', '2')->pluck('name','id')->toarray();
        
        foreach($getStateData as $key=>$state){
            
            foreach($cityData as $city){
                if($state == $city['state']){
                
                    $cityaa = new City;
                    $cityaa->name = $city['name'];
                    $cityaa->parent_id = $key;
                    $cityaa->type = 3;
                    $cityaa->save();
                }
            }
        }
        
        return response()->json([
            'status' => 'success',
            'data' => 'Updated Sucessfully'
        ]);
    }

    public function import(Request $request) 
    {
        $url = "https://m2admin.anitadongre.com/pub/custom_script/feeds/envigo/sheet/and-google-in.csv";
        $contents = file_get_contents($url);
        $name = substr($url, strrpos($url, '/') + 1);
        $data = Storage::put($name, $contents);

        if($data == true){

            $file = Storage::path('and-google-in.csv');
            $data = Excel::import(new FeedsImport,$file);
            $msg = 'Feeds has been updated successfully.';
            $request->session()->flash('message', $msg);
            return redirect()->route('admin.feedSetting');

        }else{
            
            $msg = 'Something went wrong!';
            $request->session()->flash('error', $msg);
            return redirect()->route('admin.feedSetting');
        }
    }

}