using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using TodoXaml.Models;

namespace TodoXaml.Views;

public sealed partial class MainPage : Page, INotifyPropertyChanged
{
    public ObservableCollection<TodoItem> Todos { get; set; } = new()
    {
        new TodoItem()
        {
            Id = 3,
            Title = "Add Neptun code to neptun.txt",
            Description = "CCCCCC",
            Priority = Priority.Normal,
            IsDone = false,
            Deadline = new DateTime(2024, 11, 08)
        },
        new TodoItem()
        {
            Id = 1,
            Title = "Buy milk",
            Description = "Should be lactose and gluten free!",
            Priority = Priority.Low,
            IsDone = true,
            Deadline = DateTimeOffset.Now + TimeSpan.FromDays(1)
        },
        new TodoItem()
        {
            Id = 2,
            Title = "Do the Computer Graphics homework",
            Description = "Ray tracing, make it shiny and gleamy! :)",
            Priority = Priority.High,
            IsDone = false,
            Deadline = new DateTime(2024, 11, 08)
        },
    };
    public MainPage()
    {
        InitializeComponent();
    }

    private bool _isFormVisible;
    public bool IsFormVisible{
        get => _isFormVisible;
        set
        {
            _isFormVisible = value;
            OnPropertyChanged();
        }
    }

    public TodoItem NewItem { get; set; } = new TodoItem();

    private void OnAddClicked(object sender, RoutedEventArgs e)
    {
        TodoListView.SelectedItem = null;
        NewItem = new TodoItem();
        IsFormVisible = true;
    }

    private void OnSaveClicked(object sender, RoutedEventArgs e)
    {
        Todos.Add(NewItem);
        IsFormVisible = false;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}
