require "final"
require "audio"

local arcade_config = require("arcade_config")

o2aX = arcade_config.org2arc_escala

avatar_abad_x = 3*o2aX
avatar_abad2_x = 4*o2aX
avatar_abad_y = 0
avatar_batman_x = 4*o2aX
avatar_batman_y = 48*o2aX
avatar_premiere_x = 69*o2aX
avatar_premiere_y = 20*o2aX
avatar_elalien_x = 3*o2aX
avatar_elalien_y = 16*o2aX

scenes={
    intro_01= {
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ah!, què bo!","Nit de Netflix amb","un bon bol de cacaus!"},die=20},
    },
    intro_02= {
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Eh! On està el","bol de cacaus???",""},die=20},
    },
    intro_03= {
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",musica=audio_song_batman,text={"ME L'HE ENDUT JO!!!", "S'HAN ACABAT ELS","CACAUS PA TU!"},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Què??","Però perquè???",""},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Estàs com una vaca!","Has de deixar de menjar", "eixes gorrinades"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Mira'm a mi. Estic","tot buenorro i faig", "esport"},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Esport?????","I una merda!!","He de cuidar la panxa!"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Pos t'aguantes.","Ja pots despedir-te", "dels cacaus..."},die=20},
    },
    intro_04= {
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Serà mamó...","",""},die=20},
    },
    abad_inici={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Se va a enterar eixe","imbècil de Batman","quan el pille..."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Gonna per el GPS","per a trobarlo.",""}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"I igual hi ha","algú més per ací","que em puga ajudar."}},
    },
    abad_corfes={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Osti, està ple de","monstruacos.",""}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Pos al que s'acoste","s'endurà una corfa","de cacau al cap!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"I si van altets","igual val la pena","ajupir-se..."}}
    },
    abad_portes={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Qui ha tancat totes","les portes?",""}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"A saber on està","cada interruptor","per a obrir-les..."}},
    },
    abad_porta_casa={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Vaja, la porta de","casa també està","tancada."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Però he de eixir!","Batman viu en la","casa de davant!"}},
    },
    abad_porta_batman={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Be, era de esperar","que Batman tinguera","la porta tancada."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Crec que hi havia","un passadis secret","del meu sotan al seu."}},
    },
    gps_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ale, ja tinc el GPS.","Ara puc trobar tot","el que necessite..."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"polsant les tecles","de l'1 al 5",""}},
    },
    premiere_intro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_premiere,text={"Premiere??","Qué fas per ma casa?",""}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Porca miseria!!","l'uomo pipistrello!!","m'ha llevatto il gorro!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Pipistrello??","Això ques??",""}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Il batmano!!","Incontrame il gorro","per l'amore d'un porco!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Tinc faena. A mi m'ha","furtat els cacaus i","he de trobarlos."}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Lei tenne in su quarto!","Io tengo micha clave!","Achudame i t'achudo!"}}
    },
    premiere_intro_gorro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_premiere,text={"Premiere??","Qué fas per ma casa?",""}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Porca miseria!!","l'uomo pipistrello!!","m'ha llevatto il gorro!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Pipistrello??","Això... be, te igual","Ací el tens, atontat."}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Per fortuna!","Grazie mille!","Come podo achudarte?"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Pos Batman m'ha furtat","els cacaus. Supose que","els te amagats."}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"In lo suo quarto!","Io tengo micha clave!","Tenne, tenne!!"}}
    },
    premiere_gorro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_premiere,text={"Tin, el ditxos gorro.","",""}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Bravissimo!","Che giornatta","fantastica! Figo!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Tu si que eres un","figo. On està eixa","mitja clau?"}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"Giusto! Aquí tenne!","Buona fortuna per","la tua avventura!"}},
        {x=avatar_premiere_x,y=avatar_premiere_y,flip=true,audio="premiere",text={"E NON TOCARE il","Batmano! Sei molto","poderoso!"}}
    },
    gorro_trobat_nointro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Mira, el gorro","de Premiere.","Millor l'agafe."}}
    },
    gorro_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Mira, el gorro","de Premiere.","A la saca."}}
    },
    elalien_intro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_elalien,text={"Que passa alien?","A tu també t'ha","putejat el Batman?"}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"CAGUEN L'OU!!","ESTAVA JO TAN TRANQUIL","PODANT AL MEU GOS..."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"...I QUAN NO MIRABA","M'HA FURTAT EL","MARANÀ!!!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Es...taves podant","a un gos...?",""}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"EN JUPITER NO POTS","DEIXAR QUE LI CREIXEN","MES DE 12 PATES!!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Uh... clar...","A mi m'ha furtat","els cacaus."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"ELS TÉ EN LA SEUA","HABITACIÓ SEGUR.","TROBA EL BERENAR..."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"...I JO ET DONARÉ","LA MEITAT DE LA CLAU","PER A ENTRAR."}}
    },
    elalien_intro_peu={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_elalien,text={"Que passa alien?","A tu també t'ha","putejat el Batman?"}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"CAGUEN L'OU!!","ESTAVA JO TAN TRANQUIL","PODANT AL MEU GOS..."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"...I QUAN NO MIRABA","M'HA FURTAT EL","MARANÀ!!!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Podant...? be, te","igual. Açò deu ser","el teu berenar."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"OSTI QUE BÓ!!!","GRACIES AUELO!","A TU QUE T'HA FET?"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"M'ha furtat els cacaus.","Segur que els té","al seu quarto."}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"SEGUR! PERO JO TINC","LA MEITAT DE LA CLAU.","TIN, I SORT!"}}
    },
    elalien_peu={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",musica=audio_song_elalien,text={"Ací tens...","Uff, que peste fa...",""}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"GRACIES!!!","SÍ, EH? QUE","SUCOSET I BÓ."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Clar...","Be, i eixa","mitja clau?"}},
        {x=avatar_elalien_x,y=avatar_elalien_y,flip=true,audio="elalien",text={"TIN! QUAN TINGUES","ELS CACAUS, PEGA-LI","CACAUÀ A BATMAN!"}},
    },
    peu_trobat_nointro={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Un peu podrit??","Qué asco!! Segur","que és d'el alien."}}
    },
    peu_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Joer, açò deu ser","el berenar d'el alien.","Ecs..."}}
    },
    habitacio_batman={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"L'habitació de Batman!","Segur que els cacaus","estàn ahí dins."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Però necessite","una clau...",""}}
    },
    bol_trobat={
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"PER FI, JA TINC","ELS MEUS CACAUS!!!","OLE!! OLE!!"}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Se va a enterar","Batman ara quan","el pille..."}},
        {x=avatar_abad2_x,y=avatar_abad_y,flip=false,audio="abad",text={"Ara si que li puc","pegar una bona","cacauà..."}}
    },
    final= {
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",musica=audio_song_batman,text={"AI!", "M'has fet mal!!!",""},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Aixó per furtar-me","el bol de cacaus,","imbèsil"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Cert, esta me la","mereixia... però", "mira que be estàs!"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Amb el que t'ha","costat pillar-me", "t'has ficat en forma"},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"No me vingues en","tonteries,","mamarratxo."},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"Creus que no he","notat que falta mig","bol de cacaus?"},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Jejejeje, es que","estàn molt bons!", ""},die=20},
        {x=avatar_batman_x,y=avatar_batman_y,flip=true,audio="batman",text={"Però si vols te","done del meu bol", "de tramussos."},die=20},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"TRAMUSSOS...","",""},die=10},
        {x=avatar_abad_x,y=avatar_abad_y,flip=false,audio="abad",text={"M'atreviré a","passar l'última","frontera...?"},die=40},
    },
}

