cxr2 = arcade_config.character_per_row_base2

premiere={name="premiere",
          hab=75,
          x=24,
          y=24,
          w=32,
          h=32,
          flip=false,
          frame=12,
          wait=0,
          step=0,
          anim={12,13,12,14},
          bb={x=0,y=0,w=32,h=32},
          scene_intro=false,
          scene_object=false}

function premiere.init()
    -- premiere.x=24
    premiere.x=4
    -- premiere.y=24
    premiere.y=3
    premiere.w=32
    premiere.h=32
    premiere.frame=12
    premiere.wait=0
    premiere.step=0
    -- premiere.update=premiere.update_normal
    premiere.update=premiere.update_stay
    premiere.scene_intro=false
    premiere.scene_object=false
    -- local habs={75,44,20,0}
    local habs={75,44,20,0}
    -- premiere.hab=habs[1+math.random(0,3)]
    
    premiere.hab=0
    
    local world_x, world_y = coords.room_to_world(premiere.hab,premiere.x,premiere.y)
    premiere.x=world_x
    premiere.y=world_y
    -- print(premiere.hab)
end

function premiere.draw()
    local scr_x, scr_y = viewp:screen_coords( premiere.x, premiere.y )
    local x = (premiere.frame&7)*premiere.w
    local y = (premiere.frame>>cxr2)*premiere.h
    if premiere.update == premiere.update_stay then
        -- dibuixar a premiere excepte els peus
        draw.surf(x,y,premiere.w,premiere.h-8,scr_x,scr_y,premiere.w,premiere.h-8,premiere.flip)
        -- dibuixar peus statics
        scr_y = scr_y + 24; -- Posició dels peus en pantalla
        if premiere.frame==13 then
            draw.surf(224,16,premiere.w,8,scr_x,scr_y,premiere.w,8,premiere.flip)
        elseif premiere.frame==14 then
            draw.surf(224,24,premiere.w,8,scr_x,scr_y,premiere.w,8,premiere.flip)
        else
            draw.surf(x,y+24,premiere.w,8,scr_x,scr_y,premiere.w,8,premiere.flip)
        end
    else
        draw.surf(x,y,premiere.w,premiere.h,scr_x,scr_y,premiere.w,premiere.h,premiere.flip)
    end
end

function premiere.hit()
end

function premiere.update_stay()
    premiere.wait=premiere.wait+1

    if premiere.wait==96 then
        premiere.wait=0
        premiere.step=(premiere.step+1)%4
        premiere.frame=premiere.anim[premiere.step+1]
    end
    
    if premiere.hab==abad.hab then
        if collision(abad,premiere) then
            if not premiere.scene_intro then
                if abad.objects.gorro~=nil then
                    start_scene(scenes.premiere_intro_gorro)
                    premiere.scene_object=true
                    abad.objects.clau_premiere=true
                    abad.objects.gorro=nil
                else
                    start_scene(scenes.premiere_intro)
                end
                premiere.scene_intro=true
            elseif not premiere.scene_object and abad.objects.gorro~=nil then
                start_scene(scenes.premiere_gorro)
                premiere.scene_object=true
                abad.objects.clau_premiere=true
                abad.objects.gorro=nil
            end
        end
    end    
end

