/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ManicMiner;

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.io.*;

/**
 *
 * @author supervisor
 */
public class Nivel {

    Personaje pj;
    IMonstruo [] monstruos;
    TiledLayer t;
    TiledLayer tColision;


    int nivel;

    int llaves;
    int mecanismos;
    FabricaNivel fb;

    LayerManager lm;


    public static final int NIVELES = 6;
    
    private int viewPortx;
    private int viewPorty;

    private boolean recolocar;


    //TILES DE ANIMACION
    private int tileAnimCinta;
    private int tileAnimLlave;

    private int []seqAnimCinta;
    private int []seqAnimLlave;

    private int contCinta;
    private int contLlave;

   private Sprite puerta;


   //Para la barra
   private int barra; //Se mide en porcentaje pero multiplicado por 100
   private int ciclos;
   private int ciclosPuerta;
   
   int puntos;

    //CONSTRUCTOR
    public Nivel(){

        ciclosPuerta=0;
        
       // fb = new FabricaNivel(new RemakeManicMiner());
        this.lm = new LayerManager();
        //pj = fb.fabricaPersonaje();
        //setNivel(1);

        fb = new FabricaNivel();

        
        viewPortx=0;
        viewPorty=0;
        recolocar=false;

        puntos = 0;
    }

    int getNivel(){
        return nivel;
    }
    
    void siguienteNivel(MinerCanvas mc){
        setNivel(nivel+1,mc);
    }

    void setNivel(int nivel,MinerCanvas mc){

        fb.setGameDesign(new RemakeManicMiner());
        lm = new LayerManager();


        pj = fb.fabricaPersonaje(nivel);
        pj.setSueloCaible(34);
        this.nivel=nivel;
        llaves = fb.getLlaves(nivel);
        t = fb.fabricaTile(nivel);
        tColision = fb.fabricaTileColision(nivel);
        puerta = fb.fabricaPuerta(nivel);
        puerta.setFrame(0);
        monstruos = fb.fabricaMonstruos(nivel);
         
        /*
        for(int i = 0; i < monstruos.length;i++){
            monstruos[i].reiniciar();
        }*/
        contCinta=0;
        contLlave=0;

        viewPortx=0;
        viewPorty=-(mc.getHeight() - t.getHeight())/2;


        recolocar=false;
        barra=100;
        ciclos = 0;

        //Actualizar estado del personaje
        pj.setTotalLlaves(llaves);

        this.tileAnimCinta = fb.fabricaTileAnimCinta(nivel);
        this.tileAnimLlave = fb.fabricaTileAnimLlave(nivel);

        this.seqAnimCinta = fb.fabricaCintaSeq(nivel);
        this.seqAnimLlave = fb.fabricaLlaveSeq(nivel);

        fb.updaterNivel(lm, nivel);
        pj.reiniciar();
    }

    public TiledLayer getTiles(){
        return t;
    }

    public TiledLayer getTilesColision(){
        return tColision;
    }

    void pintar(Graphics g,MinerCanvas mc){
        
        lm.paint(g, 0, 0);

        if(nivel>0){
        pintarBarra(g,mc);
        pintarMarcadores(g,mc);
        }
    }

    public void pintarBarra(Graphics g,MinerCanvas mc){

        int x1,y1,altura1,altura2;
        
        int alturaBarra;

        if(mc.getHeight() <= t.getHeight()){ //Si la pantalla es menor o igual que el mapa
            y1 = (mc.getHeight()*8)/100;

            altura1 = (mc.getHeight()*86)/100;
        }else{ //Si el mapa es menor que la pantalla
            y1 = (t.getHeight()*8)/100 - viewPorty;

            altura1 = (t.getHeight()*86)/100;
        }

        
        x1 = (mc.getWidth()*4)/100;
        altura2 = altura1 - 4;

        //g.setColor(0, 111, 158);
        g.setColor(0, 0, 150);
        g.fillRect(x1, y1, 3, altura1);

        if(barra >= 75){
            g.setColor(255,255,0);
        }else if (barra >25){
           g.setColor(233,93,15);
        }else{
           g.setColor(255,0,0);
        }

        alturaBarra = (altura2*barra)/100;
        g.fillRect(x1+1, y1+2+altura2-alturaBarra, 1, alturaBarra );


    }

    public void actualizarBarra(){
        
        ciclos++;

        if(ciclos%24 == 0){
            barra--;
            ciclos = 0;
        }

    }

