using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Cogwheel;
using SessionZero.Data;
using SessionZero.Data.Sztl;

namespace SessionZero;

public static class TestingCommands
{
    private static Datapack pack;
    private static Dataset set;
    private static SzTemplate template;
    private static SzDataObject obj;
    
    
    [Command(Name = "createpack")]
    private static void CreateTestPack()
    {
        pack = new Datapack("SessionZero Core", "sz_core");
        pack.Save();

        set = new Dataset("Core Items", "core_items", "item", "core_item");
        pack.AddDataset(set);
        
        string toml = @"
            [metadata]
            template_type = ""data""
            id = ""core_item""
            uuid = ""0""
            data_type = ""item""
            version = ""1.0.0""
            description = ""Core SessionZero item template""

            [name]
            type = ""text""

            [description]
            type = ""textblock""

            [consumable]
            type = ""bool""
            default_value = false

            [stats.value]
            type = ""number""

            [stats.weight]
            type = ""number""

            [stats.modifiers.base]
            type = ""number""
            default_value = 1
            ";

        try
        {
            template = SztlParser.ParseTemplate(toml);
            pack.AddTemplate(template);

            try
            {
                obj = SzObjectFactory.CreateObject(template) as SzDataObject;
                obj.Id = "test_item";
                set.AddObject(obj);
            }
            catch (Exception e)
            {
                COGWHEEL.LogError($"Error parsing template: {e.Message}");
            }
        }
        catch (Exception ex)
        {
            COGWHEEL.LogError($"Error adding template: {ex.Message}");
        }
    }
    
    
    [Command(Name = "testdata")]
    private static void TestParseTemplate()
    {
        try
        {
            var objFromJson = JsonSerializer.Deserialize<SzObject>(File.ReadAllText($"{AppManager.DatapacksDirectory}/sz_core/datasets/objects/test_item.szo"));
            COGWHEEL.Log($"The object's id is {objFromJson.Id}");
        }
        catch (Exception e)
        {
            COGWHEEL.LogError($"Error deserializing object: {e.Message}");
        }
    }
}