function start_scene(scene,offset)
    --bkg=newsurf(128,96)
    --setdest(bkg)
    --setsource(0)
    --draw.surf(0,0,128,96,0,0)
    --setsource(tiles)
    --setdest(0)
    scenes.current_scene=scene
    scenes.dnum=1
    scenes.step=0
    scenes.char=1
    scenes.wait=0
    scenes.offset=28
    scenes.die=scenes.current_scene[scenes.dnum].die or 0
    if scene[1].musica then
        --print(scene[1].musica)
        music.play(scenes.current_scene[scenes.dnum].musica)
    end
    if offset then scenes.offset=offset end
    old_update=game_update
    game_update=update_scene
end

function playtext(snd)
    if snd=="abad" then
        sound.play(audio_text_abad)
    elseif snd=="batman" then
        sound.play(audio_text_batman)
    elseif snd=="premiere" then
        sound.play(audio_text_premiere)
    elseif snd=="elalien" then
        sound.play(audio_text_elalien)
    end
end

function draw_photo ( photo_x, photo_y, flip, step )
	local photo_w = 11*o2aX
	local photo_h = 10*o2aX
	
	local bkg_x =  8*o2aX
	local bkg_y = 12*o2aX
	local bkg_w = photo_w*2+2
	local bkg_h = photo_h*2+2
	local bkg_color = 16

	if flip then
		bkg_x = 96*o2aX+1
	end

	if step==3 then
		bkg_y = 12*o2aX
		bkg_h = 12*o2aX
	elseif step==4 or step==8 then
		bkg_y = 7*o2aX
		bkg_h = photo_h*2+2
	end
	
	local photo_dx = bkg_x+1
	local photo_dy = bkg_y+1
	local photo_dw = bkg_w-2
	local photo_dh = bkg_h-2
	
	draw.rectf(bkg_x,bkg_y,bkg_w,bkg_h,bkg_color)
	draw.surf(photo_x,photo_y,photo_w,photo_h,photo_dx,photo_dy,photo_dw,photo_dh, flip)