    public void animarNivelPasado(Graphics g, MinerCanvas mc){

        while(barra > 0)
        {


              barra--;
              puntos = puntos + 18;

              pintarBarra(g,mc);
              pintarMarcadores(g,mc);
                
              mc.flushGraphics(0,0,mc.getWidth(),mc.getHeight());
              try{
                Thread.sleep(62);
             }catch (InterruptedException e){
                 System.out.println(e);
            }
        }
        
        
    }

  
    
    public void setCiclos(int ciclos){
        this.ciclos = ciclos;
    }
    
    public void pintarMarcadores(Graphics g, MinerCanvas mc){

        int x1 = 2;
        int y1 = 3;

        int y2 = y1+2;

        int x2 = x1+2;

        int xbloque2 = 0;
        
        Image fmarc;
        try{
            //Imagen del fondo del marcador
            fmarc = Image.createImage("/marcador.png");
            g.drawImage(fmarc, x1, y1, Graphics.LEFT|Graphics.TOP);

            xbloque2 = mc.getWidth()-fmarc.getWidth()-x1;

            g.drawImage(fmarc, xbloque2, y1, Graphics.LEFT | Graphics.TOP);
        }catch(IOException e){
            System.out.println("No se puede cargar la imagen: "+e);
        }

        g.setColor(255,255,255);
        //Dibujo las vidas:
        g.drawString("vidas:"+this.pj.getVidas(), x2, y2, Graphics.LEFT | Graphics.TOP);

        //dibujo puntos
        g.drawString("puntos:"+puntos, xbloque2, y2, Graphics.LEFT | Graphics.TOP);

    }
    public void sumarPuntos(int puntos){
        this.puntos = this.puntos + puntos;
    }

    public void animarMonstruos(){

        for(int i=0; i< this.monstruos.length; i++){
            monstruos[i].mover(t);

        }

    }

    public void animarTiles(){

        t.setAnimatedTile(tileAnimCinta, this.seqAnimCinta[contCinta]);

        t.setAnimatedTile(tileAnimLlave, this.seqAnimLlave[contLlave]);
       
        if(llaves == pj.getLLaves())
            animarPuerta();

        contCinta = (contCinta+1)%seqAnimCinta.length;
        contLlave = (contLlave+1)%seqAnimLlave.length;

    }

    public void animarPuerta(){
        int  div = 10;
        if(ciclosPuerta%5==0){
            if(puerta.getFrame() < puerta.getFrameSequenceLength()-1)
             puerta.nextFrame();
            else
            puerta.setFrame(3);
         
        }
        ciclosPuerta++;

        if(ciclosPuerta>10*div*ciclosPuerta)
        {
            ciclosPuerta=0;
        }

    }

    Personaje getPersonaje(){
      
           return  pj;
      
    }

    IMonstruo [] getMonstruos(){
        return monstruos;
    }
    void hacerScroll(GameCanvas g){


        int x1 = g.getWidth()/4; 
        int x2 = ((3*g.getWidth())/4);
        
        int y1 = g.getHeight()/4;
        int y2 = (3*g.getHeight())/4;

        if(!recolocar){
             if(pj.getPosx()<x1+viewPortx && viewPortx> 0){
                recolocar = true;
            }

            if(pj.getPosx()+ pj.getWidth()>x2 + viewPortx && viewPortx + g.getWidth() < t.getWidth() + t.getX()){
                recolocar = true;
            }

             if(pj.getPosy()< y1+viewPorty && viewPorty > 0){
                 recolocar = true;
             }

             if(pj.getPosy()+pj.getHeigth() > y2 + viewPorty && viewPorty + g.getHeight() < t.getHeight() + t.getY()){
                 recolocar = true;
             }
        }else{
            int x3 = g.getWidth()/2;
            int y3 = g.getWidth()/2;

            if(pj.getPosx() + pj.getWidth() < x3 +viewPortx && viewPortx>0)
                viewPortx= viewPortx-4;
            else if(pj.getPosx() > x3 + viewPortx && viewPortx + g.getWidth() < t.getWidth() + t.getX()){
                viewPortx = viewPortx+4;

            }
            else if(pj.getPosy()+ pj.getHeigth() < y3 + viewPorty && viewPorty >0)
                viewPorty = viewPorty-4;
            else if(pj.getPosy() > y3 + viewPortx && viewPorty + g.getHeight() < t.getHeight() + t.getY())
                viewPorty = viewPorty+4;
            else{
                recolocar=false;
            }
        }




        lm.setViewWindow(viewPortx, viewPorty, g.getWidth(), g.getHeight());
    }


    public void comprobarBarra(){
        if(barra == 0){
            this.pj.morirse();
        }
    }
}
