using GlobalPayments.Api.Terminals;
using GlobalPayments.Api.Terminals.Ingenico;
using GlobalPayments.Api.Terminals.Ingenico.Requests;
using GlobalPaymentsTestApp.Entities.Response;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GlobalPaymentsTestApp.Control {
    public partial class XMLResponseControl : UserControl {

        private PATRequest _request;
        private bool _isFileExplorerOpen;

        public XMLResponseControl(PATRequest request) {
            InitializeComponent();

            _request = request;
            _isFileExplorerOpen = false;

            SetFieldValues();
        }

        private void SetFieldValues() {
            string ip = ((IPEndPoint)_request.Client.Client.RemoteEndPoint).Address.ToString();
            string message = string.Empty;

            switch(_request.RequestType) {
                case PATRequestType.TableReceipt: message = "The terminal request a Receipt Message\r\n XML response"; break;
                case PATRequestType.TableList: message = "The terminal request a Table List\r\n XML response"; break;
                case PATRequestType.AdditionalMessage: message = "The terminal request a Additional Message\r\n XML response"; break;
                default: message = "Invalid Request Type"; break;
            }

            Invoker.SetLabel(lblRequestType, _request.RequestType.ToString());
            Invoker.SetLabel(lblIPAddress, ip);
            Invoker.SetLabel(lblMessage, message);

            Invoker.SetTextBox(txtWaiterId, _request.WaiterId);
            Invoker.SetTextBox(txtTableId, _request.TableId);
            Invoker.SetTextBox(txtTerminalId, _request.TerminalId);
            Invoker.SetTextBox(txtTerminalCurrency, _request.TerminalCurrency);
        }

        public PATResponse GetPATResponse() {
            PATResponse response = new PATResponse();

            if (txtXMLPath.Text.Equals(""))
                throw new Exception("Please select an XML file.");

            response.XMLPath = txtXMLPath.Text;

            return response;
        }

        private void btnBrowse_Click(object sender, EventArgs e) {
            try {
                if (!_isFileExplorerOpen) {
                    Thread t = new Thread(() => {
                        _isFileExplorerOpen = true;
                        string fileLocation = string.Empty;
                        OpenFileDialog openFile = new OpenFileDialog();
                        openFile.RestoreDirectory = true;

                        if (openFile.ShowDialog() == DialogResult.OK) {
                            fileLocation = openFile.FileName;

                            Invoker.SetTextBox(txtXMLPath, openFile.FileName);
                        }
                        _isFileExplorerOpen = false;
                    });

                    t.SetApartmentState(ApartmentState.STA);
                    t.Start();
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Warning.");
            }
        }
    }
}
