using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace JtkPcClientWpf {
    /// <summary>
    /// OrderWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class OrderWindow : Window, INotifyPropertyChanged {
        private static readonly bool designMode = !(Application.Current is App);
        public event PropertyChangedEventHandler PropertyChanged;

        public readonly OrderManager OrderManager = Application.Current.Resources["orderManager"] as OrderManager;

        public int totalCost = designMode ? 2100 : 0;
        /// <summary>
        /// 구별을 위한 이름입니다.
        /// </summary>
        public int TotalCost {
            get => totalCost;
            private set { totalCost = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("TotalCost")); }
        }

        public OrderWindow() {
            InitializeComponent();
        }

        private void Close_Click(object sender, RoutedEventArgs e) {
            Close();
        }

        private void Category_Click(object sender, RoutedEventArgs e) {
            var btn = sender as Button;
            var cat = btn.Content.ToString();
            OrderManager.SelectCategory(cat);
        }

        private void ProductAdd_Click(object sender, RoutedEventArgs e) {
            var btn = sender as Button;
            var prod = btn.Tag as Product;
            var newItem = new OrderListItem(prod, 1);

            var orderList = new List<OrderListItem>(OrderManager.OrderList);
            foreach (var item in orderList) {
                if (item.Product == prod) {
                    item.Quantity++;
                    OrderManager.UpdateTotalCost();
                    return;
                }
            }
            orderList.Add(newItem);
            OrderManager.OrderList = orderList;
            OrderManager.UpdateTotalCost();
        }

        private void QuantityIncrease_Click(object sender, RoutedEventArgs e) {
            var btn = sender as Button;
            var orderListItem = btn.Tag as OrderListItem;
            
            if (orderListItem.Quantity < 9)
                orderListItem.Quantity++;
            OrderManager.UpdateTotalCost();
        }
        private void QuantityDecrease_Click(object sender, RoutedEventArgs e) {
            var btn = sender as Button;
            var orderListItem = btn.Tag as OrderListItem;
            
            if (orderListItem.Quantity > 1)
                orderListItem.Quantity--;
            else
                Except_Click(sender, e);
            OrderManager.UpdateTotalCost();
        }
        private void Except_Click(object sender, RoutedEventArgs e) {
            var btn = sender as Button;
            var orderListItem = btn.Tag as OrderListItem;
            
            var orderList = new List<OrderListItem>(OrderManager.OrderList);
            orderList.Remove(orderListItem);
            OrderManager.OrderList = orderList;
            OrderManager.UpdateTotalCost();
            //for (int i = 0; i < orderList.Count; i++) {
            //    if (orderList[i] == )
            //}

        }

        private void Payment1_Click(object sender, RoutedEventArgs e) {
            PaymentButton1.IsEnabled = false;
            PaymentButton2.IsEnabled = true;
            PaymentButton3.IsEnabled = true;
            OrderManager.PaymentMethod = "cash";
        }

        private void Payment2_Click(object sender, RoutedEventArgs e) {
            PaymentButton1.IsEnabled = true;
            PaymentButton2.IsEnabled = false;
            PaymentButton3.IsEnabled = true;
            OrderManager.PaymentMethod = "card";
        }

        private void Payment3_Click(object sender, RoutedEventArgs e) {
            PaymentButton1.IsEnabled = true;
            PaymentButton2.IsEnabled = true;
            PaymentButton3.IsEnabled = false;
            OrderManager.PaymentMethod = "point";
        }

        private void thisWindow_Loaded(object sender, RoutedEventArgs e) {
            OrderManager.RefreshList();
            OrderManager.SelectCategory(OrderManager.Categories.First());
        }

        private void Order_Click(object sender, RoutedEventArgs e) {
            if (OrderManager.SubmitOrder(out var err)) {
                DialogResult = true;
                Close();
            } else {
                new AlertWindow("실패", err).ShowDialog();
            }
        }
    }
}
