using LSFE.MobApp.Entities.Doctor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace LSFE.MobApp.ViewModels.Coverage
{
    public class ViewDoctorVM : INotifyPropertyChanged
    {
        private readonly Doctor _doctor;

        public ViewDoctorVM(Doctor doctor)
        {
            _doctor = doctor;

            CloseCommand = new Command(async () => await Close());
            CopyEmailCommand = new Command(async () => await CopyEmail());
            CallPhoneCommand = new Command(async () => await CallPhone());
        }

        public string FullName => $"Dr. {_doctor.FirstName} {_doctor.MiddleInitial} {_doctor.LastName}".Replace("  ", " ").Trim();
        public string EmailAddress => _doctor.EmailAddress ?? "N/A";
        public string PhoneNo => _doctor.PhoneNo ?? "N/A";
        public string LicenseNo => _doctor.LicenseNo ?? "N/A";
        public string Specialization => _doctor.SpecializationName ?? "General Practice";
        public string InstitutionName => _doctor.InstitutionName ?? "No Institution Assigned";
        public string Address => _doctor.Address ?? "No address provided";
        public string MaxVisit => $"{_doctor.MaxVisit} doctors";
        public string BirthDateFormatted => _doctor.BirthDate.ToString("MMMM dd, yyyy");
        public string CreatedDateFormatted => _doctor.CreatedDate.ToString("MMMM dd, yyyy");

        public bool HasInstitution => !string.IsNullOrEmpty(_doctor.InstitutionName);
        public bool HasAddress => !string.IsNullOrEmpty(_doctor.Address);

        public string StatusText
        {
            get
            {
                return _doctor.Status switch
                {
                    0 => "Pending",
                    1 => "Approved",
                    2 => "For DSM Approval",
                    3 => "Inactive",
                    _ => "Unknown"
                };
            }
        }

        public Color StatusBackgroundColor
        {
            get
            {
                return _doctor.Status switch
                {
                    1 => Color.FromArgb("#D1FAE5"), // Approved - Green
                    2 => Color.FromArgb("#FEF3C7"), // For DSM Approval - Yellow
                    0 => Color.FromArgb("#E0E7FF"), // Pending - Blue
                    3 => Color.FromArgb("#FEE2E2"), // Inactive - Red
                    _ => Color.FromArgb("#F3F4F6")
                };
            }
        }

        public Color StatusTextColor
        {
            get
            {
                return _doctor.Status switch
                {
                    1 => Color.FromArgb("#065F46"), // Approved - Dark Green
                    2 => Color.FromArgb("#92400E"), // For DSM Approval - Dark Yellow
                    0 => Color.FromArgb("#3730A3"), // Pending - Dark Blue
                    3 => Color.FromArgb("#991B1B"), // Inactive - Dark Red
                    _ => Color.FromArgb("#4B5563")
                };
            }
        }

        public ICommand CloseCommand { get; }
        public ICommand CopyEmailCommand { get; }
        public ICommand CallPhoneCommand { get; }

        private async Task Close()
        {
            await Application.Current.MainPage.Navigation.PopAsync();
        }

        private async Task CopyEmail()
        {
            if (!string.IsNullOrEmpty(_doctor.EmailAddress))
            {
                await Clipboard.SetTextAsync(_doctor.EmailAddress);
                await Application.Current.MainPage.DisplayAlert("Copied", "Email address copied to clipboard", "OK");
            }
        }

        private async Task CallPhone()
        {
            if (!string.IsNullOrEmpty(_doctor.PhoneNo))
            {
                try
                {
                    if (PhoneDialer.Default.IsSupported)
                    {
                        PhoneDialer.Default.Open(_doctor.PhoneNo);
                    }
                    else
                    {
                        await Clipboard.SetTextAsync(_doctor.PhoneNo);
                        await Application.Current.MainPage.DisplayAlert("Phone Number", "Phone number copied to clipboard", "OK");
                    }
                }
                catch (Exception ex)
                {
                    await Application.Current.MainPage.DisplayAlert("Error", $"Unable to dial: {ex.Message}", "OK");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
