#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>

#include <iostream>
#include <ctime>

#include "Case.h"
#include "Level.h"
#include "Kraken.h"
#include "Hero.h"

#define TILE_WIDTH 30
#define TILE_HEIGHT 30
#define ASSETS_PATH "../assets/"
using namespace sf;



void funct(std::string name)
{
   SoundBuffer Ssound;
   if(!Ssound.loadFromFile(name.c_str())){std::cout<<"bug music (loading)"<<std::endl;};
   Sound sound(Ssound);
   sound.play();
   while(sound.getStatus()==Sound::Playing){}
}




int main()
{

    RenderWindow window(VideoMode::getDesktopMode(),"Kraken's Game");

    srand(time(NULL));



    Thread bearRoaring(&funct,ASSETS_PATH "BearRoaring.wav");
    Thread bubble(&funct,ASSETS_PATH  "bubbleunderwater.wav");
    Thread explosion(&funct,ASSETS_PATH "Explosion2.wav");
    Thread scream(&funct,ASSETS_PATH "screamingMan.wav");

    Case theCase(&window);
    theCase.Settexture(ASSETS_PATH "tilesetNew.png");

    Level theLevel(&theCase,int(VideoMode::getDesktopMode().width/TILE_HEIGHT), int(VideoMode::getDesktopMode().height/TILE_HEIGHT));
    theLevel.create();

    Kraken theBeast(&theLevel,int(VideoMode::getDesktopMode().width/TILE_HEIGHT), int(VideoMode::getDesktopMode().height/TILE_HEIGHT),2);
    theBeast.Settexture(ASSETS_PATH "tilesetNew2.png");

    Hero Josh(&theBeast,70,70);
    theLevel.setCoordCase(int(70/TILE_WIDTH),int(70/TILE_HEIGHT),'3');
    Josh.Settexture(ASSETS_PATH "tilesetNew2.png");

        View beastView;
    beastView.setCenter(theBeast.getposition().x+TILE_WIDTH*2,theBeast.getposition().y+TILE_HEIGHT);
    beastView.setSize(2*TILE_WIDTH,3*TILE_HEIGHT);
    beastView.setViewport(FloatRect(0.9f,0,0.2f,0.25f));
    beastView.zoom(2.0f);

        View joshView;
    joshView.setCenter(Josh.GetpozX(),Josh.GetpozY());
    joshView.setSize(10*TILE_WIDTH,10*TILE_HEIGHT);


    bool attack(false);
   // joshView.setViewport(FloatRect(0, 0, 0.8f, 1));


/*        RectangleShape rect;
    rect.setSize(Vector2f(200,200));
    rect.setPosition(Vector2f(beastView.getCenter().x-beastView.getSize().x/2,beastView.getCenter().y-beastView.getSize().y));
    rect.setFillColor(Color::Black); */
//




    while (window.isOpen())
    {

        Event event;
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case (Event::Closed): window.close();
                case Event::KeyReleased:
                    {
                        if (event.key.code==Keyboard::Up || event.key.code==Keyboard::Down)     {Josh.SetspeedY(0);}
                        if (event.key.code==Keyboard::Right || event.key.code==Keyboard::Left)  {Josh.SetspeedX(0);}
                        //if (event.key.code==Keyboard::Space)    {attack=true;}
                    }
                case Event::KeyPressed:
                        {
                            if(event.key.code==Keyboard::Space){attack=true; }
                        }

	      default: break;
            }
       // if(Keyboard::isKeyPressed(Keyboard::Add)){}
        if(Keyboard::isKeyPressed(Keyboard::Up))    {Josh.SetspeedY(-0.2);}
        if(Keyboard::isKeyPressed(Keyboard::Down))  {Josh.SetspeedY(0.2);}
        if(Keyboard::isKeyPressed(Keyboard::Right)) {Josh.SetspeedX(0.2);}
        if(Keyboard::isKeyPressed(Keyboard::Left))  {Josh.SetspeedX(-0.2);}
        if(Keyboard::isKeyPressed(Keyboard::Space)) {attack=false;}



       // for(int j(0);j<1000;j+=0.1){}


        }




if (attack)
{
    //scream.launch();
    Josh.restartClock();Josh.Setattacking(true); attack=false;
}
/*
///SOUND
if (Josh.isDead()){explosion.launch();}
if  (theBeast.isAttacking())    {bearRoaring.launch();}
if(theLevel.getCoord(int((Josh.GetpozX()+TILE_WIDTH/2)/TILE_WIDTH),int((Josh.GetpozY()+TILE_HEIGHT/2)/TILE_HEIGHT))=='4') {bubble.launch();}
*/
    window.clear(Color(0,100,250));
    theBeast.setCoord(Josh.GetpozX(),Josh.GetpozY());

///

///Big map
        joshView.setCenter(Josh.GetpozX(),Josh.GetpozY());
       window.setView(joshView);

         theLevel.draw();

        Josh.draw();
        Josh.update();
        theBeast.update();



///LITTLE MAP
        window.setView(beastView);
        beastView.setCenter(theBeast.getposition().x+TILE_WIDTH*2,theBeast.getposition().y+TILE_HEIGHT);
        theLevel.draw();
        Josh.draw();
        Josh.update();
        theBeast.update();
///
    window.display();
    }

    return 0;
}