function premiere.update_normal()
    premiere.wait=premiere.wait+1

    if premiere.wait==6 then
        premiere.wait=0
        premiere.step=(premiere.step+1)%4
        premiere.frame=premiere.anim[premiere.step+1]

        local inc=12 if premiere.flip then inc=2 end
        if not premiere.flip and premiere.x==84 then
            premiere.hab=premiere.hab+1
            premiere.x=-4
        elseif check_tile(premiere.hab,premiere.x+inc,premiere.y+14)<tiletype.half then
            if premiere.flip then
                premiere.x=premiere.x-2
            else
                premiere.x=premiere.x+2
            end
            if premiere.x<-4 then
                premiere.hab=premiere.hab-1
                premiere.x=84
            end
        elseif check_tile(premiere.hab,premiere.x+inc,premiere.y+6)<tiletype.half then
            premiere.update=premiere.update_jumping
            premiere.step=0
        else
            --if check_tile(premiere.hab,premiere.x+4,premiere.y+16)==tiletype.void and ((premiere.x+4)&7==0 or check_tile(premiere.hab,premiere.x+12,premiere.y+16)==tiletype.void) then
                --premiere.update=premiere.update_falling
            --else
                premiere.flip=not premiere.flip
            --end
        end
        if check_tile(premiere.hab,premiere.x+4,premiere.y+16)==tiletype.void and ((premiere.x+4)&7==0 or check_tile(premiere.hab,premiere.x+12,premiere.y+16)==tiletype.void) then
            premiere.update=premiere.update_falling
        end

        if premiere.hab==abad.hab then
            if aabb(abad,premiere) then
                if not premiere.scene_intro then
                    if abad.objects.gorro~=nil then
                        start_scene(scenes.premiere_intro_gorro)
                        premiere.scene_object=true
                        abad.objects.clau_premiere=true
                        abad.objects.gorro=nil
                    else
                        start_scene(scenes.premiere_intro)
                    end
                    premiere.scene_intro=true
                elseif not premiere.scene_object and abad.objects.gorro~=nil then
                    start_scene(scenes.premiere_gorro)
                    premiere.scene_object=true
                    abad.objects.clau_premiere=true
                    abad.objects.gorro=nil
                end
            end
        end

    end
end

function premiere.update_jumping()
    premiere.frame=13
    premiere.wait=premiere.wait+1

    if premiere.wait==6 then
        premiere.wait=0
        
        local inc=12 if premiere.flip then inc=2 end
        if not premiere.flip and premiere.x==84 then
            premiere.hab=premiere.hab+1
            premiere.x=-4
        elseif check_tile(premiere.hab,premiere.x+inc,premiere.y+14)<tiletype.half and check_tile(premiere.hab,premiere.x+inc,premiere.y+16)~=tiletype.void then
            if premiere.flip then
                premiere.x=premiere.x-2
            else
                premiere.x=premiere.x+2
            end
            if premiere.x<-4 then
                premiere.hab=premiere.hab-1
                premiere.x=84
            end
        end

        if premiere.step<6 then
            if premiere.y>0 then
                if check_tile(premiere.hab,premiere.x+4,premiere.y-2)~=tiletype.block then
                    if (premiere.x+4)&7==0 or check_tile(premiere.hab,premiere.x+12,premiere.y-2)~=tiletype.block then
                            premiere.y=premiere.y-2
                    end
                end
            else
                premiere.hab=premiere.hab-10
                premiere.y=32
            end
        elseif premiere.step>6 then
            premiere.update=premiere.update_falling
        end
        premiere.step=premiere.step+1
    end

end

function premiere.update_falling()
    premiere.frame=13
    premiere.wait=premiere.wait+1

    if premiere.wait==6 then
        premiere.wait=0

        local inc=12 if premiere.flip then inc=2 end
        if not premiere.flip and premiere.x==84 then
            premiere.hab=premiere.hab+1
            premiere.x=-4
        elseif check_tile(premiere.hab,premiere.x+inc,premiere.y+14)<tiletype.half and check_tile(premiere.hab,premiere.x+inc,premiere.y+16)~=tiletype.void then
            if premiere.flip then
                premiere.x=premiere.x-2
            else
                premiere.x=premiere.x+2
            end
            if premiere.x<-4 then
                premiere.hab=premiere.hab-1
                premiere.x=84
            end
        end

        local xx=math.floor((premiere.x+4)/8)
        local yy=math.floor((premiere.y+16)/8)
        
        if premiere.y<32 then
            if (premiere.y+16)&7==0 and (check_tile(premiere.hab,premiere.x+4,premiere.y+16)>=tiletype.half or ((premiere.x+4)&7~=0 and check_tile(premiere.hab,premiere.x+12,premiere.y+16)>=tiletype.half)) then
                premiere.update=premiere.update_normal
                return
            end
            premiere.y=premiere.y+2
        else
            premiere.hab=premiere.hab+10
            premiere.y=0
        end
    end
end

