using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PlantDemo
{
    public class ZombieShower : EntityShower
    {
        static string[] bmpPaths = { "images/zomb.png", "images/zombWire.png" };
        static BitmapImage[] bmps;
        public static BitmapImage[] Bitmaps
        {
            get { return bmps; }
        }

        static ZombieShower()
        {
            bmps = new BitmapImage[bmpPaths.Length];
            for (int i = 0; i < bmpPaths.Length; i++)
                bmps[i] = new BitmapImage(new Uri(bmpPaths[i], UriKind.Relative));
        }

        protected Rectangle rectBlood;
        BitmapImage bmp;
        protected Image image;
        protected FormatConvertedBitmap gbmp;
        public FormatConvertedBitmap GrayBitmap
        {
            get
            {
                if (gbmp == null)
                {
                    gbmp = new FormatConvertedBitmap();
                    gbmp.BeginInit();
                    gbmp.Source = bmp;
                    gbmp.DestinationFormat = PixelFormats.Gray8;
                    gbmp.EndInit();
                }
                return gbmp;
            }
        }

        public Zombie Zombie
        {
            get { return Entity as Zombie; }
        }

        public ZombieShower(Zombie zomb)
            : base(zomb)
        {
            StackPanel spanel = new StackPanel();
            spanel.HorizontalAlignment = HorizontalAlignment.Left;
            rectBlood = new Rectangle() { Width = Zombie.Width * zomb.HeathPoint / 100, Height = 8, Stroke = Brushes.Black, Fill = Brushes.Red };
            spanel.Children.Add(rectBlood);
            bmp = bmps[0];
            image = new Image { Source = bmp, Width = zomb.Width, Height = zomb.Height };
            spanel.Children.Add(image);
            Shower = spanel;
            Canvas.SetLeft(Shower, zomb.X);
            Canvas.SetTop(Shower, zomb.Y);
            zomb.OnFrozen += Zomb_OnFrozen;
        }

        public ZombieShower(Zombie zomb, string bmpPath)
            : base(zomb)
        {
            int k = Array.IndexOf(bmpPaths, bmpPath);
            if (k >= 0)
                bmp = bmps[k];
            else
                bmp = new BitmapImage(new Uri(bmpPath, UriKind.Relative));
            image = new Image() { Source = bmp, Width = zomb.Width, Height = zomb.Height };
            StackPanel spanel = new StackPanel();
            spanel.HorizontalAlignment = HorizontalAlignment.Left;
            rectBlood = new Rectangle() { Width = zomb.Width * zomb.HeathPoint / 100, Height = 8, Stroke = Brushes.Black, Fill = Brushes.Red };
            spanel.Children.Add(rectBlood);
            spanel.Children.Add(image);
            Shower = spanel;
            zomb.OnFrozen += Zomb_OnFrozen;
            zomb.OnUnfrozen += Zomb_OnUnfrozen; ;
        }

        private void Zomb_OnFrozen(object sender, EventArgs e)
        {
            image.Source = GrayBitmap;
        }

        private void Zomb_OnUnfrozen(object sender, EventArgs e)
        {
            image.Source = bmp;
        }

        public override void UpdateShow()
        {
            base.UpdateShow();
            rectBlood.Width = Entity.Width * Zombie.HeathPoint / 100;
        }
    }

    public class WireZombieShower : ZombieShower
    {
        public WireZombieShower(WireZombie zomb)
            : base(zomb, "images/zombWire.png")
        { }
    }
}
