<?php

namespace brain\data;

use brain\data\Member;
use brain\utility\DocTools;
use brain\utility\Sorting;

use function _\find;
use function _\pull;
use function _\remove;

class Settings
{
    private $folks;
    private static $tags;
    private static $settings;

    public function __construct()
    {
        //gets all settings files and converts to php objects
        $this->folks = json_decode(file_get_contents("../config/folks.json"), true);
        self::$tags = json_decode(file_get_contents("../config/tags.json"), true);
        self::$settings = json_decode(
            file_get_contents("../config/settings.json"),
            true
        );
    }

    public static function sync($data)
    {
        $settings = self::$settings;
        $settings["global"]["base_url"] = $data["global"]["base_url"];
        $settings["global"]["title"] = $data["global"]["title"];
        $settings["global"]["descriptions"] = $data["global"]["descriptions"];
        $settings["global"]["base_url"] = $data["global"]["base_url"];
        $settings["global"]["private"] = $data["global"]["private"];
        $settings["global"]["renderOnSave"] = $data["global"]["renderOnSave"];
        $settings["global"]["theme"] = $data["global"]["theme"];
        $settings["global"]["externalAPI"] = $data["global"]["externalAPI"];
        $settings["global"]["dynamicRender"] = $data["global"]["dynamicRender"];

        Member::updateData("handle", $data["member"]["handle"]);
        Member::updateData("email", $data["member"]["email"]);

        $settings["email"]["active"] = $data["email"]["active"];
        $settings["email"]["smtp"] = $data["email"]["smtp"];
        $settings["email"]["mailgun"] = $data["email"]["mailgun"];

        DocTools::writeSettings("../config/settings.json", $settings);
    }

    public static function navSync($data)
    {
        $settings = self::$settings;

        $remove = $data["remove"];
        //if remove contains id, find nav item page and set menu to false
        if ($remove != null || $remove != "") {
            $page = (new Book("../content/pages"))->findPageById($remove);
            $page["menu"] = "false";
            $page["published"]
            ? ($page["published"] = "true")
            : ($page["published"] = "false");
            $page["featured"]
            ? ($page["featured"] = "true")
            : ($page["featured"] = "false");
            $page["deleted"]
            ? ($page["deleted"] = "true")
            : ($page["deleted"] = "false");
            $updated = new \Moment\Moment();
            $created = new \Moment\Moment($page["rawCreated"]);
            $page["created"] = $created->format("Y-m-d\TH:i:sP");
            $page["updated"] = $updated->format("Y-m-d\TH:i:sP");

            $md = DocTools::objectToMD($page);

            if ($page["layout"] == "index") {
                $writePath = "../content/pages/start/index.md";
            } else {
                $writePath =
                "../content/pages/" . $page["path"] . "/" . $page["slug"] . ".md";
            }

            DocTools::writePages("write", $page["path"], $writePath, $md);
        }

        $settings["menu"] = [];
        $items = $data["menu"];
        foreach ($items as $item) {
            array_push($settings["menu"], [
            "title" => $item["title"],
            "id" => $item["id"],
            "uuid" => $item["uuid"],
            "slug" => $item["slug"],
            "path" => $item["path"],
            ]);
        }

        DocTools::writeSettings("../config/settings.json", $settings);
    }

    public function getFolks($key = null)
    {
        if (isset($key)) {
            $member = Session::get("member");
            $found = find($this->folks, ["handle" => $member["handle"]]);
            if ($found) {
                return $found[$key];
            }
        } else {
            return $this->folks;
        }
    }

    public function getSettings($key = null)
    {
        return self::$settings;
    }

    public static function getTags()
    {
        return self::$tags;
    }

    public static function updateGlobalData($key, $data)
    {
        $settings = self::$settings;
        $settings["global"][$key] = $data;
        DocTools::writeSettings("../config/settings.json", $settings);
    }

    public static function getCurrentIndex()
    {
        $settings = self::$settings;
        return $settings["library_stats"]["current_index"];
    }

    public static function updateIndex()
    {
        $settings = self::$settings;

        $settings["library_stats"]["current_index"] =
        $settings["library_stats"]["current_index"] + 1;

        DocTools::writeSettings("../config/settings.json", $settings);
    }

    public static function updateMenu($body)
    {
        $settings = self::$settings;
        //$menu = $settings["menu"];
        $item = [
        "title" => $body["title"],
        "id" => $body["id"],
        "uuid" => $body["uuid"],
        "slug" => $body["slug"],
        "path" => $body["path"],
        ];
        if ($body["menu"] == "true") {
            if (!find($settings["menu"], ["uuid" => $item["uuid"]])) {
                array_push($settings["menu"], $item);
            }
        } else {
            if (find($settings["menu"], ["uuid" => $item["uuid"]])) {
                pull($settings["menu"], $item);
            }
        }
        DocTools::writeSettings("../config/settings.json", $settings);
    }

    public static function updateTags()
    {
        $tags = Sorting::tags();
        DocTools::writeSettings("../config/tags.json", $tags);
    }
}