using System;
using AgkSharp;
using AGKCore;

namespace ImageTest0
{

    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Core.CreateWin32Window("AGK-Title", 640, 480, false);
            if (!Core.InitAGK())
                return;

            Agk.SetVirtualResolution(320, 480);
            Agk.SetRawWritePath(Agk.GetReadPath());
            uint size = (uint)AgkImage.GetMaxTextureSize();

            AgkText Title = new AgkText("Image Test 0", 24.0f);
            //Title.Size = 24.0f;
            //Title.Label = "Image Test 0";
            Title.Alignment = EAlignment.Center;
            Title.Position = new AGKVector2(160, 10);

            // create background sprite
            AgkSprite back = new AgkSprite("media/background.jpg");

            // create/load first image
            AgkImage img0 = new AgkImage("media/item0.png");

            // get image dimensions
            int width = img0.Width;     // img0.GetWidth();
            int height = img0.Height;   // img0.GetHeight();

            // get image pixel data
            img0.GetRawData(out byte[] img0Data);
            int[] img1Data = new int[img0Data.Length / 4];

            // make outline image from image
            for (uint i = 0; i < img0Data.Length; i += 4)
            {
                int alpha = img0Data[i + 3];

                // set pixel only if the alpha value of current pixel equal 255
                if (alpha == 255)
                {
                    // get pixel position around the current pixel
                    int aUp = (int)i - (width * 4);
                    int aDown = (int)i + (width * 4);
                    int aRight = (int)i + 4;
                    int aLeft = (int)i - 4;

                    // note image boundary
                    if (aUp < 0 || aDown > img0Data.Length || (i % (width*4)) == 0 || (i % (width*4)) == ((width-1)*4))
                    {
                        img1Data[i / 4] = (255 << 24) | (255 << 16) | (0 << 8) | 0;
                        continue;
                    }

                    // calculate the average alpha value of all surrounding pixels
                    alpha = (alpha + img0Data[aUp + 3] + img0Data[aDown + 3] + img0Data[aRight + 3] + img0Data[aLeft + 3]) / 5;

                    // set pixel to blue if the average value below 255
                    if (alpha < 255)
                        img1Data[i / 4] = (255 << 24) | (255 << 16) | (0 << 8) | 0;
                }
            }

            // create a new image from raw data
            AgkImage img1 = new AgkImage((uint)width, (uint)height, img1Data);

            // save the new created image
            img1.Save("media/outline.png");

            // create sprites from original image and created image
            AgkSprite sprite0 = new AgkSprite(img0);
            AgkSprite sprite1 = new AgkSprite(img1);

            while (Core.LoopAGK())
            {
                // draw background
                back.Draw();

                // set position of both sprites and draw sprites
                sprite0.SetPosition(0, 50);
                sprite1.SetPosition(0, 50);
                sprite0.Draw();
                sprite1.Draw();

                // set new position of both sprites and draw sprites again
                sprite0.SetPosition(64, 50);
                sprite1.SetPosition(128, 50);
                sprite0.Draw();
                sprite1.Draw();

                Title.Draw();

                Agk.Sync();
            }

            Core.CleanUp();
        }
    }
}