end

function draw_scene()
    local txt_x = not scenes.current_scene[scenes.dnum].flip and 38 or 8
	
    view.origin(0,scenes.offset*o2aX)
    if scenes.step==0 then
        draw.rectf(0,16*o2aX,129*o2aX,2*o2aX,12)
        draw.line(0,15*o2aX,128*o2aX,15*o2aX,16)
        draw.line(0,18*o2aX,128*o2aX,18*o2aX,16)
    elseif scenes.step==1 then
        draw.rectf(0,8*o2aX,129*o2aX,19*o2aX,12)
        draw.line(0,7*o2aX,128*o2aX,7*o2aX,16)
        draw.line(0,27*o2aX,128*o2aX,27*o2aX,16)
    elseif scenes.step==2 then
        draw.rectf(0,0,129*o2aX,35*o2aX,12)
        draw.line(0,0,128*o2aX,0,16)
        draw.line(0,34*o2aX+1,128*o2aX,34*o2aX+1,16)
    elseif scenes.step==3 then
		draw_photo ( scenes.current_scene[scenes.dnum].x, scenes.current_scene[scenes.dnum].y, scenes.current_scene[scenes.dnum].flip, scenes.step)
    elseif scenes.step==4 then
		draw_photo ( scenes.current_scene[scenes.dnum].x, scenes.current_scene[scenes.dnum].y, scenes.current_scene[scenes.dnum].flip, scenes.step)
    elseif scenes.step==5 then
        --  text(string.sub(scenes.current_scene[scenes.dnum].text[1],1,scenes.char),txt_x*o2aX,7*o2aX,2)
		-- view.origin(0,scenes.offset-20) -- Ni idea de perquè 38, o es 19*o2aX?
        -- arc_textB(string.sub(scenes.current_scene[scenes.dnum].text[1],1,scenes.char),txt_x*o2aX,7*o2aX,2)
        arc_textB(utf8.sub(scenes.current_scene[scenes.dnum].text[1],1,scenes.char),txt_x*o2aX,7*o2aX,2)
        -- if scenes.char<#scenes.current_scene[scenes.dnum].text[1] then
        if scenes.char<utf8.len(scenes.current_scene[scenes.dnum].text[1]) then
            scenes.step=scenes.step-1
            scenes.char=scenes.char+1
            playtext(scenes.current_scene[scenes.dnum].audio)
        else
            scenes.char=1
        end
    elseif scenes.step==6 then
        -- text(string.sub(scenes.current_scene[scenes.dnum].text[2],1,scenes.char),txt_x*o2aX,15*o2aX,2)
		-- view.origin(0,scenes.offset-20) -- Ni idea de perquè 38, o es 19*o2aX?
        -- arc_textB(string.sub(scenes.current_scene[scenes.dnum].text[2],1,scenes.char),txt_x*o2aX,15*o2aX,2)
        arc_textB(utf8.sub(scenes.current_scene[scenes.dnum].text[2],1,scenes.char),txt_x*o2aX,15*o2aX,2)
        -- if scenes.char<#scenes.current_scene[scenes.dnum].text[2] then
        if scenes.char<utf8.len(scenes.current_scene[scenes.dnum].text[2]) then
            scenes.step=scenes.step-1
            scenes.char=scenes.char+1
            playtext(scenes.current_scene[scenes.dnum].audio)
        else
            scenes.char=1
        end
    elseif scenes.step==7 then
        -- text(string.sub(scenes.current_scene[scenes.dnum].text[3],1,scenes.char),txt_x*o2aX,23*o2aX,2)
		-- view.origin(0,scenes.offset-20) -- Ni idea de perquè 38, o es 19*o2aX?
        -- arc_textB(string.sub(scenes.current_scene[scenes.dnum].text[3],1,scenes.char),txt_x*o2aX,23*o2aX,2)
        arc_textB(utf8.sub(scenes.current_scene[scenes.dnum].text[3],1,scenes.char),txt_x*o2aX,23*o2aX,2)
        -- if scenes.char<#scenes.current_scene[scenes.dnum].text[3] then
        if scenes.char<utf8.len(scenes.current_scene[scenes.dnum].text[3]) then
            scenes.step=scenes.step-1
            scenes.char=scenes.char+1
            playtext(scenes.current_scene[scenes.dnum].audio)
        else
            scenes.char=1
        end
    elseif scenes.step==8 then
        draw.rectf(0,0,129*o2aX,35*o2aX,12)
        draw.line(0,0,128*o2aX,0,16)
        draw.line(0,34*o2aX+1,128*o2aX,34*o2aX+1,16)
		draw_photo ( scenes.current_scene[scenes.dnum].x, scenes.current_scene[scenes.dnum].y, scenes.current_scene[scenes.dnum].flip, scenes.step)
        -- if not scenes.current_scene[scenes.dnum].flip then
        --     draw.rectf(8*o2aX,7*o2aX,24*o2aX,22*o2aX,16)
        --     draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,9*o2aX,8*o2aX,20*o2aX,20*o2aX)
        -- else
        --     draw.rectf(97*o2aX,7*o2aX,24*o2aX,22*o2aX,16)
        --     draw.surf(scenes.current_scene[scenes.dnum].x,scenes.current_scene[scenes.dnum].y,avatar_w,avatar_h,98*o2aX,8*o2aX,20*o2aX,20*o2aX,true)
        -- end
        -- text(scenes.current_scene[scenes.dnum].text[1],txt_x*o2aX,7*o2aX,2)
        -- text(scenes.current_scene[scenes.dnum].text[2],txt_x*o2aX,15*o2aX,2)
        -- text(scenes.current_scene[scenes.dnum].text[3],txt_x*o2aX,23*o2aX,2)
		arc_textB(scenes.current_scene[scenes.dnum].text[1],txt_x*o2aX,7*o2aX,2)
        arc_textB(scenes.current_scene[scenes.dnum].text[2],txt_x*o2aX,15*o2aX,2)
        arc_textB(scenes.current_scene[scenes.dnum].text[3],txt_x*o2aX,23*o2aX,2)
    end
    if scenes.step<=8 then scenes.step=scenes.step+1 end
    view.origin(0,0)
