#include "m_logo.h"
#include "jdraw.h"
#include "jinput.h"
#include "controller.h"
#include "jaudio.h"
#include "config.h"
#include <SDL2/SDL.h>

namespace modules
{
    namespace logo
    {
        struct pixel_t
        {
            int x, y;
            float distance;
            float angle;

            float di, dd, ai, ad;
        };

        pixel_t pixels[200];
        int num_pixels = 0;
        float d = 1;
        float a = 0;
        draw::surface *anys = nullptr;

        void get_dist_angle_from_xy(int pixel);
        void calculate_coords_from_dist_angle(int pixel);

        void init()
        {
            srand(SDL_GetTicks());
            anys = draw::getSurface("25anys.gif");
            num_pixels = 0;
            draw::surface *surf = draw::getSurface("jailgames.gif");
            for (int y=0; y<surf->h; ++y)
                for (int x=0; x<surf->w; ++x)
                    if (surf->pixels[x+y*surf->w]!=0)
                    {
                        pixels[num_pixels].x = x;
                        pixels[num_pixels].y = y;
                        get_dist_angle_from_xy(num_pixels);
                        pixels[num_pixels].dd = float((rand()%5)+1)/100.0f;
                        pixels[num_pixels].ad = float((rand()%10)+1);
                        pixels[num_pixels].di = 1;
                        pixels[num_pixels].ai = 0;
                        for (int i=0; i<40; ++i)
                        {
                            pixels[num_pixels].di += pixels[num_pixels].dd;
                            pixels[num_pixels].ai += pixels[num_pixels].ad;
                        }
                        num_pixels++;
                    }

            if (config::isMusicEnabled()) audio::playSound("snd_logo.wav", SOUND_BASIC);
        }

        int steps=0;

        bool loop()
        {
            if (controller::pressed(KEY_MENU) || controller::pressed(KEY_JUMP) || controller::pressed(KEY_PICK) ) {
                return false;
            }

            draw::cls(2);
            draw::color(1);

            if (steps < 40) {
                for (int i=0; i<num_pixels; ++i)
                {
                    calculate_coords_from_dist_angle(i);
                    draw::swapcol(1, 12+(steps/5));

                    draw::fillrect(60+pixels[i].x*4, 106+pixels[i].y*4, 4*pixels[i].di, 4*pixels[i].di);
                    pixels[i].di -= pixels[i].dd;
                    pixels[i].ai -= pixels[i].ad;
                }
            } else if (steps<56) {
                draw::swapcol(1, 12+(steps-40)/2);
                draw::swapcol(2, 19-(steps-40)/2);
                for (int i=0; i<num_pixels; ++i)
                {
                    pixels[i].di = 1;
                    pixels[i].ai = 0;
                    calculate_coords_from_dist_angle(i);
                    draw::fillrect(60+pixels[i].x*4, 106+pixels[i].y*4, 4*pixels[i].di, 4*pixels[i].di);
                }
            } else if (steps<100) {
                for (int i=0; i<num_pixels; ++i)
                {
                    pixels[i].di = 1;
                    pixels[i].ai = 0;
                    calculate_coords_from_dist_angle(i);
                    draw::fillrect(60+pixels[i].x*4, 106+pixels[i].y*4, 4*pixels[i].di, 4*pixels[i].di);
                }
            } else if (steps<200) {
                draw::swapcol(1,WHITE+LIGHT);
                draw::swapcol(2,BLACK);
                for (int i=0; i<num_pixels; ++i)
                {
                    draw::fillrect(60+pixels[i].x*4, 106+pixels[i].y*4, 4*pixels[i].di, 4*pixels[i].di);
                }
                draw::setSource(anys);
                draw::draw(212, 136, 40,7, 0, 0);
            } else if (steps<216) {
                draw::swapcol(1, 19-(steps-200)/2);
                for (int i=0; i<num_pixels; ++i)
                {
                    draw::fillrect(60+pixels[i].x*4, 106+pixels[i].y*4, 4*pixels[i].di, 4*pixels[i].di);
                }
                draw::draw(212, 136, 40,7, 0, 0);
            } else if (steps>280) {
                return false;
            }

            draw::render();
            steps++;
            return true;
        }

        void get_dist_angle_from_xy(int pixel)
        {
            int x = pixels[pixel].x - 25;
            int y = 3 - pixels[pixel].y;
            pixels[pixel].distance = SDL_sqrtf(x*x + y*y);
            pixels[pixel].angle = SDL_atan2f(y, x)/(M_PI/180.0f);
        }

        void calculate_coords_from_dist_angle(int pixel)
        {
            pixels[pixel].x = 25 + SDL_roundf(SDL_cosf((pixels[pixel].angle+pixels[pixel].ai)*(M_PI/180.0f)) * (pixels[pixel].distance*pixels[pixel].di));
            pixels[pixel].y = 3 - SDL_roundf(SDL_sinf((pixels[pixel].angle+pixels[pixel].ai)*(M_PI/180.0f)) * (pixels[pixel].distance*pixels[pixel].di));
        }
    }
}