using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NZSDK_CSharpSample.Properties;

namespace NZSDK_CSharpSample
{
    public partial class NZSdkForm : Form
    {
        #region Fields

        NZSDK.SDKKinds sdkKind;
        NZSDK sdk;

        #endregion

        #region Constructors

        public NZSdkForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        public NZSDK.SDKKinds SDKKind
        {
            get { return sdkKind; }
            set { sdkKind = value; }
        }

        #endregion

        #region Private / Protected Methods

        private void SaveConnection()
        {
            if (sdkKind == NZSDK.SDKKinds.NVR)
            {
                Settings.Default.NZSDK_NvrAddress = txtAddress.Text;
                Settings.Default.NZSDK_NvrHttpPort = txtHttpPort.Text;
                Settings.Default.NZSDK_NvrRtspPort = txtRtspPort.Text;
                Settings.Default.NZSDK_NvrLoginId = txtLoginId.Text;
                Settings.Default.NZSDK_NvrPassword = txtPassword.Text;
            }
            else
            {
                Settings.Default.NZSDK_NrsAddress = txtAddress.Text;
                Settings.Default.NZSDK_NrsHttpPort = txtHttpPort.Text;
                Settings.Default.NZSDK_NrsRtspPort = txtRtspPort.Text;
                Settings.Default.NZSDK_NrsLoginId = txtLoginId.Text;
                Settings.Default.NZSDK_NrsPassword = txtPassword.Text;
            }

            Settings.Default.Save();
        }

        private void LoadDefaultConnection()
        {
            txtAddress.Text = Settings.Default.NZSDK_DefaultAddress;
            txtHttpPort.Text = (sdkKind == NZSDK.SDKKinds.NVR) ? Settings.Default.NZSDK_DefaultNvrHttpPort : Settings.Default.NZSDK_DefaultNrsHttpPort;
            txtRtspPort.Text = Settings.Default.NZSDK_DefaultRtspPort;
            txtLoginId.Text = Settings.Default.NZSDK_DefaultLoginId;
            txtPassword.Text = Settings.Default.NZSDK_DefaultPassword;
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            if (!e.Cancel)
            {
                pageNZSdkLive1.Close();
                pageNZSdkPlayback1.Close();
            }
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);

            sdk.Dispose();
        }

        #endregion

        #region Public Methods

        #endregion

        #region Event Handlers

        private void NZSdkForm_Load(object sender, EventArgs e)
        {
            sdk = new NZSDK(sdkKind);
            if (sdkKind == NZSDK.SDKKinds.NVR)
            {
                lblRtspPort.Enabled = false;
                txtRtspPort.Enabled = false;
            }
            else
            {
                tabControl1.TabPages.Remove(tabPageData);
                tabControl1.TabPages.Remove(tabPagePlayback);
            }


            if (String.IsNullOrEmpty((sdkKind == NZSDK.SDKKinds.NVR) ? Settings.Default.NZSDK_NvrAddress : Settings.Default.NZSDK_NrsAddress))
            {
                LoadDefaultConnection();
            }
            else if (sdkKind == NZSDK.SDKKinds.NVR)
            {
                txtAddress.Text = Settings.Default.NZSDK_NvrAddress;
                txtHttpPort.Text = Settings.Default.NZSDK_NvrHttpPort;
                txtRtspPort.Text = Settings.Default.NZSDK_NvrRtspPort;
                txtLoginId.Text = Settings.Default.NZSDK_NvrLoginId;
                txtPassword.Text = Settings.Default.NZSDK_NvrPassword;
            }
            else
            {
                txtAddress.Text = Settings.Default.NZSDK_NrsAddress;
                txtHttpPort.Text = Settings.Default.NZSDK_NrsHttpPort;
                txtRtspPort.Text = Settings.Default.NZSDK_NrsRtspPort;
                txtLoginId.Text = Settings.Default.NZSDK_NrsLoginId;
                txtPassword.Text = Settings.Default.NZSDK_NrsPassword;
            }

            tabControl1.Enabled = false;
            pageNZSdkData1.SDK = sdk;
            pageNZSdkLive1.SDK = sdk;
            pageNZSdkPlayback1.SDK = sdk;
        }

        private void butConnect_Click(object sender, EventArgs e)
        {
            bool bResult = sdk.Connect(txtAddress.Text, 
                sdkKind == NZSDK.SDKKinds.NVR ? txtHttpPort.Text : String.Format("http={0}&rtsp={1}", txtHttpPort.Text, txtRtspPort.Text), 
                txtLoginId.Text, txtPassword.Text);
            if (!bResult)
                return;

            tabControl1.Enabled = true;
            SaveConnection();
            butConnect.Enabled = false;
        }

        private void butDisconnect_Click(object sender, EventArgs e)
        {
            sdk.Disconnect();
            tabControl1.Enabled = false;
            butConnect.Enabled = true;
        }

        private void butDefault_Click(object sender, EventArgs e)
        {
            LoadDefaultConnection();
        }

        #endregion
    }
}
