using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BPASmartClient.DialogWindow;
using BPASmartClient.Helper;
using BPASmartClient.Model;
using BPASmartClient.ViewModel;
using Newtonsoft.Json;

namespace BPASmartClient.Control
{
    /// <summary>
    /// ShopDeviceConfigView.xaml 的交互逻辑
    /// </summary>
    public partial class ShopDeviceConfigView : UserControl
    {
        public ShopDeviceConfigView()
        {
            InitializeComponent();
        }

        private void IcoButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NewShopWindow newShopWindow = new NewShopWindow();
            var res = newShopWindow.ShowDialog();

            if (res != null && res == true)
            {
                VisibilityControl();
            }
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

            Refresh();
        }

        private void Refresh()
        {
            if (this.tv.SelectedValue is DeviceConfigModel deviceConfigModel)
            {
            }
            else if (this.tv.SelectedValue is DeviceModel deviceModel)
            {
                this.Device.Visibility = Visibility.Visible;
                this.CommType.Visibility = Visibility.Collapsed;
                this.VarTab.Visibility = Visibility.Collapsed;
                this.Serial.Visibility = Visibility.Collapsed;
                this.Network.Visibility = Visibility.Collapsed;
                this.GridCommunication.Visibility = Visibility.Collapsed;
            }
            else if (this.tv.SelectedValue is CommunicationModel communicationModel)
            {
                this.Device.Visibility = Visibility.Collapsed;
                this.CommType.Visibility = Visibility.Visible;
                this.VarTab.Visibility = Visibility.Visible;
                this.GridCommunication.Visibility = Visibility.Visible;
                this.Serial.Visibility = (bool)this.rbSerial.IsChecked ? Visibility.Visible : Visibility.Collapsed;
                this.Network.Visibility = (bool)this.rbNetwork.IsChecked ? Visibility.Visible : Visibility.Collapsed;


                if (ShopDeviceConfigViewModel.deviceConfig.Count > 0)
                {
                    int index = Array.FindIndex(ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ToArray(), p => p.Id == communicationModel.DeviceModelId);
                    if (index >= 0 && index < ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.Count)
                    {
                        int commIndex = Array.FindIndex(ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ToArray(), p => p.DeviceModelId == communicationModel.DeviceModelId);
                        if (commIndex >= 0 && commIndex < ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.Count)
                        {
                            if (ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).variables.Count <= 0)
                            {
                                for (int i = 0; i < 20; i++)
                                {
                                    ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).variables.Add(new Variable()
                                    {
                                        Id = ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).variables.Count,
                                        Address = string.Empty,
                                        ReadLeng = 0
                                    });
                                }
                            }
                        }
                    }
                }

            }
        }

        private void rbNetwork_Checked(object sender, RoutedEventArgs e)
        {
            rbVisibility();
        }

        private void rbSerial_Checked(object sender, RoutedEventArgs e)
        {
            rbVisibility();
        }

        private void rbVisibility()
        {
            this.Network.Visibility = (bool)this.rbNetwork.IsChecked ? Visibility.Visible : Visibility.Collapsed;
            this.Serial.Visibility = (bool)this.rbSerial.IsChecked ? Visibility.Visible : Visibility.Collapsed;
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            ShopDeviceConfigViewModel.Shops.Clear();
            DirectoryInfo directoryInfo = new DirectoryInfo(LocaPath.GetInstance().GetDeviceConfigPath);
            var files = directoryInfo.GetFiles();
            foreach (var item in files)
            {
                var res = System.IO.Path.GetFileNameWithoutExtension(item.FullName);
                if (res != null && res.Length > 0 && item.FullName.Contains("json")) ShopDeviceConfigViewModel.Shops.Add(res);
            }
        }

        private void shop_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig");
            if (this.shop.Items.Contains(text)) this.shop.SelectedValue = text;
            DirectoryInfo directoryInfo = new DirectoryInfo(LocaPath.GetInstance().GetDeviceConfigPath);
            var files = directoryInfo.GetFiles();
            if (files != null && this.shop.SelectedValue != null && this.shop.SelectedValue.ToString().Length > 0)
            {
                var file = files.FirstOrDefault(p => p.FullName.Contains(this.shop.SelectedValue.ToString()));
                if (file != null && File.Exists(file.FullName))
                {
                    string JsonString = File.ReadAllText(file.FullName);
                    var result = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
                    if (result != null)
                    {
                        ShopDeviceConfigViewModel.deviceConfig.Clear();
                        foreach (var item in result)
                        {
                            ShopDeviceConfigViewModel.deviceConfig.Add(item);
                        }
                        VisibilityControl();
                    }
                }
            }
            TextHelper.GetInstance.WriteTextInfo(this.shop.SelectedValue?.ToString(), "StartShop", "DeviceConfig");
        }

        private void VisibilityControl()
        {
            if (this.Device != null) this.Device.Visibility = Visibility.Collapsed;
            if (this.CommType != null) this.CommType.Visibility = Visibility.Collapsed;
            if (this.VarTab != null) this.VarTab.Visibility = Visibility.Collapsed;
            if (this.Serial != null) this.Serial.Visibility = Visibility.Collapsed;
            if (this.Network != null) this.Network.Visibility = Visibility.Collapsed;
            if (this.GridCommunication != null) this.GridCommunication.Visibility = Visibility.Collapsed;
        }
    }
}
