using System;
using System.Linq;
using System.Threading.Tasks;
using ElectronNET.API;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System.IO;
using Timelapse.Api;
using Microsoft.AspNetCore.Components;
using SixLabors.ImageSharp.Processing;
namespace Timelapse.Data
{
    public class ExtensionProjectService
    {
        
        public static RecentFileService RecentFile {get;set;}
        public async Task SendImageAsync(Image<Rgb24> imageFrame)
        {
            imageFrame.Mutate(x=> x.Resize(new Size(Project.Width,Project.Height)));
            await Extensions.OnNewFrame(imageFrame);
            await imageFrame.SaveAsPngAsync(Path.Combine(ProjectDirectory,"Sections",Project.CurrentSection,$"{FrameIndex++}.png"));
        }
        public string ProjectFileName {get; internal set;}
        public string ProjectDirectory { get {return Path.Combine(Path.GetDirectoryName(ProjectFileName),Path.GetFileNameWithoutExtension(ProjectFileName));} }
        public int FrameIndex {get;internal set;}
        public bool IsProjectLoaded {get {return  Project!=null;}}

        public TimelapseProject Project {get;set;}

        public void StartRecording()
        {
            //start recording

            //tell extensions
            Extensions.OnStartRecording();
        }
        public void EnableOneX()
        {
            //enable oneX

            //tell extensions
            Extensions.OnEnableOneX();
        }
        public void DisableOneX()
        {
            //disable oneX

            //tell extensions
            Extensions.OnDisableOneX();
        }
        public void StopRecording()
        {
            //stop recording

            //tell extensions
            Extensions.OnStopRecording();
        }
        public void LoadProject(string path)
        {
            RecentFile.Add(path);
           
                SaveProject();
            
            ProjectFileName = path;
            TimelapseProject project= Newtonsoft.Json.JsonConvert.DeserializeObject<TimelapseProject>(File.ReadAllText(path));
            if(string.IsNullOrWhiteSpace(project.CurrentSection))
            {
                project.CurrentSection="Default";
            }
            string sectionPath = Path.Combine(ProjectDirectory,"Sections",project.CurrentSection);
            if(!Directory.Exists(sectionPath))
            {
                Directory.CreateDirectory(sectionPath);
            }
            int count = Directory.GetFiles(sectionPath,"*.png").Length+1;
            FrameIndex=count;
            Project=project;
            Extensions.OnLoadProject(project);
            
        }
        public void SaveProject()
        {
            if(IsProjectLoaded)
            {
               SaveProject(ProjectFileName,Project);
            }
        }
        public void SaveProject(string fileloc,TimelapseProject prj)
        {
            string json= Newtonsoft.Json.JsonConvert.SerializeObject(prj);
               File.WriteAllText(fileloc,json);
        }
        
    }
}