using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Http.Core;
using Http.Core.Exceptions;
using Http.Core.Extensions;
using Newtonsoft.Json;
using PCUT.Entities;
using PCUT.Entities.ApiResponse;
using PCUT.Models;
using static Http.Core.Constants.HttpConstants;

namespace PCUT.ViewModels
{
    public class DesignCenterViewModel : NotificationBase
    {
        public ObservableCollection<PortInfo> Ports { get; set; }
        public ObservableCollection<PaperSize> PaperSizes { get; }

        private double _width;
        public double Width
        {
            get { return _width; }
            set
            {
                SetProperty(ref _width, value);
            }
        }

        private double _height;
        public double Height
        {
            get { return _height; }
            set
            {
                SetProperty(ref _height, value);
            }
        }

        private int _selectedHeightIndex = -1;
        public int SelectedHeightIndex
        {
            get { return _selectedHeightIndex; }
            set
            {
                if (_selectedHeightIndex != value && value >= 0 && value < PaperSizes.Count)
                {
                    SetProperty(ref _selectedHeightIndex, value);
                    Width = PaperSizes[value].Width;
                    Height = PaperSizes[value].Height;
                }
            }
        }

        private int _selectedPortIndex = -1;
        public int SelectedPortIndex
        {
            get { return _selectedPortIndex; }
            set
            {
                if (_selectedPortIndex != value)
                {
                    SetProperty(ref _selectedPortIndex, value);
                }
            }
        }

        public string SelectedPortName => Ports.Any() && _selectedPortIndex >= 0 && _selectedPortIndex < Ports.Count ? Ports[_selectedPortIndex].PortName : null;
        public string SelectedDeviceName => Ports.Any() && _selectedPortIndex >= 0 && _selectedPortIndex < Ports.Count ? Ports[_selectedPortIndex].DeviceName : null;

        private string _name;
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref _name, value); }
        }

        private string _type;
        public string Type
        {
            get { return _type; }
            set { SetProperty(ref _type, value); }
        }

        private string _brand;
        public string Brand
        {
            get { return _brand; }
            set
            {
                SetProperty(ref _brand, value);
            }
        }

        private string _year;
        public string Year
        {
            get { return _year; }
            set
            {
                SetProperty(ref _year, value);
            }
        }

        private string _model;
        public string Model
        {
            get { return _model; ; }
            set
            {
                SetProperty(ref _model, value); ;
            }
        }

        private string _series;
        public string Series
        {
            get { return _series; }
            set
            {
                SetProperty(ref _series, value);
            }
        }

        public string ItemId { get; set; }
        public string FileId { get; set; }
        public DesignCenterViewModel()
        {
            Ports = new ObservableCollection<PortInfo>();
            PaperSizes = new ObservableCollection<PaperSize>
            {
                new PaperSize(7000, 360),
                new PaperSize(10000, 550),
            };
        }

        public async Task<bool> GetCdrFileAsync(string fileId)
        {
            using(var httpClient = HttpClientFactory.CreateClient(ClientNames.ApiClient))
            {
                try
                {
                    var response = await httpClient.GetAsync(Api.CdrFileById.FormatRoute(fileId));
                    if (response.IsSuccessStatusCode)
                    {
                        var apiResponse = await response.DeserializeObjectAsync<DataResponse<CdrFile>>();
                        if (apiResponse?.Data != null)
                        {
                            Name = apiResponse.Data.Name ?? string.Empty;
                            Type = apiResponse.Data.Type?.Value ?? string.Empty;
                            Brand = apiResponse.Data.Brand?.Value ?? string.Empty;
                            Year = apiResponse.Data.Year?.Value ?? string.Empty;
                            Model = apiResponse.Data.Model?.Value ?? string.Empty;
                            Series = apiResponse.Data.Series?.Value ?? string.Empty;

                            ItemId = fileId;
                            FileId = apiResponse.Data.File?.Id;
                            return true;
                        }
                    }
                }
                catch (Exception ex) when (!(ex is AppOutdatedException))
                {

                }
            }
            return false;
        }

        private double _minSize = 0;
        public double MinSize
        {
            get => _minSize;
            set
            {
                SetProperty(ref _minSize, value);
            }
        }

        public bool UseOrigin { get; set; } = true;
    }

    public class PortInfo
    {
        public string PortName { get; set; }
        public string DeviceName { get; set; }

        public PortInfo(string portName, string deviceName)
        {
            PortName = portName;
            DeviceName = deviceName;
        }
    }

    public class PaperSize
    {
        public double Width { get; set; }
        public double Height { get; set; }

        public PaperSize(double width, double height)
        {
            Width = width;
            Height = height;
        }
    }
}
