<?php

namespace Korkz\Demirteknoloji\Models\Admin;

use Exception;
use Korkz\Demirteknoloji\Connect\DbOperations;
use Korkz\Demirteknoloji\traits\VerotUpload;

class SettingsModel extends DbOperations
{
    use VerotUpload;

    /**
     * @param $tittle
     * @param $keyword
     * @param $description
     * @param $author
     * @param $email
     * @param $phone
     * @param $footer_text
     * @param $setting_lang
     * @return Exception|bool
     */
    protected function editModel($tittle, $keyword, $description, $author, $email, $phone, $footer_text, $setting_lang): bool|int
    {
        try {
            $settings = $this->getData("settings", ["Lang"]);
            if (!$settings) {
                $setting = $this->insertData("settings", ["Lang" => $setting_lang, "Tittle" => $tittle, "Author" => $author, "Description" => $description, "Keywords" => $keyword, "Phone" => $phone, "Mail" => $email, "footer_text" => $footer_text]);
            } else {
                $found = false;
                $setting = null;
                foreach ($settings as $settingItem) {
                    if (in_array($setting_lang, $settingItem)) {
                        $setting = $this->updateData("settings", ["Tittle" => $tittle, "Author" => $author, "Description" => $description, "Keywords" => $keyword, "Phone" => $phone, "Mail" => $email, "footer_text" => $footer_text, "updated_at" => date("Y-m-d H:i:s")], ["Lang" => $setting_lang]);
                        $found = true;
                        break;
                    }
                }
                if (!$found) {
                    $setting = $this->insertData("settings", ["Lang" => $setting_lang, "Tittle" => $tittle, "Author" => $author, "Description" => $description, "Keywords" => $keyword, "Phone" => $phone, "Mail" => $email, "footer_text" => $footer_text]);
                }
            }
            if ($setting) {
                return true;
            } else {
                return false;
            }
        } catch (Exception $exception) {
            http_response_code(500);
            return $exception;
        }
    }

    protected function settingFileUploadedModel($file, $option, $fileToWhere, $lang, $file_languages)
    {
        $pdo = $this->getPdo();
        $pdo->beginTransaction();
        try {
            $uploaded = $this->fileUpload($file, $option, $lang);
            $setting = null;
            if ($uploaded["boolean"]) {
                $settings = $this->getData("settings", ["Lang"]);
                if (!$settings) {
                    foreach ($file_languages["file_languages"] as $file_language) {
                        $setting = $this->insertData("settings", ["Lang" => $file_language, ucwords($fileToWhere) => $uploaded["path"]]);
                    }
                } else {
                    $found = false;
                    $languages = [];
                    $paths = [];
                    foreach ($file_languages["file_languages"] as $file_language) {

                        $settings = $this->getData("settings", ["Lang", "Logo", "Favicon"], ["Lang" => $file_language]);
                        if ($settings) {
                            $paths[] = $settings[0][ucwords($fileToWhere)];
                            $setting = $this->updateData("settings", [ucwords($fileToWhere) => $uploaded["path"], "updated_at" => date("Y-m-d H:i:s")], ["Lang" => $file_language]);
                        } else {
                            $found = true;
                            $languages[] = $file_language;
                        }
                    }
                    $fileDelete = false;
                    $pathsList = [];
                    foreach ($paths as $p) {
                        if (!empty($p)) {
                            $settings = $this->getData("settings", ["Logo", "Favicon"], [ucwords($fileToWhere) => $p]);
                            if (!$settings) {
                                $pathsList[] = dirname(__DIR__, 3) . "/$p";
                                $fileDelete = true;
                            }
                        }
                    }
                    if ($fileDelete) {
                        foreach ($pathsList as $path) {
                            if (file_exists($path)) {
                                unlink($path);
                            }
                        }
                    }
                    if ($found) {
                        foreach ($languages as $language) {
                            $setting = $this->insertData("settings", ["Lang" => $language, ucwords($fileToWhere) => $uploaded["path"]]);
                        }
                    }
                }
                if ($setting) {
                    $pdo->commit();
                    return true;
                } else {
                    $pdo->rollBack();
                    return false;
                }
            } else {
                $pdo->rollBack();
                return false;
            }
        } catch (Exception $exception) {
            $pdo->rollBack();
            http_response_code(500);
            return $exception;
        }
    }


}