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

import { ApprovalState } from 'bitbucket/internal/bbui/models/models';
import ReviewerStatus from 'bitbucket/internal/bbui/reviewer-status/reviewer-status';

QUnit.module('bitbucket-reviewer-status', {
    beforeEach: function() {
        this.render = props => {
            const Component = wrap(ReviewerStatus);
            this.component = TestUtils.renderIntoDocument(<Component {...props} />);
            this.el = findDOMNode(this.component);
            this.needsWork = this.el.querySelector('.aui-button:first-child');
            this.approve = this.el.querySelector('.aui-button:last-child');
        };
    },
});

QUnit.test('no buttons are pressed by default', function(assert) {
    this.render({});

    assert.equal(this.needsWork.getAttribute('aria-pressed'), 'false');
    assert.equal(this.approve.getAttribute('aria-pressed'), 'false');
});

QUnit.test('setting needs work status', function(assert) {
    this.render({ status: ApprovalState.NEEDS_WORK });

    assert.equal(this.needsWork.getAttribute('aria-pressed'), 'true');
    assert.equal(this.approve.getAttribute('aria-pressed'), 'false');
});

QUnit.test('setting approve status', function(assert) {
    this.render({ status: ApprovalState.APPROVED });

    assert.equal(this.needsWork.getAttribute('aria-pressed'), 'false');
    assert.equal(this.approve.getAttribute('aria-pressed'), 'true');
});

QUnit.test('unpressed title for approve button', function(assert) {
    this.render({});

    assert.equal(this.approve.getAttribute('original-title'),
                 AJS.I18n.getText('bitbucket.component.reviewer.status.approve.html'));
});

QUnit.test('pressed title for approve button', function(assert) {
    this.render({ status: ApprovalState.APPROVED });

    assert.equal(this.approve.getAttribute('original-title'),
                 AJS.I18n.getText('bitbucket.component.reviewer.status.approve.deselect.html'));
});

QUnit.test('unpressed title for needs work button', function(assert) {
    this.render({});

    assert.equal(this.needsWork.getAttribute('original-title'),
                 AJS.I18n.getText('bitbucket.component.reviewer.status.needs.work.html'));
});

QUnit.test('pressed title for needs work button', function(assert) {
    this.render({ status: ApprovalState.NEEDS_WORK });

    assert.equal(this.needsWork.getAttribute('original-title'),
                 AJS.I18n.getText('bitbucket.component.reviewer.status.needs.work.deselect.html'));
});

QUnit.test('clicking approve fires callback', function(assert) {
    const done = assert.async();
    this.render({
        onStatusClick: status => {
            assert.deepEqual(status, { newStatus: ApprovalState.APPROVED });
            done();
        },
    });

    TestUtils.Simulate.click(this.approve);
});

QUnit.test('toggling approve fires callback', function(assert) {
    const done = assert.async();
    this.render({
        status: ApprovalState.APPROVED,
        onStatusClick: status => {
            assert.deepEqual(status, { newStatus: ApprovalState.UNAPPROVED });
            done();
        },
    });

    TestUtils.Simulate.click(this.approve);
});

QUnit.test('toggling needs work fires callback', function(assert) {
    const done = assert.async();
    this.render({
        status: ApprovalState.NEEDS_WORK,
        onStatusClick: status => {
            assert.deepEqual(status, { newStatus: ApprovalState.UNAPPROVED });
            done();
        },
    });

    TestUtils.Simulate.click(this.needsWork);
});

QUnit.test('clicking needs work fires callback', function(assert) {
    const done = assert.async();
    this.render({
        onStatusClick: status => {
            assert.deepEqual(status, { newStatus: ApprovalState.NEEDS_WORK });
            done();
        },
    });

    TestUtils.Simulate.click(this.needsWork);
});

QUnit.test('clicking from approved to needs work should toggle the state', function(assert) {
    const done = assert.async();
    this.render({
        status: ApprovalState.APPROVED,
        onStatusClick: status => {
            assert.deepEqual(status, { newStatus: ApprovalState.NEEDS_WORK });
            done();
        },
    });

    TestUtils.Simulate.click(this.needsWork);
});
