using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Helper;
using System.IO;
using NetSDKCS;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace AlarmDemo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        MainWindow mainWindow; //main window object.
        fDisConnectCallBack disConnect; //call back for device disconnect.

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            LoadLanguage();
            mainWindow = new MainWindow();
            disConnect = new fDisConnectCallBack(DisConnect); //set disconnect callback.
            try
            {
                bool res = NETClient.Init(disConnect, IntPtr.Zero, null); //init NetClient.
                if (res == false)
                {
                    LogHelper.WriteWithMethod(res.ToString());
                    MessageBox.Show((string)App.Current.Resources["ID_INIT_ERROR"]);
                }
                mainWindow.Show();
                LogHelper.WriteWithMethod("Leave");
            }
            catch (Exception ex)
            {
                LogHelper.WriteException(ex);
                MessageBox.Show(ex.Message);
                Process.GetCurrentProcess().Kill(); //general cannot find dll about netsdk or cannot call dll.
            }
        }

        /// <summary>
        /// load language
        /// </summary>
        private void LoadLanguage()
        {
            LogHelper.WriteWithMethod("Enter");
            string path = AppDomain.CurrentDomain.BaseDirectory + "Language";
            string fileNamePath = string.Empty;
            ResourceDictionary resourceDictionary = new ResourceDictionary();
            switch (System.Globalization.CultureInfo.CurrentCulture.LCID)
            {
                case 0x00804:
                    fileNamePath = path + "\\zh-cn.xaml";
                    if (!File.Exists(fileNamePath))
                    {
                        resourceDictionary.Source = new Uri("pack://application:,,,/language/zh-cn.xaml");
                    }
                    else
                    {
                        resourceDictionary.Source = new Uri(fileNamePath, UriKind.RelativeOrAbsolute);
                    }
                    break;
                default:
                    fileNamePath = path + "\\en-us.xaml";
                    if (!File.Exists(fileNamePath))
                    {
                        resourceDictionary.Source = new Uri("pack://application:,,,/language/en-us.xaml");
                    }
                    else
                    {
                        resourceDictionary.Source = new Uri(fileNamePath, UriKind.RelativeOrAbsolute);
                    }
                    break;
            }
            App.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            LogHelper.WriteWithMethod("Leave");
        }

        //device disconnect callback function.
        private void DisConnect(IntPtr lLoginID, IntPtr pchDVRIP, int nDVRPort, IntPtr dwUser)
        {
            LogHelper.WriteWithMethod("Enter");
            string ip = Marshal.PtrToStringAnsi(pchDVRIP);
            try
            {
                Dispatcher.BeginInvoke((Action<string>)UpdateDisConnectUI, ip);
                NETClient.StopListen(lLoginID);
            }
            catch (Exception ex)
            {
                if (ex is NETClientExcetion)
                {
                    Dispatcher.BeginInvoke((Action<NETClientExcetion, string, string>)mainWindow.UpdateError, ex as NETClientExcetion, "StopListen", ip);
                }
                else
                {
                    LogHelper.WriteException(ex);
                }
            }
            LogHelper.WriteWithMethod("Leave");
        }

        private void UpdateDisConnectUI(string ip)
        {
            LogHelper.WriteWithMethod("Enter");
            foreach (Device item in mainWindow.connected_listbox.Items)
            {
                if (item.IP == ip)
                {
                    item.DeviceStatus = Status.OffLine;
                    mainWindow.connected_listbox.Items.Refresh();
                    break;
                }
            }
            LogHelper.WriteWithMethod("Leave");
        }

        //ovveride onexit function for call cleanup.
        protected override void OnExit(ExitEventArgs e)
        {
            LogHelper.WriteWithMethod("Enter");
            base.OnExit(e);
            NETClient.Cleanup();                        //cleanup NetClient resources.
            LogHelper.WriteWithMethod("Leave");
        }
    }
}
