require( 'facebook/common/style.lua' );
dofile( "facebook/common/facebook_functions.lua" );

dofile( "facebook/game/facebook_globals.lua" );

local FirstStage = false;

if FirstStage then


FacebookButtonRolloverSound = "sfx/main_rollover";
FacebookButtonClickSound = "cadi/ui_click";

facebook_share_style_position =
{
    x = kMax, y = 0,
    align = kHAlignRight + kVAlignTop,
    sound = FacebookButtonClickSound,  
    rolloversound = FacebookButtonRolloverSound,
};

facebook_connectbutton_style_position =
{
    x = kMax,
    y = kMax,
    align = kHAlignRight + kVAlignBottom,
};

facebook_friendfeed_style_position =
{
    x = kMax,
    y = kCenter - 30,
    align = kHAlignRight + kVAlignCenter,
};

facebook_friendfeed_tutorial_position =
{
    x = kMax - 10,
    y = kMax - 40,
    align = kHAlignRight + kVAlignBottom,
};

facebook_friendfeed_tutorial_arrow_position =
{
    posx = 725 + 15, posy = 85,
};

--	Group = Window;
--	FBUI_FriendFeed = Window;
    FBFXWindow = Window;

    function CheckUsers()
        return true;
    end;

    function IsFacebookEnabled()
        return true;
    end;

    function GetGlobalIntPreference()
        return '0';
    end;

    function Facebook_IsLoggedIn()
        return true;
    end;

    function Facebook_FriendFeed_NumFriends()
        return 1;
    end;
end;


facebook_connect_button =
{
    "facebook/common/facebook_connect",
};


-- fonts
facebook_font = 'facebook/common/helvetica.mvec';

-- background art assets

facebook_friendfeedcolor = Color(255,255,255,255);
facebook_friendfeedtitlecolor = Color(0,216,246,255);

FacebookBlackColor = Color(0, 0, 0, 255);



-- friendfeed tutorial TEXT


-- text styles

facebook_friendfeed_connecting_textstyle =
{
    font = {facebook_font, 15, facebook_friendfeedcolor},
    flags = kVAlignCenter + kHAlignCenter,
    x=2,y=0,w=kMax-2,h=kMax,
    label = 'facebook_friendfeed_connecting',
};

facebook_friendfeed_error_textstyle =
{
    font = {facebook_font, 15, facebook_friendfeedcolor},
    flags = kVAlignCenter + kHAlignCenter,
    x=10,y=0,w=kMax-10,h=kMax,
    label = 'facebook_msg_error_general_body',
};

facebook_friendfeed_nofriends_textstyle =
{
    font = {facebook_font, 15, facebook_friendfeedcolor},
    flags = kVAlignCenter + kHAlignCenter,
    x=2,y=0,w=kMax-2,h=kMax,
    label = '#'..GetString('facebook_friendfeed_nofriends', GetString('facebook_application_name')),
    -- label = 'nofriends',
};

facebook_friendfeed_name_textstyle =
{
    font = {facebook_font, 10, facebook_friendfeedcolor},
    flags = kVAlignCenter + kHAlignCenter,
    x=60,y=6,w=kMax-5,h=50,
    label = '#TEST NAME GOES HERE'
};

facebook_friendfeed_title_textstyle =
{
    font = {facebook_font, 10, facebook_friendfeedtitlecolor},
    flags = kVAlignTop + kHAlignCenter,
    x=2,y=60,w=kMax-2,h=30,
    label = '#TEST TITLE GOES HERE'
};


facebook_friendfeed_comment_textstyle =
{
    font = {facebook_font, 10, facebook_friendfeedcolor},
    flags = kVAlignCenter + kHAlignCenter,
    x=2,y=100,w=kMax-2,h=30,
    label = '#TEST COMMENT GOES HERE'
};

function Facebook_FriendFeed_Start()
    EnableWindow('friendfeed_bkgd', true);
    EnableWindow('friendfeed_connect_button', false);
    Facebook_FriendFeed_Connecting(true);
end

function Facebook_FriendFeed_Stop()
    EnableWindow('friendfeed_bkgd', false);
    EnableWindow('friendfeed_connect_button', true);
end

