using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
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>
    /// ReviewWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class ReviewWindow : Window, INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
        public readonly LoginManager LoginManager = Application.Current.Resources["loginManager"] as LoginManager;
        private string userId;

        private string nickName = "";
        public string Nickname {
            get => nickName;
            set { nickName = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Nickname")); }
        }

        private string reviewText = "";
        public string ReviewText {
            get => reviewText;
            set { reviewText = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ReviewText")); }
        }

        private bool likeButtonEnabled = true;
        public bool LikeButtonEnabled {
            get => likeButtonEnabled;
            set { likeButtonEnabled = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("LikeButtonEnabled")); }
        }

        private bool dislikeButtonEnabled = true;
        public bool DislikeButtonEnabled {
            get => dislikeButtonEnabled;
            set { dislikeButtonEnabled = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DislikeButtonEnabled")); }
        }

        public ReviewWindow(string userId) {
            this.userId = userId;
            InitializeComponent();
        }

        private void Like_Click(object sender, RoutedEventArgs e) {
            LikeButtonEnabled = false;
            DislikeButtonEnabled = true;
        }

        private void Dislike_Click(object sender, RoutedEventArgs e) {
            LikeButtonEnabled = true;
            DislikeButtonEnabled = false;
        }

        private async void Submit_Click(object sender, RoutedEventArgs e) {
            Nickname = Nickname.Trim();
            ReviewText = ReviewText.Trim();
            if (LikeButtonEnabled && DislikeButtonEnabled) {
                new AlertWindow("실패", "좋아요 또는 싫어요 중 선택해주세요.").ShowDialog();
                return;
            }
            if (ReviewText.Length < 10) {
                new AlertWindow("실패", "평가 내용을 10자 이상 입력해주세요.").ShowDialog();
                return;
            }
            
            SubmitButton.IsEnabled = CloseButton.IsEnabled = false;
            await LoginManager.WriteReview(userId, string.IsNullOrEmpty(Nickname) ? "익명" : Nickname, LikeButtonEnabled ? -1 : 1, ReviewText);
            SubmitButton.IsEnabled = CloseButton.IsEnabled = true;
            DialogResult = true;
            Close();
        }

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