function premiere.healer_update()
     premiere.wait=premiere.wait+1

    -- Esperar mentres dona la vida
    if premiere.health_wait>0 and premiere.health_wait<100 then
        premiere.health_wait = premiere.health_wait - 1
    else
        premiere.x = premiere.x+1
        if premiere.wait>=6 then
            premiere.wait = 0
            premiere.step=(premiere.step+1)%4
            premiere.frame=premiere.anim[premiere.step+1]
        end
    end

    -- Lloc de pausa
    local hab, tx, ty = coords.world_to_tile(premiere.x, premiere.y)
    if hab == 55 and tx==0 and ty==3 and premiere.health_wait==100 then
        -- 55 (0,3)
        dialeg.add(premiere, "Tenne! Io t'achudo. Figo!", 200)
        -- table.insert(
        --     dialeg.list, 
        --     { actor=premiere, 
        --       text=_text,
        --       time=200,
        --       w = math.floor(4.6*#_text), 
        --       h = 20,
        --     }
        -- )
        premiere.health_wait = premiere.health_wait - 1
        table.insert( actors, health_potion.new(
            premiere.hab, 
            premiere.x+premiere.bb.x+premiere.bb.w, 
            premiere.y+premiere.bb.y+premiere.bb.h/3, 
            1, 24, 3) 
        )
        -- health_potion.init(premiere.hab, 
        --                    premiere.x+premiere.bb.x+premiere.bb.w,
        --                    premiere.y+premiere.bb.y+premiere.bb.h/3,
        --                    1, 24, 2)
    elseif hab >= 56 then
        premiere.restore()
    end
end

function premiere.backup()
    -- Guardar l'estat de Premiere
    premiere.from_hab = premiere.hab 
    premiere.from_x = premiere.x
    premiere.from_y = premiere.y
    premiere.from_update = premiere.update
end

function premiere.restore()
    -- Restaurar a Premiere
    premiere.hab    = premiere.from_hab
    premiere.x      = premiere.from_x
    premiere.y      = premiere.from_y
    premiere.update = premiere.from_update
end

function premiere.stage1_ending_init()
    -- if premiere.update==premiere_update_healer then return true end
    premiere.backup()
    -- Moure a premiere al ending
    premiere.hab = 7
    premiere.x = 6
    premiere.y = 3
    local world_x, world_y = coords.room_to_world(premiere.hab,premiere.x,premiere.y)
    premiere.x=world_x
    premiere.y=world_y
    premiere.update = premiere.stage1_ending_update
end

function premiere.stage1_ending_update()
    premiere.wait=premiere.wait+1

    -- avançar a 8,1,3
    local hab, tx, ty = coords.world_to_tile(premiere.x, premiere.y)
    if hab == 8 and tx==1 and ty==3 then
        -- premiere.health_wait = premiere.health_wait - 1
    else
        premiere.x = premiere.x+1
        if premiere.wait>=6 then
            premiere.wait = 0
            premiere.step=(premiere.step+1)%4
            premiere.frame=premiere.anim[premiere.step+1]
        end
    end
end

function premiere.stage1_ending_init2()
    premiere.flip = true
    premiere.update = premiere.stage1_ending_update2
end

function premiere.stage1_ending_update2()
    premiere.wait=premiere.wait+1

    -- avançar a 8,1,3
    local hab, tx, ty = coords.world_to_tile(premiere.x, premiere.y)
    if hab == 7 and tx==5 and ty==3 then
        -- premiere.health_wait = premiere.health_wait - 1
        premiere.flip=false
    else
        premiere.x = premiere.x-1
        if premiere.wait>=6 then
            premiere.wait = 0
            premiere.step=(premiere.step+1)%4
            premiere.frame=premiere.anim[premiere.step+1]
        end
    end
end

function premiere.stage1_ending_init3()
    -- if premiere.update==premiere_update_healer then return true end
    premiere.backup()
    -- Moure a premiere al ending
    premiere.hab = 7
    premiere.x = 6
    premiere.y = 2
    local world_x, world_y = coords.room_to_world(premiere.hab,premiere.x,premiere.y)
    premiere.x=world_x
    premiere.y=world_y
    premiere.update = premiere.stage1_ending_update3
end

function premiere.stage1_ending_update3()
    premiere.wait=premiere.wait+1

    -- avançar a 8,1,3
    local hab, tx, ty = coords.world_to_tile(premiere.x, premiere.y)
    if hab == 7 and tx==5 and ty==3 then
        -- premiere.health_wait = premiere.health_wait - 1
        premiere.flip=false
    else
        if premiere.wait>=3 then
            premiere.x = premiere.x+1
            premiere.wait = 0
            premiere.frame=premiere.anim[1]
        end
    end
end