using System.IO;
using Assets.Source.Core.Data.SaveData;
using Assets.Source.Patterns.ScriptableObjects;
using ProtoBuf;
using UnityEngine;

namespace Assets.Source.Content.Workers
{
    public class WorkerPlayerProgress : Worker
    {
        private const string FILENAME_PROGRESS = "/progress.bin";

        public ProgressData Data;

        public static string ProgressDataPath => Application.persistentDataPath + FILENAME_PROGRESS;

        protected override void VInit()
        {
            base.VInit();
            LoadProgress();
        }
        
        #region GENERAL

        public void LoadProgress()
        {
            if (!File.Exists(ProgressDataPath))
            {
                Data = new ProgressData();
                return;
            }
            var file = File.OpenRead(ProgressDataPath);
            Data = Serializer.Deserialize<ProgressData>(file);
        }

        public void SaveProgress()
        {
            var path = ProgressDataPath;
            var file = File.Create(path);
            Serializer.Serialize(file, Data);
            file.Flush();
            file.Close();
        }

        public void ResetProgress()
        {
            Data = new ProgressData();
            SaveProgress();
        }

        #endregion
    }
}
