<?php

namespace App\Http\Controllers\Webprofile\Front;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Design;
use App\Models\Webprofile\Menu;
use App\Models\Webprofile\Setting;
use GuzzleHttp\Client;
use Session;

class PersonController extends Controller
{
    public function index($name)
    {
        $setting = webprofilesetting();

        if (!Session::has('selected_language')) {
            session(['selected_language' => 'id']);
        }

        if (Session::get('selected_language') == 'id') {
            $data = $this->getDataId($setting, $name);

            return view('webprofile.front.'.$setting['theme'].'.team', $data);
        } elseif (Session::get('selected_language') == 'en') {
            $data = $this->getDataEn($setting, $name);

            return view('webprofile.front.'.$setting['theme'].'.en.team', $data);
        } elseif (Session::get('selected_language') == 'de') {
            $data = $this->getDataDe($setting, $name);

            return view('webprofile.front.'.$setting['theme'].'.de.team', $data);
        } elseif (Session::get('selected_language') == 'ar') {
            $data = $this->getDataSa($setting, $name);

            return view('webprofile.front.'.$setting['theme'].'.ar.team', $data);
        } elseif (Session::get('selected_language') == 'zh') {
            $data = $this->getDataZh($setting, $name);

            return view('webprofile.front.'.$setting['theme'].'.zh.team', $data);
        }
    }

    public function getDataId($setting, $name)
    {
        $setting = webprofilesetting();
        $data = $this->getData($name);
        $menu = Menu::orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'footer' => $footer,
            'name' => $name,
        ];

        return $data;
    }

    public function getDataEn($setting, $name)
    {
        $setting = webprofilesetting();
        $data = $this->getData($name);
        $menu = Menu::with(['rEn'])->orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'footer' => $footer,
            'name' => $name,
        ];

        return $data;
    }

    public function getDataDe($setting, $name)
    {
        $setting = webprofilesetting();
        $data = $this->getData($name);
        $menu = Menu::with(['rDe'])->orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'footer' => $footer,
            'name' => $name,
        ];

        return $data;
    }

    public function getDataSa($setting, $name)
    {
        $setting = webprofilesetting();
        $data = $this->getData($name);
        $menu = Menu::with(['rSa'])->orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'footer' => $footer,
            'name' => $name,
        ];

        return $data;
    }

    public function getDataZh($setting, $name)
    {
        $setting = webprofilesetting();
        $data = $this->getData($name);
        $menu = Menu::with(['rZh'])->orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'footer' => $footer,
            'name' => $name,
        ];

        return $data;
    }

    public function getData($name)
    {
        $pick = Setting::where('name_setting', 'team')->first();

        $listTeam = [];

        if ($pick) {
            $listTeam = $this->listTeam($pick->value_setting);
        }
        $team = [];
        if ($listTeam) {
            if ($name == 'lecturer') {
                if (array_key_exists(1, $listTeam)) {
                    $team = $listTeam[1];
                } else {
                    $team = [];
                }
            } elseif ($name == 'academic-staff') {
                if (array_key_exists(0, $listTeam)) {
                    $team = $listTeam[0];
                } else {
                    $team = [];
                }
            }
        }

        return $team;
    }

    private function listTeam($unit)
    {
        try {
            $client = new Client();
            $URI = 'http://i-sdm.unesa.ac.id/api/pegawai-per-prodi/asdfLKJH12309==/'.$unit;
            $response = $client->get($URI);
            $statuscode = $response->getStatusCode();

            if (200 === $statuscode) {
                // Do something
                $responseJson = json_decode($response->getBody(), true);
            } elseif (304 === $statuscode) {
                // Nothing to do
            } elseif (404 === $statuscode) {
                // Clean up DB or something like this
                $responseJson = [];
            } else {
            }
        } catch (\Throwable $th) {
            //throw $th;
            $responseJson = [];
        }

        return $responseJson;
    }
}
