import AJS from 'aui';
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import { findDOMNode } from 'react-dom';

import models from 'bitbucket/internal/bbui/models/models';
import { SelfAction, ApprovalState } from 'bitbucket/internal/bbui/models/models';
import SelfReviewer from 'bitbucket/internal/bbui/self-reviewer/self-reviewer';

import mockModels from 'mocks/models';

const userApproved = mockModels.participant({
    role: models.ParticipantRole.REVIEWER,
    state: ApprovalState.APPROVED,
}, 'name');
const userNeedsWork = mockModels.participant({
    role: models.ParticipantRole.REVIEWER,
    state: ApprovalState.NEEDS_WORK,
}, 'name');
const userUnapproved = mockModels.participant({
    role: models.ParticipantRole.REVIEWER,
    state: ApprovalState.UNAPPROVED,
}, 'name');

QUnit.module('bitbucket-self-reviewer', {
    beforeEach: function() {
        this.render = props => {
            this.component = TestUtils.renderIntoDocument(<SelfReviewer {...props} />);
            this.el = findDOMNode(this.component);
            this.approveBadge = this.el.querySelector('.badge.approved');
            this.needsWorkBadge = this.el.querySelector('.badge.needs-work');
            this.removeSelf = this.el.querySelector('.remove-self');
            this.avatarWrapper = this.el.querySelector('.user-avatar');
            this.avatar = this.el.querySelector('img');
        };
    },
});

QUnit.test('Add Self button exists', function(assert) {
    this.render({
        removeSelfModalId: 'foo',
        onSelfClick: () => {},
    });

    assert.ok(this.el.querySelector('.add-self'), 'expected \'.add-self\' class to be present');
});

QUnit.test('rendering content', function(assert) {
    const removeSelfModalId = 'foo';
    this.render({
        removeSelfModalId: removeSelfModalId,
        onSelfClick: () => {},
        currentUserAsReviewer: userUnapproved,
    });

    assert.ok(this.el.classList.contains('self-avatar'), 'expected \'.self-avatar\' class to be present');
    assert.equal(this.avatarWrapper.dataset.username, userUnapproved.user.name, 'expected usernames to match');
    assert.equal(this.needsWorkBadge.textContent, AJS.I18n.getText('bitbucket.component.avatar.badge.needs.work'));
    assert.equal(this.removeSelf.getAttribute('title'), AJS.I18n.getText('bitbucket.component.self.reviewer.remove'));
    assert.equal(this.approveBadge.textContent, AJS.I18n.getText('bitbucket.component.avatar.badge.approved'));
    assert.equal(this.removeSelf.firstChild.getAttribute('aria-controls'), removeSelfModalId, 'expected aria-controls to match removeSelfModalId');
    assert.equal(this.el.querySelector('aui-inline-dialog').id, removeSelfModalId, 'expected aui-inline-dialog id to match removeSelfModalId');
});

QUnit.test('clicking add self fires callback', function(assert) {
    const done = assert.async();
    this.render({
        removeSelfModalId: 'bar',
        onSelfClick: action => {
            assert.equal(action, SelfAction.ADD_SELF);
            done();
        },
    });
    TestUtils.Simulate.click(this.el.querySelector('button.aui-button'));
});

QUnit.test('approved state', function(assert) {
    this.render({
        removeSelfModalId: 'foo',
        onSelfClick: () => {},
        currentUserAsReviewer: userApproved,
    });

    assert.ok(this.needsWorkBadge.classList.contains('badge-hidden'), 'expected Needs Work badge to be hidden');
    assert.notOk(this.approveBadge.classList.contains('badge-hidden'), 'expected Approved badge to be visible');
});

QUnit.test('needs work state', function(assert) {
    this.render({
        removeSelfModalId: 'foo',
        onSelfClick: () => {},
        currentUserAsReviewer: userNeedsWork,
    });

    assert.notOk(this.needsWorkBadge.classList.contains('badge-hidden'), 'expected Needs Work badge to be visible');
    assert.ok(this.approveBadge.classList.contains('badge-hidden'), 'expected Approved badge to be hidden');
});

