
#include "Tscene.h"
#include "../ECO/OMesh_gltf.h"


void Tscene::interface_create(OTextureList& textureList)
{
    s_texParam texParam;
    ORect_i dst, src;

    OTextureList::P_setPath(PATH_INTERFACE);
    OTextureList::P_setFilter(OTEXTURELIST_INTERFACE);

    textureList.add("interface.png");

    OTextureList::P_setFilter(OTEXTURELIST_MESH);

    texParam = textureList.getParam("interface.png");

    // ---------------
    // --- general ---
    // ---------------

    dst(0,20,320,16);
    lblInfo0.create(dst, "pav.num. = caméra / enter = rotation");
        lblInfo0.setBackground(OLABEL_FND_BOX, clWhite32, clBlack32);

    dst.x = 512;
    lblInfo1.create(dst, "mouse roll = camera speed");
        lblInfo1.setBackground(OLABEL_FND_BOX, clWhite32, clBlack32);
    m_msg_tps = 0;

    pnlSky.create(textureList, 0,0,-1);
    pnlSky.setHour(5.5f);

    // ---

    const std::string gl_bt_eau[] = {
        "lake low", "lake med", "(no lake hig)",
        "sea low" //, "sea_ med" //, "sea hig"
    };

    src.set(100,0,100,20);
    dst.set(10, ECO_getViewPortH() -200, 120,20);

    rbtEau.create(dst, gl_bt_eau[0], texParam, src);
    for (size_t i = 1; i < ECO_sizeOf(gl_bt_eau); i++)
    {
        rbtEau.add(0, -30, gl_bt_eau[i]);
    }
    rbtEau.setChecked(0);

    // ---

    //enum {PP_NULL, PP_ALIASING,PP_FOG, PP_FOG_HIGHT, PP_RAIN};
    std::string gl_post_process[] = {"null", "aAliasing", "brouillard", "pluie", "neige"};

    src.set(100,0,100,20);
    dst.set(240, ECO_getViewPortH()-10, 100, 20);

    rbtProcess.create(dst, gl_post_process[0], texParam, src);
    for (size_t i = 1; i < ECO_sizeOf(gl_post_process); i++)
    {
        rbtProcess.add(0,-30, gl_post_process[i]);
    }
    rbtProcess.setChecked(1);




    // --------------------------
    // --- ile se (perso flag ---
    // --------------------------

    const std::string gl_bt_shader[] = { "barrel light", "light_pbr" };

    src.set(100,0,100,20);
    dst.set(ECO_getViewPortW() - 140, 150, 120,20);

    rbtShader.create(dst, gl_bt_shader[0], texParam, src);
    for (size_t i = 1; i < ECO_sizeOf(gl_bt_shader); i++)
    {
        rbtShader.add(0,-30, gl_bt_shader[i]);
    }
    rbtShader.setChecked(0);
    m_mode_shader = 0;

    // ---

    const std::string gl_bt_perso[] = { "barbarian", "squeleton" };

    src.set(100,0,100,20);
    dst.set(ECO_getViewPortW() - 190, 700, 120,20);

    rbtPerso.create(dst, gl_bt_perso[0], texParam, src);
    dst.y -= 30;
    for (size_t i = 1; i < ECO_sizeOf(gl_bt_perso); i++)
    {
        rbtPerso.add(dst, gl_bt_perso[i], src);
        dst.y -= 30;
    }
    rbtPerso.setChecked(0);
    m_perso_en_cours = 0;

    // ---

    std::string gl_bt_objet[] = { "<> casque", "<> crane", "<> bouclier", "<> épée" };

    src.set(100,0,100,20);
    dst.set(ECO_getViewPortW()-170, 640, 100, 20);

    for (size_t i = 0; i < ECO_sizeOf(gl_bt_objet); i++)
    {
        btObjet.push_back(OButton());
        btObjet[i].create(dst, gl_bt_objet[i], texParam, src);
        dst.y -= 24;
    }

    // ---

    std::string gl_bt_effect[] = { "null", "freeze", "KO", "fire" };

    src.set(100,0,100,20);
    dst.set(ECO_getViewPortW()-170, 740, 100, 20);

    for (size_t i = 0; i < ECO_sizeOf(gl_bt_effect); i++)
    {
        btEffect.push_back(OButton());
        btEffect[i].create(dst, gl_bt_effect[i], texParam, src);
        dst.y += 24; // ! +
    }

    // ---

    pnlLstAnim.create(textureList);




    // ------------------------
    // --- ile ne (vegetale ---
    // ------------------------

    const std::string gl_bt_grass[] = {
        "grass geometry", "grass instanced"
        //"grass comp"
    };

    src.set(100,0,100,20);
    dst.set(ECO_getViewPortW() - 130, ECO_getViewPortH() -200, 120,20);

    rbtGrass.create(dst, gl_bt_grass[0], texParam, src);
    for (size_t i = 1; i < ECO_sizeOf(gl_bt_grass); i++)
    {
        rbtGrass.add(0,-30, gl_bt_grass[i]);
    }
    rbtGrass.setChecked(0);

    // ---

    const std::string gl_bt_tree[] = {
        "tree(model)", "wood(p+r+s)"
    };

    //src.set(100,0,100,20);
    dst.y -= 100;
    rbtTree.create(dst, gl_bt_tree[0], texParam, src);
    for (size_t i = 1; i < ECO_sizeOf(gl_bt_tree); i++)
    {
        rbtTree.add(0,-30, gl_bt_tree[i]);
    }
    rbtTree.setChecked(0);




    // --------------------
    // --- ile so (meca ---
    // --------------------

    src.set(0,280, 200,40);
    dst.set(20,400, 200,40);
    imgClick.create(dst, texParam, src);

}

