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

namespace GlobalPaymentsTestApp.Control {
    public partial class ConfirmationResponseControl : UserControl {
        private PATRequest _request;
        private TransactionOutcome _transactionOutcome;
        private decimal? _amount;
        public ConfirmationResponseControl(PATRequest request, decimal? amount) {
            InitializeComponent();

            _request = request;
            _amount = amount;

            SetFieldValues();
        }

        public PATResponse GetPATResponse() {
            PATResponse response = new PATResponse();
            decimal? amount = decimal.Parse(txtAmount.Text);

            if (txtAmount.Text.Equals(""))
                throw new Exception("Please input amount.");

            if (amount <= 0) {
                throw new Exception("Amount must be greater than zero.");
            } else if (amount >= 1000000) {
                throw new Exception("Amount cannot be exceeded from 999999.");
            }

            response.Amount = amount;
            response.CurrencyCode = txtCurrency.Text;
            response.PATPaymentMode = chkAdditionalMessage.Checked ? PATPaymentMode.USE_ADDITIONAL : PATPaymentMode.NO_ADDITIONAL;

            return response;
        }

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

            switch (_request.RequestType) {
                case PATRequestType.TableLock: 
                    message = "The terminal request a Table Lock \r\nconfirmation response";
                    Invoker.SetCheckBoxVisibility(chkAdditionalMessage, false);
                    Invoker.SetButton(btnTransOutcome, false);
                    Invoker.SetTextBox(txtAmount, true);
                    break;
                case PATRequestType.TransactionOutcome: 
                    message = "The terminal request a Transaction Outcome\r\n confirmation response";
                    Invoker.SetCheckBoxVisibility(chkAdditionalMessage, true);
                    Invoker.SetButton(btnTransOutcome, true);
                    Invoker.SetTextBox(txtAmount, false);
                    break;
                case PATRequestType.TransferData: 
                    message = "The terminal request a Transfer Data\r\n confirmation response";
                    Invoker.SetCheckBoxVisibility(chkAdditionalMessage, false);
                    Invoker.SetButton(btnTransOutcome, false);
                    Invoker.SetTextBox(txtAmount, false);
                    break;
                case PATRequestType.SplitSaleReport: 
                    message = "The terminal request a Split Sale Report\r\n confirmation response";
                    Invoker.SetCheckBoxVisibility(chkAdditionalMessage, false);
                    Invoker.SetButton(btnTransOutcome, false);
                    Invoker.SetTextBox(txtAmount, false);
                    break;
                case PATRequestType.Ticket:
                    message = "The terminal request a Ticket\r\n confirmation response";
                    Invoker.SetCheckBoxVisibility(chkAdditionalMessage, false);
                    Invoker.SetButton(btnTransOutcome, false);
                    Invoker.SetTextBox(txtAmount, false);
                    break;
                case PATRequestType.EndOfDayReport: 
                    message = "The terminal request a End of Day Report\r\n confirmation response";
                    Invoker.SetCheckBoxVisibility(chkAdditionalMessage, false);
                    Invoker.SetButton(btnTransOutcome, false);
                    Invoker.SetTextBox(txtAmount, false);
                    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);
            Invoker.SetTextBox(txtCurrency, "826");
            if (_request.RequestType.Equals(PATRequestType.TableLock))
                Invoker.SetTextBox(txtAmount, "");
            else
                Invoker.SetTextBox(txtAmount, _amount);

            if (_request.RequestType.Equals(PATRequestType.TransactionOutcome)) {
                decimal? amount = decimal.Parse(_request.TransactionOutcome.Amount) / 100;

                Invoker.SetTextBox(txtAmount, amount);
                Invoker.SetTextBox(txtCurrency, _request.TransactionOutcome.CurrencyCode);
                Invoker.SetTextBox(txtStatus, _request.TransactionOutcome.Status);
            }
        }

        private void txtAmount_KeyPress(object sender, KeyPressEventArgs e) {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.')) {
                e.Handled = true;
            } else {
                if (e.KeyChar == '.') {
                    int len = txtAmount.TextLength;
                    txtAmount.MaxLength = len + 3;
                } else if (!txtAmount.Text.Contains(".") && txtAmount.MaxLength != 8) {
                    txtAmount.MaxLength = 8;
                }
            }
        }

        private void btnTransOutcome_Click(object sender, EventArgs e) {
            _transactionOutcome = new TransactionOutcome(_request);
            _transactionOutcome.Show();
        }
    }
}
