/**
 * (c) by aheadware.com
 */

var app = app || {};
app.state = app.state || {};

app.state.GroupTermsConditions = function()
{
    var state = app.core.StateManager.createState( 'group-terms-conditions' );

    state.onEnter = function( p )
    {
        var $content = app.core.View.getContent(),
            groupId = p.groupId;

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Team',
            'getDetails',
            { teamId : groupId },
            function( res )
            {
                var mGroup = new app.model.Group(res.team),
                    currentProfile = null;

                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'group-terms',
                        {
                            group: mGroup,
                            currentProfile: app.model.SessionUser.getUserProfile()
                        })
                );

                $content.find( '[data-id="btn-copy-ical-group-link"]' ).first().click( function()
                {
                    app.util.Helper.copyInputToClipboard( $content.find( '[data-id="input-group-ical"]' ).first() );
                    app.core.View.toastSuccess( _lc( 'CALENDER_LINK_COPIED' ) );
                });

                $content.find( '[data-id="checkbox-terms-conditions-accept"]' ).first().change( function()
                {
                    let accepted = $content.find( '[data-id="checkbox-terms-conditions-accept"]' ).first().is(":checked");
                    app.core.Rpc.call(
                        'Profile',
                        'acceptTerms',
                        { groupId: groupId, termsAccepted: accepted },
                        function( res )
                        {
                            let msg = _lc( accepted ? 'GROUP_TERMS_ACCEPTED' : 'GROUP_TERMS_NOT_ACCEPTED');

                            $content.find( '[data-id="terms-accepted-note"]' ).first().text("(" + msg + ")");
                            app.core.View.toastSuccess( msg );
                        },
                        function ( res ){
                            app.core.View.toastError( _lc( 'GENERAL_SERVER_ERROR' ) );
                        });

                });
                app.gui.PageLoader.hide();
            }
        );
    };

    return state;
};