<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Stichoza\GoogleTranslate\GoogleTranslate;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class AdminTranslationController extends Controller
{

    public function front_end()
    {
        $translation_data = json_decode(file_get_contents(resource_path('lang/front.json')));
        return view('admin.translation.front-end', compact('translation_data'));
    }

    public function front_end_update(Request $request) 
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }

        $arr_key = [];
        foreach($request->key_arr as $item) {
            $arr_key[] = strip_tags($item);
        }
        $arr_value = [];
        foreach($request->value_arr as $item) {
            $arr_value[] = strip_tags($item);
        }
        for($i=0;$i<count($arr_key);$i++) {
            $data[$arr_key[$i]] = $arr_value[$i];
        }

        $new_json = json_encode($data,JSON_PRETTY_PRINT);
        file_put_contents(resource_path('lang/front.json'), $new_json);

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function front_end_translate(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }

        $json_data = file_get_contents(resource_path('lang/front.json'));
        $json_data = json_decode($json_data, true);

        $tr = new GoogleTranslate($request->code);

        $new_json = [];
        foreach($json_data as $key => $value) {
            $new_json[$key] = $tr->translate($value);
        }

        $new_json = json_encode($new_json,JSON_PRETTY_PRINT);
        file_put_contents(resource_path('lang/front.json'), $new_json);

        return redirect()->back()->with('success', __('Front End Translation is performed successfully'));
    }


    public function back_end()
    {
        $translation_data = json_decode(file_get_contents(resource_path('lang/back.json')));
        return view('admin.translation.back-end', compact('translation_data'));
    }

    public function back_end_update(Request $request) 
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }

        $arr_key = [];
        foreach($request->key_arr as $item) {
            $arr_key[] = strip_tags($item);
        }
        $arr_value = [];
        foreach($request->value_arr as $item) {
            $arr_value[] = strip_tags($item);
        }
        for($i=0;$i<count($arr_key);$i++) {
            $data[$arr_key[$i]] = $arr_value[$i];
        }

        $new_json = json_encode($data,JSON_PRETTY_PRINT);
        file_put_contents(resource_path('lang/back.json'), $new_json);

        return redirect()->back()->with('success', __('Data is updated successfully'));
    }

    public function back_end_translate(Request $request)
    {
        if(env('PROJECT_MODE') == 0) {
            return redirect()->back()->with('info', env('PROJECT_NOTIFICATION'));
        }
        
        $json_data = file_get_contents(resource_path('lang/back.json'));
        $json_data = json_decode($json_data, true);

        $tr = new GoogleTranslate($request->code);

        $new_json = [];
        foreach($json_data as $key => $value) {
            $new_json[$key] = $tr->translate($value);
        }

        $new_json = json_encode($new_json,JSON_PRETTY_PRINT);
        file_put_contents(resource_path('lang/back.json'), $new_json);

        return redirect()->back()->with('success', __('Back End Translation is performed successfully'));
    }
    
}
