<?php

namespace App\Http\Controllers;

use App\Models\Premium;
use App\Models\ZEmail;
use Illuminate\Http\Request;
use Session;

class AppController extends Controller
{
    public function mailbox($email = null) {
        if ($email) {
            $validatedData = validator(['email' => $email], [
                'email' => 'required|email',
            ])->validate();

            if (json_decode(config('app.settings.configuration_settings'))->enable_create_from_url) {
                ZEmail::createCustomEmailFull($email);
            }
            return redirect()->route('mailbox');
        }
        if (!ZEmail::getEmail()) {
            return redirect()->route('home');
        }
        if (json_decode(config('app.settings.configuration_settings'))->disable_mailbox_slug) {
            return redirect()->route('home');
        }
        return $this->app();
    }

    public function app() {
        return redirect()->route('home');
    }
    public function switch($email) {
        ZEmail::setEmail($email);
        if (json_decode(config('app.settings.configuration_settings'))->disable_mailbox_slug) {
            return redirect()->route('home');
        }
        return redirect()->route('mailbox');
    }

    public function delete($email = null) {
        if ($email) {
            $emails = ZEmail::getEmails();
            ZEmail::removeEmail($email);
            return redirect()->route('mailbox');
        } else {
            return redirect()->route('home');
        }
    }

    public function switchP($email) {
        if (Session::get('isInboxTypePremium')) {
            Premium::setEmailP($email);
        } else {
            ZEmail::setEmail($email);
        }
        if (json_decode(config('app.settings.configuration_settings'))->disable_mailbox_slug) {
            return redirect()->route('dashboard');
        }
        return redirect()->route('dashboard.premium');
    }

    public function deleteP($email = null) {
        if ($email) {
            if (Session::get('isInboxTypePremium')) {
                $emails = Premium::getEmails();
                Premium::removeEmail($email);
            } else {
                $emails = ZEmail::getEmails();
                ZEmail::removeEmail($email);
            }
            return redirect()->route('dashboard.premium');
        } else {
            return redirect()->route('dashboard');
        }
    }

    public function locale($locale) {
        if (in_array($locale, config('app.locales'))) {
            session(['locale' => $locale]);
            return redirect()->back();
        }
        abort(400);
    }


    private function getStringBetween($string, $start, $end) {
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return '';
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
    private function setHeaders($page) {
        $header = $page->header;
        foreach ($page->meta ? unserialize($page->meta) : [] as $meta) {
            if ($meta['name'] == 'canonical') {
                $header .= '<link rel="canonical" href="' . $meta['content'] . '" />';
            } else if (str_contains($meta['name'], 'og:')) {
                $header .= '<meta property="' . $meta['name'] . '" content="' . $meta['content'] . '" />';
            } else {
                $header .= '<meta name="' . $meta['name'] . '" content="' . $meta['content'] . '" />';
            }
        }
        $page->header = $header;
        return $page;
    }


}
