using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells;
using Newtonsoft.Json;
using Prism.Mvvm;

namespace StartServerWPF.Models
{
    public class WorkareasModelArray
    {
        public int selectIndex { get; set; }
        public List<Workareas> workarea { get; set; }
    }
    public class Workareas 
    {
        public string workareaname { get; set; }
        public string filepath { get; set; }
    }

    public class WorkareaModel : BindableBase
    {
        private string workareaname1;
        private bool apmpush2wx1;
        private bool toolpush2wx1;
        private string savepath1;
        private string chartsavepath1;
        private string apmsavepath1;
        private string toolsavepath1;
        private int delayTime1;
        private ApmsModel apmModel1;

        [JsonProperty("workareaname")]
          public string workareaname { get => workareaname1; set => SetProperty(ref workareaname1, value); }
          
        [JsonProperty("savepath")]
         public string savepath { get => savepath1; set => SetProperty(ref savepath1, value); }
   
        [JsonProperty("chartsavepath")]
        public string chartsavepath { get => chartsavepath1; set => SetProperty(ref chartsavepath1, value); }

        [JsonProperty("apmsavepath")]
        public string apmsavepath { get => apmsavepath1; set => SetProperty(ref apmsavepath1, value); }
       
        [JsonProperty("toolsavepath")]
        public string toolsavepath { get => toolsavepath1; set => SetProperty(ref toolsavepath1, value); }

        [JsonProperty("apmpush2wx")]
        public bool apmpush2wx { get => apmpush2wx1; set => SetProperty(ref apmpush2wx1, value); }
    
        [JsonProperty("toolpush2wx")]
        public bool toolpush2wx { get => toolpush2wx1; set => SetProperty(ref toolpush2wx1, value); }

        [JsonProperty("delayTime")]
        public int delayTime { get => delayTime1; set => SetProperty(ref delayTime1, value); }

        public ApmsModel apmModel { get => apmModel1; set => SetProperty(ref apmModel1, value); }

        public StationConfigModel StationConfig { get; set; }

        public List<StationControlModel> CreateStationFromCSV(string fn, string location)
        {
            if (!File.Exists(fn)) return null;
            List<StationControlModel> stations = new List<StationControlModel>();
            Aspose.Cells.TxtLoadOptions lo = new TxtLoadOptions();
            //Debug.WriteLine(Encoding.Default.ToString());
            lo.Encoding = Encoding.Default;//设置编码方式
            Workbook workbook = new Workbook(fn, lo);

            //配置读取文件的类型（CSV）
            workbook.FileFormat = FileFormatType.Csv;//可在此配置Excel文件类型
            Worksheet worksheet = workbook.Worksheets[0];//默认第一个Sheet页

            Cells cells = worksheet.Cells;

            //读取到DataTable中            
            DataTable dt = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 1, true);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                StationControlModel model = new StationControlModel();
                model.NetWork = row[0].ToString();
                model.Num = row[1].ToString();
                model.Coordinate = new Coordinate3D(double.Parse(row[2].ToString()), double.Parse(row[3].ToString()), double.Parse(row[4].ToString()));
                model.Sens = double.Parse(row[5].ToString());
                model.BeginUseTime = DateTime.Parse(row[6].ToString());
                model.StopUseTime = DateTime.Parse(row[7].ToString());
                model.Location = location;
                stations.Add(model);
            }
            stations = stations.OrderBy(x => x.Num).ToList();
            //释放资源         
            workbook = null;
            worksheet = null;
            return stations;
        }
    }

    public class StationConfigModel: BindableBase
    {
        private List<StationState> stations1;

        [JsonProperty("network")]
        public string Network { get; set; }

        [JsonProperty("location")]
        public string Location { get; set; }

        [JsonProperty("channels")]
        public string Channels { get; set; }

        [JsonProperty("stations")]
        public List<StationState> Stations { get => stations1; set => SetProperty(ref stations1, value); }
    }

    public class StationState: BindableBase
    {
        private string name;
        private bool isEnable;

        public string Name { get => name; set => SetProperty(ref name, value); }
        public bool IsEnable { get => isEnable; set => SetProperty(ref isEnable, value); }
    }
    public class Coordinate3D
    {
        [JsonProperty("X")]
        public double X { get; set; }

        [JsonProperty("Y")]
        public double Y { get; set; }

        [JsonProperty("Z")]
        public double Z { get; set; }
        public Coordinate3D()
        {
            
        }
        public Coordinate3D(double x,double y,double z)
        {
            X= x;
            Y= y;
            Z= z;
        }
    }
}