function Facebook_FriendFeed_Connecting(bConnecting)
    EnableWindow('friendfeed_connectingdisplay', bConnecting);
    EnableWindow('friendfeed_errordisplay', false);
    if (bConnecting == false) then
        EnableWindow('friendfeed_nofriendsdisplay', (Facebook_FriendFeed_NumFriends() <= 0));
        EnableWindow('friendfeed_frienddisplay', (Facebook_FriendFeed_NumFriends() > 0));
    else
        EnableWindow('friendfeed_nofriendsdisplay', false);
        EnableWindow('friendfeed_frienddisplay', false);
    end
end

function Facebook_FriendFeed_Error()
    EnableWindow('friendfeed_connectingdisplay', false);
    EnableWindow('friendfeed_nofriendsdisplay', false);
    EnableWindow('friendfeed_frienddisplay', false);
    EnableWindow('friendfeed_errordisplay', true);
end


facebook_logout_button_graphics =
{
    "facebook/common/fb_no_btn_up",
    "facebook/common/fb_no_btn_down",
    "facebook/common/fb_no_btn_rollover",
};

facebook_friendfeed_logout_buttonstyle =
{
    sound = FacebookButtonClickSound,
    rolloversound = FacebookButtonRolloverSound,
    type = kPush,
    graphics = facebook_logout_button_graphics,
    font = FacebookButtonFont,
};

function Facebook_CreateFriendFeed(precondition, buttonpressfunc, loginfunc)
    DebugOut("Facebook_CreateFriendFeed()");
    if (not IsFacebookEnabled()) then return Group{} end;
    
    -- C++ code could call this to continue login
    Complete_Facebook_Login_After_UserCheck = function()
        if (facebook_login(true)) then
            FB_SetState("++FBMMConnectLogin");
            Facebook_SaveCurrentUser();
            Facebook_FriendFeed_StartFeed();
            if (loginfunc) then
                loginfunc();
            end;
        end;
    end;
    
    return Group
    -- facebook_friendfeed_layout
    {
        SetStyle(facebook_connectbutton_style_position),
        FBButton
        {
            sound = FacebookButtonClickSound,
            rolloversound = FacebookButtonRolloverSound,

            graphics = facebook_connect_button,
            type = kPush,
            name = 'friendfeed_connect_button',
            
            command=
                function()
                    FB_SetState("++FBConnectClicks");	
                    
                    if not Facebook_IsLoggedIn() then
                        FB_SetState("++FBMMConnectTry");
                    end;
                    
                    FB_SetState("FBTriedIt", "1");
                    SetGlobalIntPreference('fbtutorial', 1, true);	
                    if (buttonpressfunc) then
                        buttonpressfunc();
                    end
                    if (precondition ~= nil) then
                        if (precondition() == false) then
                            return;
                        end
                    end
                    if (facebook_login(true)) then
                        FB_SetState("++FBMMConnectLogin");
                        Facebook_SaveCurrentUser();
                        Facebook_FriendFeed_StartFeed();
                        if (loginfunc) then
                            loginfunc();
                        end
                    end
                end;
        },

        SetStyle(facebook_friendfeed_style_position),
        FBUI_FriendFeed
        {
            name = 'friendfeed',
            fit = true,

            fit = true,
            picx = 7, -- 30,
            picy = 6, -- 31,
            picwidth = 50,
            picheight = 50,
            timer = 7000,
            nametruncatelines = 0,
            titletruncatelines = 3,
            commenttruncatelines = 3,
            maxfeeds = 10,
            fadetime = 500,

            SetStyle{},
            Window
            {
                x = 7,
                y = 6,
                w = 50,
                h = 50,
            };

            Bitmap
            {
                image = 'facebook/common/friendfeed_bkgd',
                name = 'friendfeed_bkgd',
                Window
                {
                    name = 'friendfeed_connectingdisplay',
                    x = 0, y = 0,
                    w = kMax, h = kMax,
                    SetStyle(facebook_friendfeed_connecting_textstyle),
                    Text
                    {
                        name = 'friendfeed_connecting',
                    };
                };

                Window
                {
                    name = 'friendfeed_errordisplay',
                    x = 0, y = 0,
                    w = kMax, h = kMax,
                    SetStyle(facebook_friendfeed_error_textstyle),
                    Text
                    {
                        name = 'friendfeed_error',
                    };
                };

                Window
                {
                    name = 'friendfeed_nofriendsdisplay',
                    x = 1, y = 1,
                    w = kMax - 1, h = kMax - 50,
                    SetStyle(facebook_friendfeed_nofriends_textstyle),
                    FBButton
                    {
                        name = 'friendfeed_nofriends',
                        command =
                            function()
                                if FB_IsVersionTwo() then
                                    FB_SetState("++FBRandomPostClicks");
                                end;
                                Facebook_PostStreamUI('userpost');
                                Facebook_FriendFeed_StartFeed();
                            end
                    };
                };

                Window
                {
                    name = 'friendfeed_frienddisplay',
                    x = 0, y = 0,
                    w = kMax, h = kMax,
                    SetStyle(facebook_friendfeed_name_textstyle),
                    Text
                    {
                        name = 'friendfeed_friendname',
                    };

                    SetStyle(facebook_friendfeed_title_textstyle),
                    Text
                    {
                        name = 'friendfeed_friendtitle',
                    };


                    SetStyle(facebook_friendfeed_comment_textstyle),
                    Text
                    {
                        name = 'friendfeed_friendcomment',
                    };
                };


                SetStyle(facebook_friendfeed_logout_buttonstyle),
                FBButton
                {
                    name = 'friendfeed_logout_button',
                    x = kCenter,
                    y = kMax - 4,
                    align = kVAlignBottom,

                    command =
                        function()
                            facebook_logout(); -- LogoutCurrentUser();
                            Facebook_FriendFeed_StopFeed();
                        end;

                    Bitmap
                    {
                        image = 'facebook/common/facebook_f',
                        y = kCenter,
                        x = 10,
                    },
                    Text
                    {
                        x = 32,
                        w = kMax, h = kMax,
                        -- label = 'logout',
                        label = 'facebook_friendfeed_btn_logout',
                        flags = kVAlignCenter,
                    }
                };

            },
        },
    };
    
