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

namespace Cryville.Crtr.Extensions.Umg {
	public class RulesetResourceImporter : ResourceConverter {
		static readonly string[] SUPPORTED_FORMATS = { ".umgr" };
		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<RulesetDefinition>(reader.ReadToEnd());
				ses.AddResource(new RulesetResource(data.name, file));
			}
		}
	}
	public class RulesetResource : FileResource {
		public RulesetResource(string name, FileInfo master) : base(name, master) {
			using (var reader = new StreamReader(master.FullName)) {
				var meta = JsonConvert.DeserializeObject<RulesetDefinition>(reader.ReadToEnd());
				Attachments.Add(new FileInfo(Path.Combine(master.Directory.FullName, meta.data + ".pdt")));
			}
		}
	}
}
