require([
    'bitbucket/util/state',
    'bitbucket/internal/feature/comments/likes/web-panel'
], function(
    pageState,
    webPanels
    ) {

    'use strict';

    module('comment-likes-view', {
        setup: function() {
            sinon.stub(pageState, 'getCurrentUser');

            pageState.getCurrentUser.returns({
                slug: "admin",
                displayName: "admin"
            });
        },
        teardown: function() {
            pageState.getCurrentUser.restore();
        }
    });


    test('Comment wth empty list of likers', function() {
        var context = webPanels.contextProvider({
            comment: {
                id: 5,
                properties: {
                    likedBy: {
                        likers: [],
                        total: 0
                    }
                },
                author: {
                    slug: "user1"
                }
            }
        });
        deepEqual(context, {
            canLike: true,
            userHasLiked: false,
            likerCount: 0,
            otherLiker: null,
            otherLikers: []
        }, "like context with no likers, canlike");
    });

    test('Comment with no likedBy', function() {
        var context = webPanels.contextProvider({
            comment: {
                id: 5,
                properties: {
                },
                author: {
                    slug: "user1"
                }
            }
        });
        deepEqual(context, {
            canLike: true,
            userHasLiked: false,
            likerCount: 0,
            otherLiker: null,
            otherLikers: []
        }, "like context with no likers, canlike");
    });


    test('Comment with no properties', function() {
        var context = webPanels.contextProvider({
            comment: {
                id: 5,
                author: {
                    slug: "user1"
                }
            }
        });
        deepEqual(context, {
            canLike: true,
            userHasLiked: false,
            likerCount: 0,
            otherLiker: null,
            otherLikers: []
        }, "like context with no likers, canlike");
    });

    test('Test author cannot like own comment', function() {
        var context = webPanels.contextProvider({
            comment: {
                id: 5,
                author: {
                    slug: "admin"
                }
            }
        });
        deepEqual(context, {
            canLike: false,
            userHasLiked: false,
            likerCount: 0,
            otherLiker: null,
            otherLikers: []
        }, "like context with no likers, cannot like");
    });

    test('Test author cannot like own comment', function() {
        var context = webPanels.contextProvider({
            comment: {
                id: 5,
                author: {
                    slug: "admin"
                }
            }
        });
        deepEqual(context, {
            canLike: false,
            userHasLiked: false,
            likerCount: 0,
            otherLiker: null,
            otherLikers: []
        }, "like context with no likers, cannot like");
    });

    test('Test that userHasLiked is correctly detected', function() {
        var context = webPanels.contextProvider({
            comment: {
                id: 5,
                properties: {
                    likedBy: {
                        likers: [{
                            slug: "admin"
                        }],
                        total: 1
                    }
                },
                author: {
                    slug: "user1"
                }
            }
        });
        deepEqual(context, {
            canLike: true,
            userHasLiked: true,
            likerCount: 1,
            otherLiker: null,
            otherLikers: []
        }, "like context userHasLiked");
    });

    test('Test one other liker', function() {
        var context = webPanels.contextProvider({
            comment: {
                id: 5,
                properties: {
                    likedBy: {
                        likers: [{
                            slug: "admin"
                        }, {
                            slug: "user2"
                        }],
                        total: 2
                    }
                },
                author: {
                    slug: "user1"
                }
            }
        });
        deepEqual(context, {
            canLike: true,
            userHasLiked: true,
            likerCount: 2,
            otherLiker: {
                slug: "user2"
            },
            otherLikers: [{
                slug: "user2"
            }]
        }, "one other liker");
    });

    test('Test two other liker', function() {
        var context = webPanels.contextProvider({
            comment: {
                id: 5,
                properties: {
                    likedBy: {
                        likers: [{
                            slug: "admin"
                        }, {
                            slug: "user2"
                        }, {
                            slug: "user3"
                        }],
                        total: 3
                    }
                },
                author: {
                    slug: "user1"
                }
            }
        });
        deepEqual(context, {
            canLike: true,
            userHasLiked: true,
            likerCount: 3,
            otherLiker: {
                slug: "user2"
            },
            otherLikers: [{
                slug: "user2"
            }, {
                slug: "user3"
            }]
        }, "two other likers");
    });

    test('Test button condition', function() {
        ok(webPanels.buttonPanelCondition({
            comment: {
                author: {
                    slug: "user1"
                }
            }
        }), "button panel, showing");

        ok(!webPanels.buttonPanelCondition({
            comment: {
                author: {
                    slug: "admin"
                }
            }
        }), "button panel, not showing");
    });

    test('Test button condition', function() {
        ok(webPanels.buttonPanelCondition({
            comment: {
                author: {
                    slug: "user1"
                }
            }
        }), "button panel, showing");

        ok(!webPanels.buttonPanelCondition({
            comment: {
                author: {
                    slug: "admin"
                }
            }
        }), "button panel, not showing");
    });
});