end;

facebook_tutorial_layout =
{
    Window
    {
        name = 'fbtutorial',
        w = 1024, h = 768,
        fit = true,

        SetStyle(facebook_friendfeed_tutorial_position),
        Bitmap
        {
            name = 'facebook_friendfeed_tutorial_bg',
            image = 'facebook/common/fb_tutorial_bg_box',
            alpha = 0.7,
                
            SetStyle{};
            Text
            {
                x = 35, y = 12,
                w = kMax - 2, h = kMax - 2,
                font = { FacebookFontFile, 14, FacebookWhiteColor, },
                flags = kHAlignLeft + kVAlignTop,
                label = 'facebook_tutorial',
                -- label = 'Connect to Facebook to see what your friends are doing!',
            };
        };
    };
}

function Facebook_CreateFriendFeedPopup()
    DebugOut("Facebook_CreateFriendFeedPopup()");
    if (not IsFacebookEnabled()) then 
        DebugOut("Facebook Not Enabled()");
        return Group{} 
    end;

    local already_seen_tut = GetGlobalIntPreference('fbtutorial', 0) == 1;

    local isloggedin = Facebook_IsLoggedIn();

    DebugOut("alreadyseentut " .. tostring(already_seen_tut));
    DebugOut("isloggedin " .. tostring(isloggedin));
    
    if ( (not already_seen_tut) and (not isloggedin)) then

        DebugOut("Constructing Popup.");


        return Group(facebook_tutorial_layout);

    else
        DebugOut("NOT Constructing Popup.");
        SetGlobalIntPreference('fbtutorial', 1, true);
        return Group{};
    end
end

function FBConnectPress()
    DebugOut("HIDING Popup");
    EnableWindow('fbtutorial', false);
end

if FirstStage then

    -- MakeDialog(facebook_tutorial_layout);
    -- MakeDialog(facebook_friendfeed_layout);

MakeDialog
{
    x = 0, y = 0, w = 1024, h = 768,
    Facebook_CreateFriendFeed(CheckUsers, FBConnectPress);
};
-- Facebook_CreateFriendFeedPopup();

-- Facebook_FriendFeed_Error();
-- Facebook_FriendFeed_Connecting();
-- Facebook_FriendFeed_Start();
-- Facebook_FriendFeed_Stop();
-- Facebook_FriendFeed_Connecting(false);

end;

-- Legacy Synonym
CreateFriendFeed = Facebook_CreateFriendFeed
CreateFriendFeedPopup = Facebook_CreateFriendFeedPopup
