<?php

namespace App\Modules\System\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\System\Models\Menu;
class ConfigController extends Controller
{
    function __construct(){
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('System.views.config');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if ($request->has('action')) {
            switch ($request->input('action')) {
                case 'reloadapp':
                    $file = fopen(base_path()."\install.txt", "a");

                    fwrite($file, "Installation sist contable version 1.0" . PHP_EOL);

                    fwrite($file, "Laravel log  results" . PHP_EOL);

                    fclose($file);

                    $path = base_path('.env'); 
                    $key = array('DB_DATABASE','DB_USERNAME','DB_PASSWORD');

                    for ($i=0; $i <count($key) ; $i++) { 

                        if(is_bool(env($key[$i]))) { 

                            $old = env($key[$i])? 'true' : 'false'; 

                        } elseif(env($key[$i])===null){ 

                            $old = 'null'; 

                        } else{ 

                            $old = env($key[$i]); 
                        } 
                        if (file_exists($path)) { 
                            file_put_contents($path, str_replace( "$key[$i]=".$old, "$key[$i]=".'NEW', file_get_contents($path) )); 
                        }
                    }
                    return response()->json(['success'=>'Datos restablecidos cargando instalador......','url'=>url("home/")]);
                break;
                case 'social':
                    $path = base_path('.env'); 
                    if ($request->input('tipe') == 'facebook') {
                        $key = array('FACEBOOK_ID','FACEBOOK_SECRET','FACEBOOK_REDIRECT');
                        $message = '<i class="icon-checkmark-circle2 "> </i> Facebook api actualizada con exito ';
                    }elseif ($request->input('tipe') == 'google') {
                        $key = array('GOOGLE_CLIENT_ID','GOOGLE_CLIENT_SECRET','GOOGLE_REDIRECT');
                        $message = '<i class="icon-checkmark-circle2 "> </i> Google api actualizada con exito ';
                    }

                    for ($i=0; $i <count($key) ; $i++) { 

                        if(is_bool(env($key[$i]))) { 

                            $old = env($key[$i])? 'true' : 'false'; 

                        } elseif(env($key[$i])===null){ 

                            $old = 'null'; 

                        } else{ 

                            $old = env($key[$i]); 
                        } 
                        if (file_exists($path)) { 
                            file_put_contents($path, str_replace( "$key[$i]=".$old, "$key[$i]=".$request->input('apisocial')[$i], file_get_contents($path) )); 
                        }
                    }
                    return response()->json(['success'=>$message,'response'=>'apis']);
                break;
                case 'mail':
                    $key = array('MAIL_DRIVER','MAIL_HOST','MAIL_PORT','MAIL_USERNAME','MAIL_PASSWORD','MAIL_ENCRYPTION');

                    $path = base_path('.env'); 

                    for ($i=0; $i <count($key) ; $i++) { 

                        if(is_bool(env($key[$i]))) { 

                            $old = env($key[$i])? 'true' : 'false'; 

                        } elseif(env($key[$i])===null){ 

                            $old = 'null'; 

                        } else{ 

                            $old = env($key[$i]); 
                        } 
                        if (file_exists($path)) { 
                            file_put_contents($path, str_replace( "$key[$i]=".$old, "$key[$i]=".$request->input('mail')[$i], file_get_contents($path) )); 
                        }
                    }
                    return response()->json(['success'=>'Credenciales de email actualizadas','response'=>'mail']);
                break;
                case 'automattic':
                    $key = array('automattic_ck','automattic_cs','automattic_url');

                    $path = base_path('.env'); 

                    for ($i=0; $i <count($key) ; $i++) { 

                        if(is_bool(env($key[$i]))) { 

                            $old = env($key[$i])? 'true' : 'false'; 

                        } elseif(env($key[$i])===null){ 

                            $old = 'null'; 

                        } else{ 

                            $old = env($key[$i]); 
                        } 
                        if (file_exists($path)) { 
                            file_put_contents($path, str_replace( "$key[$i]=".$old, "$key[$i]=".$request->input('automattic')[$i], file_get_contents($path) )); 
                        }
                    }
                    return response()->json(['success'=>'Credenciales del automattic actualizadas','response'=>'automattic']);
                break;
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $menu = Menus::find($id);
        if (!empty($menu)) {
            if (Menus::where('slug', '=', $request->input("url"))->where('id','!=',$id)->count() > 0) {
                return response()->json(['error'=>'Esta url ya existe']);
            }else{
                $menu->slug = $request->input('url');
                $menu->icon_text = $request->input('ico');
                $menu->save();
                return response()->json(['success'=>'Item actualizado con exito']);
            }
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
