<?php

namespace App\Http\Controllers;

use App\Helpers\RemoveKarakter;
use App\Models\TokenWaModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class ApiWaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $wa = TokenWaModel::first();
        $wa_count = TokenWaModel::count();
        
        try {
            $curl = curl_init();
    
            curl_setopt_array($curl, array(
                CURLOPT_URL => 'https://app.ruangwa.id/api/info',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => 'token='. $wa->token .'&username=' . $wa->username,
            ));
    
            $response = curl_exec($curl);
            $api = json_decode($response, TRUE);
        } catch (\Throwable $th) {
            $api = null;
        }


        $pageConfigs = ['pageHeader' => false];
        $data = [
            'title' =>  'Api Wa',
            'first' =>  TokenWaModel::OrderBy('uuid', 'desc')->first(),
            'count' =>  $wa_count,
            'api'   =>  $api,
        ];

        return view('/content/api-wa/index', ['pageConfigs' => $pageConfigs], $data);
        
    }

    /**
     * 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)
    {
        //
    }

    /**
     * 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)
    {
        $request->validate([
            'token' =>  'required|max:300|alpha_num_dot_comma_space',
            'username'  =>  'required|max:255|alpha_num_dot_comma_space'
        ],
    [
        'token.required'    =>  'Token Harus Di Isi',
        'token.max' =>  'Karakter Token Terlalu Panjang',
        'token.alpha_num_dot_comma_space'   =>  "Token Tidak Valid",

        'username.required'  => 'Username Harus Di Isi',
        'username.max'  =>  'Karakter Username Terlalu Panjang',
        'username.alpha_num_dot_comma_space'    =>  'Username Tidak Valid'  
    ]);

        $token = RemoveKarakter::hapus_karakter($request->token);
        $username = RemoveKarakter::hapus_karakter($request->username);

        try {
            $result = TokenWaModel::all()->count();
            if($result) {
                TokenWaModel::OrderBy("uuid", 'desc')->update([
                    'token' =>  $token,
                    'username'  =>  $username
                ]);
            } else {
                $token = new TokenWaModel();
                $token->token = $token;
                $token->username = $username;
                $token->save();
            }

            return redirect()->back()->with("success", "Data Berhasil Diupdate");
        } catch (\Throwable $th) {
            return redirect()->back()->with("error", "Data Gagal Diupdate");
        }
    }

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