void Tscene::interface_draw()
{
    // actualisation dans event.keyDown > camera.move
    lblInfo0.draw();
    lblInfo1.draw();

    pnlSky.draw();
    rbtEau.draw();
    rbtProcess.draw();

    switch(m_mode_interface)
    {
    case ISLE_NO:
        break;
    case ISLE_SO:
        imgClick.draw();
        break;
    case ISLE_SE:
        rbtShader.draw();
        rbtPerso.draw();
        for (size_t i = 0; i < btObjet.size(); i++)
        {
            btObjet[i].draw();
        }
        for (size_t i = 0; i < btEffect.size(); i++)
        {
            btEffect[i].draw();
        }
        pnlLstAnim.draw();
        break;
    case ISLE_NE:
        rbtGrass.draw();
        rbtTree.draw();
        break;
    }

}


void Tscene::interface_init_lst_action(int idx)
{
    /*
    obsolete
    std::vector<s_gltf_animation> liste;
    //liste = meca[idx].mesh.getAnimations();
    liste = meca[idx].getMesh().getAnimations();
    //
    pnlLstAction.clear();
    for (size_t i = 0; i < liste.size(); i++)
    {
        pnlLstAction.addAnim(liste[i].name);
    }
    pnlLstAction.refresh_liste();
    */
}

void Tscene::interface_init_lst_anim(int idx)
{
    const std::vector<s_gltf_animation>& liste = perso[idx].mesh.getAnimations();
    //
    pnlLstAnim.clear();
    for (size_t i = 0; i < liste.size(); i++)
    {
        pnlLstAnim.addAnim(liste[i].name);
    }
    pnlLstAnim.refresh_liste();
}




