<?php

namespace App\Http\Controllers\Webprofile\Front;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Design;
use App\Models\Webprofile\Gallery;
use App\Models\Webprofile\Information;
use App\Models\Webprofile\Agenda;
use App\Models\Webprofile\Menu;
use App\Models\Webprofile\Posts;
use App\Models\Webprofile\Slider;
use Session;

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

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

        if (Session::get('selected_language') == 'id') {
            $data = $this->getDataId($setting);
            if(isset($setting['ppid']) )
            return view('webprofile.front.'.$setting['theme'].'.indexcustom', $data);
            else
            return view('webprofile.front.'.$setting['theme'].'.index', $data);
        } elseif (Session::get('selected_language') == 'en') {
            $data = $this->getDataEn($setting);
            if(isset($setting['ppid']) )
            return view('webprofile.front.'.$setting['theme'].'.en.indexcustom', $data);
            else
            return view('webprofile.front.'.$setting['theme'].'.en.index', $data);
        } elseif (Session::get('selected_language') == 'de') {
            $data = $this->getDataDe($setting);
            if(isset($setting['ppid']) )
            return view('webprofile.front.'.$setting['theme'].'.de.indexcustom', $data);
            else
            return view('webprofile.front.'.$setting['theme'].'.de.index', $data);
        } elseif (Session::get('selected_language') == 'ar') {
            $data = $this->getDataSa($setting);
            if(isset($setting['ppid']) )
            return view('webprofile.front.'.$setting['theme'].'.ar.indexcustom', $data);
            else
            return view('webprofile.front.'.$setting['theme'].'.ar.index', $data);
        } elseif (Session::get('selected_language') == 'zh') {
            $data = $this->getDataZh($setting);
            if(isset($setting['ppid']) )
            return view('webprofile.front.'.$setting['theme'].'.zh.indexcustom', $data);
            else
            return view('webprofile.front.'.$setting['theme'].'.zh.index', $data);
        }
    }

    private function getDataId($setting)
    {
        $posts = Posts::where('post_status', '1')->orderby('post_date', 'desc')->paginate($setting['post_per_page']);
        $posthome = Posts::where('post_status', '1')->orderby('post_date', 'desc')->paginate(isset($setting['post_home'])?$setting['post_home']:'6');
        $resend = Posts::where('post_status', '1')->orderby('post_date', 'desc')->limit(isset($setting['limit_terkini'])?$setting['limit_terkini']:'5')->get();
        $hot = Posts::where('post_status', '1')->orderby('viewer', 'desc')->limit(isset($setting['limit_populer'])?$setting['limit_populer']:'5')->get();
        $info = Information::where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $agenda = Agenda::where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $slider = Slider::where('is_active', '1')->orderby('created_at', 'desc')->get();
        $menu = Menu::orderby('urutan', 'asc')->get();
        $gallery = Gallery::where('is_active', '1')->orderBy('created_at', 'asc')->get();
        $quote = Design::where('name_design', 'quote')->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();
        $body = Design::where('name_design', 'body')->where('title_show' , 't')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'posts' => $posts,
            'posthome' => $posthome,
            'resend' => $resend,
            'hot' => $hot,
            'info' => $info,
            'agenda' => $agenda,
            'slider' => $slider,
            'menu' => $menu,
            'gallery' => $gallery,
            'quote' => $quote,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'body' => $body,
            'setting' => $setting,
            'footer' => $footer,
        ];

        return $data;
    }

    private function getDataEn($setting)
    {
        $posts = Posts::with(['rEn'])->where('post_status', '1')->orderby('post_date', 'desc')->paginate($setting['post_per_page']);
        $posthome = Posts::where('post_status', '1')->orderby('post_date', 'desc')->paginate(isset($setting['post_home'])?$setting['post_home']:'6');
        $resend = Posts::with(['rEn'])->where('post_status', '1')->orderby('post_date', 'desc')->limit(isset($setting['limit_terkini'])?$setting['limit_terkini']:'5')->get();
        $hot = Posts::with(['rEn'])->where('post_status', '1')->orderby('viewer', 'desc')->limit(isset($setting['limit_populer'])?$setting['limit_populer']:'5')->get();
        $info = Information::with(['rEn'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $agenda = Agenda::with(['rEn'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $slider = Slider::where('is_active', '1')->orderby('created_at', 'desc')->get();
        $menu = Menu::with(['rEn'])->orderby('urutan', 'asc')->get();
        $gallery = Gallery::where('is_active', '1')->orderBy('created_at', 'asc')->get();
        $quote = Design::with(['rEn'])->where('name_design', 'quote')->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();
        $body = Design::with(['rEn'])->where('name_design', 'body')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'posts' => $posts,
            'posthome' => $posthome,
            'resend' => $resend,
            'hot' => $hot,
            'info' => $info,
            'agenda' => $agenda,
            'slider' => $slider,
            'menu' => $menu,
            'gallery' => $gallery,
            'quote' => $quote,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'body' => $body,
            'setting' => $setting,
            'footer' => $footer,
        ];

        return $data;
    }

    private function getDataDe($setting)
    {
        $posts = Posts::with(['rDe'])->where('post_status', '1')->orderby('post_date', 'desc')->paginate($setting['post_per_page']);
        $posthome = Posts::where('post_status', '1')->orderby('post_date', 'desc')->paginate(isset($setting['post_home'])?$setting['post_home']:'6');
        $resend = Posts::with(['rDe'])->where('post_status', '1')->orderby('post_date', 'desc')->limit(isset($setting['limit_terkini'])?$setting['limit_terkini']:'5')->get();
        $hot = Posts::with(['rDe'])->where('post_status', '1')->orderby('viewer', 'desc')->limit(isset($setting['limit_populer'])?$setting['limit_populer']:'5')->get();
        $info = Information::with(['rDe'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $agenda = Agenda::with(['rDe'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $slider = Slider::where('is_active', '1')->orderby('created_at', 'desc')->get();
        $menu = Menu::with(['rDe'])->orderby('urutan', 'asc')->get();
        $gallery = Gallery::where('is_active', '1')->orderBy('created_at', 'asc')->get();
        $quote = Design::with(['rDe'])->where('name_design', 'quote')->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();
        $body = Design::with(['rDe'])->where('name_design', 'body')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'posts' => $posts,
            'posthome' => $posthome,
            'resend' => $resend,
            'hot' => $hot,
            'info' => $info,
            'agenda' => $agenda,
            'slider' => $slider,
            'menu' => $menu,
            'gallery' => $gallery,
            'quote' => $quote,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'body' => $body,
            'setting' => $setting,
            'footer' => $footer,
        ];

        return $data;
    }

    private function getDataSa($setting)
    {
        $posts = Posts::with(['rSa'])->where('post_status', '1')->orderby('post_date', 'desc')->paginate($setting['post_per_page']);
        $posthome = Posts::where('post_status', '1')->orderby('post_date', 'desc')->paginate(isset($setting['post_home'])?$setting['post_home']:'6');
        $resend = Posts::with(['rSa'])->where('post_status', '1')->orderby('post_date', 'desc')->limit(isset($setting['limit_terkini'])?$setting['limit_terkini']:'5')->get();
        $hot = Posts::with(['rSa'])->where('post_status', '1')->orderby('viewer', 'desc')->limit(isset($setting['limit_populer'])?$setting['limit_populer']:'5')->get();
        $info = Information::with(['rSa'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $agenda = Agenda::with(['rSa'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $slider = Slider::where('is_active', '1')->orderby('created_at', 'desc')->get();
        $menu = Menu::with(['rSa'])->orderby('urutan', 'asc')->get();
        $gallery = Gallery::where('is_active', '1')->orderBy('created_at', 'asc')->get();
        $quote = Design::with(['rSa'])->where('name_design', 'quote')->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();
        $body = Design::with(['rSa'])->where('name_design', 'body')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'posts' => $posts,
            'posthome' => $posthome,
            'resend' => $resend,
            'hot' => $hot,
            'info' => $info,
            'agenda' => $agenda,
            'slider' => $slider,
            'menu' => $menu,
            'gallery' => $gallery,
            'quote' => $quote,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'body' => $body,
            'setting' => $setting,
            'footer' => $footer,
        ];

        return $data;
    }

    private function getDataZh($setting)
    {
        $posts = Posts::with(['rZh'])->where('post_status', '1')->orderby('post_date', 'desc')->paginate($setting['post_per_page']);
        $posthome = Posts::where('post_status', '1')->orderby('post_date', 'desc')->paginate(isset($setting['post_home'])?$setting['post_home']:'6');
        $resend = Posts::with(['rZh'])->where('post_status', '1')->orderby('post_date', 'desc')->limit(isset($setting['limit_terkini'])?$setting['limit_terkini']:'5')->get();
        $hot = Posts::with(['rZh'])->where('post_status', '1')->orderby('viewer', 'desc')->limit(isset($setting['limit_populer'])?$setting['limit_populer']:'5')->get();
        $info = Information::with(['rZh'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $agenda = Agenda::with(['rZh'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $slider = Slider::where('is_active', '1')->orderby('created_at', 'desc')->get();
        $menu = Menu::with(['rZh'])->orderby('urutan', 'asc')->get();
        $gallery = Gallery::where('is_active', '1')->orderBy('created_at', 'asc')->get();
        $quote = Design::with(['rZh'])->where('name_design', 'quote')->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();
        $body = Design::with(['rZh'])->where('name_design', 'body')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'posts' => $posts,
            'posthome' => $posthome,
            'resend' => $resend,
            'hot' => $hot,
            'info' => $info,
            'agenda' => $agenda,
            'slider' => $slider,
            'menu' => $menu,
            'gallery' => $gallery,
            'quote' => $quote,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'body' => $body,
            'setting' => $setting,
            'footer' => $footer,
        ];

        return $data;
    }
}
