using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            if (form.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            StringBuilder keyID = new StringBuilder();
            int ret = InteropHelper.KCheckinByKeycardV3(txtUserName.Text, txtPassword.Text, txtLockID.Text,
                form.KeyOwner, "", "", form.EndDate, 0, txtPublicDoors.Text, txtElevators.Text, keyID);
            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            InteropHelper.KFireBeepSuccess();
            MessageBox.Show("Operate done, keyID=" + keyID.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            if (form.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            StringBuilder keyID = new StringBuilder();
            StringBuilder pinCode = new StringBuilder();
            int ret = InteropHelper.KCheckinByPassword(txtUserName.Text, txtPassword.Text, txtLockID.Text,
                form.KeyOwner, "", "", form.EndDate, 0, keyID, pinCode);

            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            InteropHelper.KFireBeepSuccess();
            MessageBox.Show("Operate done, keyID=" + keyID.ToString() + ", pinCode="+ pinCode.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form3 form = new Form3();
            if (form.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            StringBuilder keyID = new StringBuilder();
            StringBuilder verifyCode = new StringBuilder();
            StringBuilder pinCode = new StringBuilder();
            int ret = InteropHelper.KCheckinByMobileWithPinCodeV3(txtUserName.Text, txtPassword.Text, txtLockID.Text,
                form.KeyOwner, "", form.Mobile, "", form.EndDate, 1, 0, txtPublicDoors.Text, txtElevators.Text, keyID, verifyCode, pinCode);

            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            InteropHelper.KFireBeepSuccess();
            MessageBox.Show("Operate done, keyID=" + keyID.ToString() + ", verifyCode=" + 
                verifyCode.ToString() + ", pinCode=" + pinCode.ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form4 form = new Form4();
            if (form.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            StringBuilder keyID = new StringBuilder();
            int ret = InteropHelper.KCheckinByUserIdV3(txtUserName.Text, txtPassword.Text, txtLockID.Text,
                form.KeyOwner, "", form.UserId, form.ClientID, form.ClientSecret, "", form.EndDate, 0, txtPublicDoors.Text, txtElevators.Text, keyID);
            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            InteropHelper.KFireBeepSuccess();
            MessageBox.Show("Operate done, keyID=" + keyID.ToString());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            StringBuilder keyID = new StringBuilder();
            StringBuilder lockID = new StringBuilder();
            StringBuilder makeDate = new StringBuilder();
            StringBuilder endDate = new StringBuilder();
            StringBuilder owner = new StringBuilder();
            
            int ret = InteropHelper.KReadKeycard(txtUserName.Text, txtPassword.Text, keyID, lockID, owner,
                makeDate, endDate);
            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            StringBuilder cardID = new StringBuilder();
            ret = InteropHelper.KReadKeycardUID(cardID);

            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            uint nCardID = UInt32.Parse(cardID.ToString());
            UInt32 revertCardID = (((nCardID >> 24) & 0xFF) << 0) | (((nCardID >> 16) & 0xFF) << 8) | (((nCardID >> 8) & 0xFF) << 16) | (((nCardID >> 0) & 0xFF) << 24);

            String keyContent = revertCardID.ToString().PadLeft(10, '0') + "," + owner + "," + Environment.NewLine;

            File.AppendAllText("./carddata.csv", keyContent);

            InteropHelper.KFireBeepSuccess();
            MessageBox.Show("keyID=" + keyID.ToString() + ",lockID=" + lockID.ToString() + "\r\n" +
                "make date:" + makeDate + "\r\n" +
                "end date:" + endDate + "\r\n" +
                "owner=" + owner);

        }

        private void button6_Click(object sender, EventArgs e)
        {
            int ret = InteropHelper.KCancelKeyByKeycard(txtUserName.Text, txtPassword.Text);
            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            InteropHelper.KFireBeepSuccess();
            MessageBox.Show("Operate done");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Form5 form = new Form5();
            if (form.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            int ret = InteropHelper.KCancelKeyByLockID(txtUserName.Text, txtPassword.Text, form.LockID);
            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            InteropHelper.KFireBeepSuccess();
            MessageBox.Show("Operate done");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Form6 form = new Form6();
            if (form.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            int ret = InteropHelper.KCancelKeyByKeyID(txtUserName.Text, txtPassword.Text, form.KeyID);
            if (ret != 0)
            {
                InteropHelper.KFireBeepFailure();
                MessageBox.Show("Operate fail:" + ret);
                return;
            }

            InteropHelper.KFireBeepSuccess();
            MessageBox.Show("Operate done");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (this.timer1.Enabled)
            {
                this.button9.Text = "Auto Read(ON)";
                this.timer1.Enabled = false;
            } else
            {

                this.button9.Text = "Auto Read(OFF)";
                this.timer1.Enabled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            StringBuilder keyID = new StringBuilder();
            StringBuilder lockID = new StringBuilder();
            StringBuilder makeDate = new StringBuilder();
            StringBuilder endDate = new StringBuilder();
            StringBuilder owner = new StringBuilder();

            StringBuilder cardID = new StringBuilder();
            int ret = InteropHelper.KReadKeycardUID(cardID);

            if (ret != 0)
            {
                return;
            }


            ret = InteropHelper.KReadKeycard(txtUserName.Text, txtPassword.Text, keyID, lockID, owner,
                makeDate, endDate);
            if (ret != 0)
            {
                return;
            }

       
            uint nCardID = UInt32.Parse(cardID.ToString());
            UInt32 revertCardID = (((nCardID >> 24) & 0xFF) << 0) | (((nCardID >> 16) & 0xFF) << 8) | (((nCardID >> 8) & 0xFF) << 16) | (((nCardID >> 0) & 0xFF) << 24);

            String keyContent = revertCardID.ToString().PadLeft(10, '0') + "," + owner + "," + Environment.NewLine;

            File.AppendAllText("./carddata.csv", keyContent);

            InteropHelper.KFireBeepSuccess();
        }
    }
}
