using Cryville.Crtr.Extension;
using Cryville.Crtr.Skin;
using Newtonsoft.Json;
using System.IO;
using System.Text;

namespace Cryville.Crtr.Extensions.Umg {
	public class SkinResourceImporter : ResourceConverter {
		static readonly string[] SUPPORTED_FORMATS = { ".umgs" };
		public override string[] GetSupportedFormats() {
			return SUPPORTED_FORMATS;
		}

		public override void Convert(FileInfo file, ConversionSession ses) {
			using (StreamReader reader = new StreamReader(file.FullName, Encoding.UTF8)) {
				var data = JsonConvert.DeserializeObject<SkinDefinition>(reader.ReadToEnd());
				ses.AddResource(new SkinResource(data.name, file));
			}
		}
	}
	public class SkinResource : FileResource {
		public string RulesetName { get; private set; }
		public SkinResource(string name, FileInfo master) : base(name, master) {
			using (var reader = new StreamReader(master.FullName)) {
				var meta = JsonConvert.DeserializeObject<SkinDefinition>(reader.ReadToEnd());
				RulesetName = meta.ruleset;
				Attachments.Add(new FileInfo(Path.Combine(master.Directory.FullName, meta.data + ".pdt")));
				foreach (var frame in meta.frames) {
					Attachments.Add(new FileInfo(Path.Combine(master.Directory.FullName, frame)));
				}
			}
		}
	}
}
