using MainShell.Filewritable;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MXJM.FileWritable
{
    public class WaferMapFile : FileWriteBase
    {
        public WaferMapFile()
        { 
            Description = "芯片定位识别点数据保存";
        }
        public override void Save()
        {
            var (row, col, angle, overlapRow, overlapCol, overlapAngle) = UserData as Tuple<double[], double[], double[], double[], double[], double[]>;
            if (row == null || col == null || angle == null || overlapRow == null || overlapCol == null || overlapAngle == null)
            {
                throw new InvalidDataException("UserData must be a Tuple of six double arrays.");
            }
            SaveList(Paths.WaferFileNames["Row"], row.ToList());
            SaveList(Paths.WaferFileNames["Column"], col.ToList());
            SaveList(Paths.WaferFileNames["Angle"], angle.ToList());
            SaveList(Paths.WaferFileNames["RowOverlap"], overlapRow.ToList());
            SaveList(Paths.WaferFileNames["ColumnOverlap"], overlapCol.ToList());
            SaveList(Paths.WaferFileNames["AngleOverlap"], overlapAngle.ToList());
        }
        void SaveList(string fileName, List<double> list)
        {
            string directory = Path.GetDirectoryName(fileName);
            if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            string content = list == null || list.Count == 0
                ? string.Empty
                : string.Join(Environment.NewLine, list);
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
            {
                sw.Write(content);
            }
        }
    }

   
}
