<?php

namespace App\Http\Controllers;

use App\Path;
use App\Car;
// use App\Web_add_service;
use GuzzleHttp\Client;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;

class DockerController extends Controller
{
    //

    private $docker_ip="http://192.168.5.222:2375";

    public function init()
    {
        $query = Path::all();
        $main=Car::all();
        $client = new \GuzzleHttp\Client();
        $res = $client->get($this->docker_ip."/containers/json?all=1");
        $response = json_decode($res->getBody()->__toString(), true);

        $container_count=count($response);
        $result_content=array();
        
        for($i=0;$i<$container_count;$i++){
            array_push($result_content,['Names'=>$response[$i]['Names'],'Id'=>substr($response[$i]['Id'],0,12),'Ports'=>$response[$i]['Ports'],
            'State'=>$response[$i]['State'],'Status'=>$response[$i]['Status'],
            'Created'=>$response[$i]['Created']]);
        }
        // dd($result_content);
        return View('docker.service',['result'=>$query,'iresult'=>$main,'contents'=>$result_content]);
    }
    public function admin_service(Request $request){
        $query = Path::all();
        $main=Car::all();
        $client = new \GuzzleHttp\Client();
        $res = $client->get($this->docker_ip."/containers/json?all=1");
        $response = json_decode($res->getBody()->__toString(), true);
        return View('docker.admin_service',['result'=>$query,'iresult'=>$main,'contents'=>$response]);

    }
    public function ctrl_docker_service(Request $request){
        $status=$request->input('btn');
        $container_id=$request->input('id');
        
        $client = new \GuzzleHttp\Client();
        $res = $client->POST($this->docker_ip."/containers/".$container_id."/".$status);
        return redirect()->back();

    }
    public function container_create(Request $request){
        $module_name=$request->input('module_name');
        $origin_stream=$request->input('origin_stream');
        $dis_stream=$request->input('dis_stream');
        $identify_module=$request->input('identify_module');
        $client = new \GuzzleHttp\Client();

        $res = $client->POST($this->docker_ip."/containers/create?name=$module_name",[
            "headers" => ["Content-Type" => "application/json"],
            "json" => [
                "Image" => $identify_module,
                "Env"   =>  array(
                    'SOURCE='.$origin_stream,
                    'DESTINATION='.$dis_stream                   
                ),
                'Labels'=>array(
                    'level'=>'admin',
                    'des_stream'=>$dis_stream
                )                                        
            ]
        ]);

        if($request->input('radio_start')=="Y"){
            $response = json_decode($res->getBody()->__toString(), true);
            $container_id=$response["Id"];
            $res = $client->POST($this->docker_ip."/containers/".$container_id."/start");
        }

        // $response = json_decode($res->getBody()->__toString(), true);
        // dd($response);
        // $data=array(
        //     'service_name'=>$request->input('module_name'),
        //     'container_id'=>$response['Id']
        // );
       
        // Web_add_service::insert($data);
        return redirect()->route('service');

    }
}