bool Tscene::interface_mouseDownL(int x, int y)
{
    switch (pnlSky.mouseDown(x,y))
    {
    case TPNLSKY_CYCLE:
        light_init(pnlSky.getHour());
        return true;
    case TPNLSKY_GLEAM:
        skyEarth.gleam.setVisible( pnlSky.getGleam() );
        return true;
    case TPNLSKY_PHASE:
        skyEarth.moon.setPhase( pnlSky.getPhase() );
        skyEarth.sky.setLight(skyEarth.sun, skyEarth.moon);
        return true;
    }

    if (rbtEau.mouseDown())
    {
        return true;
    }

    if (rbtProcess.mouseDown())
    {
        switch(rbtProcess.getChecked())
        {
        case PP_FOG:
            ppRain.stop_sound();
            if (ppFog.getState() == 0)
            {
                ppFog.start();
                lblInfo1.setFonteColor(clRed32);
                lblInfo1.setText("15s for the fog");
                m_msg_tps = FPS_TEMPO * 3; // 3secondes -> voir Mcycle
            }
            break;
        case PP_RAIN:
            if (ppRain.getState() == 0)
            {
                ppRain.start();
            }
            else
            {
                ppRain.stop();
            }
            break;
        case PP_SNOW:
            ppRain.stop_sound();
            if (ppSnow.getState() == 0)
            {
                ppSnow.start();
            }
            break;
        default:
            ppRain.stop_sound();
            break;
        }
        return true;
    }

    // ---

    switch(m_mode_interface)
    {
    case ISLE_NO:
        // vide defaut
        break;
    case ISLE_SO:
        // meca
        break;
    case ISLE_SE:
        // perso
        if (rbtShader.mouseDown())
        {
            m_mode_shader = rbtShader.getChecked();
            return true;
        }

        if (rbtPerso.mouseDown())
        {
            m_perso_en_cours = rbtPerso.getChecked();
            interface_init_lst_anim(m_perso_en_cours);
            return true;
        }

        for (size_t i = 0; i < btObjet.size(); i++)
        {
            if (btObjet[i].mouseDown())
            {
                // ref_object est chargé dans load
                perso_set_equipement( objet[ref_object[i]] );
                return true;
            }
        }

        for (size_t i = 0; i < btEffect.size(); i++)
        {
            if (btEffect[i].mouseDown())
            {
                perso_set_effect(0, i); // barbare, perso 0
                return true;
            }
        }

        switch(pnlLstAnim.mouseDown(x,y))
        {
        case PNLLIST_ANIM:
            // m_perso_en_cours indique le mesh avec bones et animations en cours
            if (m_perso_en_cours == -1) return true;
            perso[m_perso_en_cours].anim_start( pnlLstAnim.getAnim(), pnlLstAnim.getLoop() );
            return true;
        case PNLLIST_LOOP:
            if (m_perso_en_cours > -1)
            {
                perso[m_perso_en_cours].anim_stop();
                return true;
            }
            return true;
        }
        break;
    case ISLE_NE:
        // vegetal
        if (rbtGrass.mouseDown())
        {
            return true;
        }
        if (rbtTree.mouseDown())
        {
            return true;
        }
        break;
    }



    return false;
}

bool Tscene::interface_mouseMove(int x, int y)
{
    pnlSky.mouseMove(x,y);
    rbtEau.mouseMove(x,y);
    rbtProcess.mouseMove(x,y);

    // ---

    switch(m_mode_interface)
    {
    case ISLE_NO:
        // vide defaut
        break;
    case ISLE_SO:
        // meca
        break;
    case ISLE_SE:
        rbtShader.mouseMove(x,y);
        rbtPerso.mouseMove(x,y);

        for (size_t i = 0; i < btObjet.size(); i++)
        {
            btObjet[i].mouseMove(x,y);
        }

        for (size_t i = 0; i < btEffect.size(); i++)
        {
            btEffect[i].mouseMove(x,y);
        }

        pnlLstAnim.mouseMove(x,y);
        break;
    case ISLE_NE:
        rbtGrass.mouseMove(x,y);
        rbtTree.mouseMove(x,y);
        break;
    }

    return false;
}

bool Tscene::interface_mouseWheel(int delta)
{
    switch(pnlSky.mouseWheel(delta))
    {
    case TPNLSKY_HOUR:
        light_init(pnlSky.getHour());
        return true;
    case TPNLSKY_PHASE:
        skyEarth.moon.setPhase( pnlSky.getPhase() );
        skyEarth.sky.setLight(skyEarth.sun, skyEarth.moon);
        return true;
    }

    if (pnlLstAnim.mouseWheel(delta)) return true;
    //if (pnlLstAction.mouseWheel(delta)) return;

    return false;
}

void Tscene::interface_shift()
{

}


