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

namespace WabApiTest
{
    public partial class Form1 : Form
    {
        CPmsApi _PmsApi = new CPmsApi();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _PmsApi.m_URL = tbUrl.Text.ToString();
            cbInterface.SelectedIndex = 0;

            tbPosArrLength.MaxLength = 5;
            tbPriority.MaxLength = 1;
            tbCarrierType.MaxLength = 5;
            tbCarrier.MaxLength = 5;

            tbOrderID.MaxLength = 50;
            tbParentOrderID.MaxLength = 50;
            tbPosArr.MaxLength = 500;
            tbOtherOrderData.MaxLength = 1000;

        }

        private void btTest_Click(object sender, EventArgs e)
        {
            tbCallTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            string strInterface = cbInterface.Text.ToString();
            switch (strInterface)
            {
                case "OrderAdd":
                    OrderAdd();
                    break;
                case "MidOrderAdd":
                    MidOrderAdd();
                    break;
                case "OrderCancel":
                    OrderCancel();
                    break;
                case "GetOrder":
                    GetOrder();
                    break;
                case "GetCurrentOrder":
                    GetCurrentOrder();
                    break;
                case "GetCarrier":
                    GetCarrier();
                    break;
                case "GetAllCarrier":
                    GetAllCarrier();
                    break;
                default:
                    break;
            }
        }
      
        private void OrderCancel()
        {
            if (tbOrderID.Text == "")
            {
                MessageBox.Show("OrderID is Empty!");
                return;
            }
            string strRequest;
            string strResult;
            string strMsg;
            if (_PmsApi.OrderCancelSync(tbOrderID.Text.ToString(), out strRequest, out strResult, out strMsg))
            {
                MessageBox.Show(strMsg);
            }

            rtbIn.Text = strRequest;
            rtbOut.Text = strResult;
        }
        private void GetOrder()
        {
            if(tbOrderID.Text == "")
            {
                MessageBox.Show("OrderID is Empty!");
                return;
            }
            string strRequest;
            string strResult;
            string strMsg;
            if (!_PmsApi.GetOrderSync(tbOrderID.Text.ToString(), out strRequest, out strResult, out strMsg))
            {
                MessageBox.Show(strMsg);
            }

            rtbIn.Text = strRequest;
            rtbOut.Text = strResult;
        }
        private void GetCarrier()
        {
            if (tbCarrier.Text == "")
            {
                MessageBox.Show("Carrier is Empty!");
                return;
            }
            string strRequest;
            string strResult;
            string strMsg;
            if (_PmsApi.GetCarrierSync(Convert.ToInt32(tbCarrier.Text.ToString()) ,out strRequest, out strResult, out strMsg))
            {
                MessageBox.Show(strMsg);
            }

            rtbIn.Text = strRequest;
            rtbOut.Text = strResult;
        }
        private void GetAllCarrier()
        {
            string strRequest;
            string strResult;
            string strMsg;
            if (_PmsApi.GetAllCarrierSync(out strRequest, out strResult, out strMsg))
            {
                MessageBox.Show(strMsg);
            }

            rtbIn.Text = strRequest;
            rtbOut.Text = strResult;
        }
        private void GetCurrentOrder()
        {
            string strRequest;
            string strResult;
            string strMsg;
            if (_PmsApi.GetCurrentOrderSync(out strRequest, out strResult, out strMsg))
            {
                MessageBox.Show(strMsg);
            }

            rtbIn.Text = strRequest;
            rtbOut.Text = strResult;
        }

        private void MidOrderAdd()
        {
            RequestMidOrder smodel = new RequestMidOrder();
            if (tbOrderID.Text == "")
            {
                MessageBox.Show("OrderID is Empty!");
                return;
            }
            if (tbPosArr.Text == "")
            {
                MessageBox.Show("PosArr is Empty!");
                return;
            }

            smodel.OrderID = tbOrderID.Text.ToString();
            smodel.ParentOrderID = tbParentOrderID.Text.ToString();
            smodel.PosArr = tbPosArr.Text.ToString();
            smodel.OtherOrderData = tbOtherOrderData.Text.ToString();
            smodel.CallTime = tbCallTime.Text.ToString();
            smodel.Type = tbType.Text.ToString();

            int.TryParse(tbPriority.Text.ToString(), out smodel.Priority);
            int.TryParse(tbPosArrLength.Text.ToString(), out smodel.PosArrLength);
            int.TryParse(tbPriority.Text.ToString(), out smodel.Priority);

            string strRequest;
            string strResult;
            string strMsg;
            if (_PmsApi.MidOrderAddSync(smodel, out strRequest, out strResult, out strMsg))
            {
                MessageBox.Show(strMsg);
            }

            rtbIn.Text = strRequest;
            rtbOut.Text = strResult;
        }

