using System;
using System.Collections.Generic;
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.Navigation;
using System.Windows.Shapes;
using System.Drawing;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void LoadImageButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp|All Files|*.*";

            if (openFileDialog.ShowDialog() == true)
            {
                string imagePath = openFileDialog.FileName;

                try
                {
                    BitmapImage image = new BitmapImage(new Uri(imagePath));
                    LoadedImage.Source = image;

                    //LoadedImageTextBlock.Visibility = Visibility.Hidden;
                    GenerateHistogram(image);
                }
                catch (Exception)
                {
                    //LoadedImageTextBlock.Visibility = Visibility.Visible;
                    LoadedImage.Source = null;
                }
            }
        }

        private void GenerateHistogram(BitmapImage image)
        {
            BitmapSource bitmapSource = BitmapFrame.Create(image);

            int[] redHistogram = new int[256];
            int[] greenHistogram = new int[256];
            int[] blueHistogram = new int[256];

            int stride = (bitmapSource.PixelWidth * bitmapSource.Format.BitsPerPixel + 7) / 8;
            int dataSize = stride * bitmapSource.PixelHeight;
            byte[] pixels = new byte[dataSize];
            bitmapSource.CopyPixels(pixels, stride, 0);

            for (int i = 0; i < pixels.Length; i += 4)
            {
                byte blue = pixels[i];
                byte green = pixels[i + 1];
                byte red = pixels[i + 2];

                blueHistogram[blue]++;
                greenHistogram[green]++;
                redHistogram[red]++;
            }

            DrawHistogram(redHistogram, Colors.Red, RedHistogramCanvas);
            DrawHistogram(greenHistogram, Colors.Green, GreenHistogramCanvas);
            DrawHistogram(blueHistogram, Colors.Blue, BlueHistogramCanvas);
        }

        private void DrawHistogram(int[] histogramData, Color color, System.Windows.Controls.Canvas canvas)
        {
            canvas.Children.Clear();

            int maxCount = histogramData.Max();

            for (int i = 0; i < histogramData.Length; i++)
            {
                double barHeight = (double)histogramData[i] / maxCount * canvas.ActualHeight;
                Rectangle rect = new Rectangle()
                {
                    Width = 5,
                    MinHeight = barHeight,
                    Fill = new SolidColorBrush(color),
                    Margin = new Thickness(i * 6, canvas.ActualHeight - barHeight, 0, 0)
                };
                canvas.Children.Add(rect);
            }
        }
    }
}