using BoredCareers.Controllers.Payment;
using BoredCareers.Services.DatabaseService;

namespace BoredCareers.Controllers {

    public class StripeIntent : IPayment {

        DatabaseService _databaseService;

        public StripeIntent(DatabaseService databaseService) {
            _databaseService = databaseService;
        }

        public void ValidatePurchase(string WebHookData, string Headers) {
            Stripe.Event e = Stripe.EventUtility.ConstructEvent( WebHookData, Headers, IPayment._EndpointSecret );
            if (e.Type == "payment_intent.succeeded") {
                // Extract Data from payment confirm
                Stripe.PaymentIntent intent = (Stripe.PaymentIntent)e.Data.Object;
                string orderNumber = "";
                int userID = 0;
                List<int> productIDs = new List<int>();
                int subtotal = 0;
                int total = 0;

                KeyValuePair<string, string>[] y = intent.Metadata.ToArray();
                foreach (KeyValuePair<string, string> cur in y) {
                    string val = cur.Key;
                    if (val == "ordernumber") {
                        orderNumber = cur.Value;
                    }
                    else if (val == "user") {
                        userID = int.Parse(cur.Value);
                    }
                    else if (val == "products") {
                        string[] products = cur.Value.Split(',');
                        foreach (string product in products) {
                            if (!string.IsNullOrEmpty(product)) {
                                productIDs.Add(Convert.ToInt32(product));
                            }
                        }
                    }
                    else if (val == "subtotal") {
                        subtotal = int.Parse(cur.Value);
                    }
                    else if (val == "total") {
                        total = int.Parse(cur.Value);
                    }
                }

                // Add data to misox receipt
            } else {
                Console.WriteLine("Unhandled event type: {0}", e.Type);
            }
        }
    }

}