end

function update_scene()
    scenes.wait=scenes.wait+1

    if scenes.wait==6 then
        scenes.wait=0
        draw_scene()
        if scenes.step>8 then
            --print(scenes.die)
            if scenes.die>0 then
                scenes.die=scenes.die-1
                if scenes.die==0 then
                    if scenes.dnum==#scenes.current_scene then
                        music.stop()
                        if batman.endgame then
                            final_init()
                            fade.fadeoutin()
                        else
                            game_update=old_update
                        end
                    else
                        scenes.dnum=scenes.dnum+1
                        scenes.die=scenes.current_scene[scenes.dnum].die or 0
                        scenes.step=2
                    end
                end
            else
                if math.floor(sys.time()*2)%2==0 then
                    draw.surf(104*o2aX,88*o2aX,7*o2aX,5*o2aX,118*o2aX,56*o2aX)
                else
                    draw.rectf(118*o2aX,56*o2aX,8*o2aX,6*o2aX,12)
                end
            end
        end
    end

    if (key.press(key.ESCAPE) or pad.press(btnPause)) and (scenes.current_scene~=scenes.final) then
        if scenes.current_scene[scenes.dnum].die then
            game_init(true)
        else
            pause()
        end
    end

    if scenes.step>8 then
        if key.press(keyShoot) or key.press(keyDown) or key.press(keyUp) or key.press(keyLeft) or key.press(keyRight) or key.press(key.RETURN) or
            pad.press(btnShoot) or pad.press(btnDown) or pad.press(btnUp) or pad.press(btnLeft) or pad.press(btnRight) then
            if scenes.dnum==#scenes.current_scene then
                music.stop()
                if batman.endgame then
                    final_init()
                    fade.fadeoutin()
                else
                    game_update=old_update
                end
            else
                scenes.dnum=scenes.dnum+1
                scenes.step=2
            end
        end
    elseif key.press(keyShoot) or pad.press(btnShoot) then
        scenes.step=8
    end
end