        private void OrderAdd()
        {
            RequestOrderInfo smodel = new RequestOrderInfo();
            if (tbOrderID.Text == "")
            {
                MessageBox.Show("OrderID is Empty!");
                return;
            }
            if(tbPosArr.Text == "")
            {
                MessageBox.Show("PosArr is Empty!");
                return;
            }

            smodel.OrderID = tbOrderID.Text.ToString();
            smodel.ParentOrderID = tbParentOrderID.Text.ToString();
            smodel.PosArr = tbPosArr.Text.ToString();
            smodel.OtherOrderData = tbOtherOrderData.Text.ToString();
            smodel.CallTime = tbCallTime.Text.ToString();

            int.TryParse(tbPriority.Text.ToString(),out smodel.Priority);
            int.TryParse(tbType.Text.ToString(),out smodel.Type);
            int.TryParse(tbPosArrLength.Text.ToString(),out smodel.PosArrLength);
            int.TryParse(tbPriority.Text.ToString(),out smodel.Priority);

            string strRequest;
            string strResult;
            string strMsg;
            if (_PmsApi.OrderAddSync(smodel, out strRequest, out strResult, out strMsg))
            {
                rtbIn.Text = strRequest;
                rtbOut.Text = strResult;
            }
            else
            {
                rtbIn.Text = strRequest;
                MessageBox.Show(strMsg);
            }
        }

        private void SetEnable(string strInterface)
        {
            bool bEnable = true;

            if (strInterface != "")
            {
                btTest.Enabled = bEnable;
            }
            else
            {
                btTest.Enabled = !bEnable;
            }

            if (strInterface == "OrderAdd"
                || strInterface == "MidOrderAdd"
                || strInterface == "OrderCancel"
                || strInterface == "GetOrder"
                )
            {
                lbOrderID.Enabled = bEnable;
                tbOrderID.Enabled = bEnable;
            }
            else
            {
                lbOrderID.Enabled = !bEnable;
                tbOrderID.Enabled = !bEnable;
            }

            if (strInterface == "OrderAdd"
                || strInterface == "MidOrderAdd"
                )
            {
                lbParentOrderID.Enabled = bEnable;
                tbParentOrderID.Enabled = bEnable;

                lbPosArr.Enabled = bEnable;
                tbPosArr.Enabled = bEnable;

                lbPosArrLength.Enabled = bEnable;
                tbPosArrLength.Enabled = bEnable;

                lbCarrierType.Enabled = bEnable;
                tbCarrierType.Enabled = bEnable;

                lbPriority.Enabled = bEnable;
                tbPriority.Enabled = bEnable;

                lbOtherOrderData.Enabled = bEnable;
                tbOtherOrderData.Enabled = bEnable;

                lbCallTime.Enabled = bEnable;
                tbCallTime.Enabled = bEnable;

                lbType.Enabled = bEnable;
                tbType.Enabled = bEnable;
                tbCallTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                lbParentOrderID.Enabled = !bEnable;
                tbParentOrderID.Enabled = !bEnable;

                lbPosArr.Enabled = !bEnable;
                tbPosArr.Enabled = !bEnable;

                lbPosArrLength.Enabled = !bEnable;
                tbPosArrLength.Enabled = !bEnable;

                lbCarrierType.Enabled = !bEnable;
                tbCarrierType.Enabled = !bEnable;

                lbPriority.Enabled = !bEnable;
                tbPriority.Enabled = !bEnable;

                lbOtherOrderData.Enabled = !bEnable;
                tbOtherOrderData.Enabled = !bEnable;

                lbCallTime.Enabled = !bEnable;
                tbCallTime.Enabled = !bEnable;

                lbType.Enabled = !bEnable;
                tbType.Enabled = !bEnable;
            }

            if(strInterface == "GetCarrier")
            {
                lbCarrier.Enabled = bEnable;
                tbCarrier.Enabled = bEnable;
            }
            else
            {
                lbCarrier.Enabled = !bEnable;
                tbCarrier.Enabled = !bEnable;
            }
        }

        private void cbInterface_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strInterface = cbInterface.Text.ToString();
            SetEnable(strInterface);
        }

        private void btModifyUrl_Click(object sender, EventArgs e)
        {
            if(!tbUrl.ReadOnly)
            {
                _PmsApi.m_URL = tbUrl.Text.ToString();
                tbUrl.ReadOnly = true;
            }
            else
            {
                tbUrl.ReadOnly = false;
            }
        }

        private void tbCarrierType_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Number only");
            }
        }

        private void tbPriority_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Number only");
            }
        }

        private void tbPosArrLength_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Number only");
            }
        }

        private void tbCarrier_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Number only");
            }
        }
    }
}

