import React from 'react';
import assert from 'assert';
import { shallow } from 'enzyme';
import sinon from 'sinon';
import apiClient from 'panoptes-client/lib/api-client';
import { ProjectPageController } from './';

const params = {
  owner: 'test',
  name: 'test'
};

const location = {
  query: {
    workflow: '6'
  }
};

const context = {
  initialLoadComplete: true,
  router: {}
};

const resources = {
  pages: [],
  project_roles: [
    {
      roles: ['owner'],
      links: {
        owner: {
          id: '1'
        }
      }
    },
    {
      roles: ['collaborator'],
      links: {
        owner: {
          id: '2'
        }
      }
    },
    {
      roles: ['tester'],
      links: {
        owner: {
          id: '3'
        }
      }
    }
  ]
};

const project = apiClient.type('projects').create({
  id: '1',
  display_name: 'A test project',
  experimental_tools: [],
  get(type) {
    return Promise.resolve(resources[type]);
  },
  links: {
    active_workflows: [1, 2, 3, 4, 5],
    avatar: { id: '1' },
    background: { id: '1' },
    owner: { id: '1' }
  }
});

const background = apiClient.type('backgrounds').create({
  id: project.links.background.id
});

const owner = apiClient.type('users').create({
  id: project.links.owner.id
});

const projectAvatar = apiClient.type('avatars').create({
  id: project.links.avatar.id
});

const preferences = apiClient.type('project_preferences').create({
  preferences: {
    selected_workflow_id: '6'
  },
  links: {
    project: project.id
  },
  save() {
    Promise.resolve(preferences)
  }
});

describe('ProjectPageController', () => {
  const wrapper = shallow(<ProjectPageController params={params} project={project} location={location} />, { context });
  const controller = wrapper.instance();
  const workflowSpy = sinon.spy(controller, 'getWorkflow');

  beforeEach(() => {
    workflowSpy.reset();
    wrapper.setState({
      loading: false,
      project,
      background,
      owner,
      pages: resources.pages,
      preferences: {},
      projectAvatar,
      projectRoles: resources.project_roles
    });
    wrapper.update();
  });

  it('should fetch a random active workflow by default', () => {
    controller.getSelectedWorkflow(project);
    const selectedWorkflowID = workflowSpy.getCall(0).args[0];
    sinon.assert.calledOnce(workflowSpy);
    assert.notEqual(project.links.active_workflows.indexOf(selectedWorkflowID), -1);
  });

  describe('with a logged-in user', () => {
    beforeEach(() => {
      controller.setupSplits = () => null;
    });

    it('should load the specified workflow for the project owner', () => {
      wrapper.setProps({ user: owner });
      wrapper.setState({ preferences });
      controller.getSelectedWorkflow(project, preferences);
      sinon.assert.calledOnce(workflowSpy);
      sinon.assert.calledWith(workflowSpy, '6', false);
    });

    it('should load the specified workflow for a collaborator', () => {
      const user = apiClient.type('users').create({ id: '2' });
      wrapper.setProps({ user });
      wrapper.setState({ preferences });
      controller.getSelectedWorkflow(project, preferences);
      sinon.assert.calledOnce(workflowSpy);
      sinon.assert.calledWith(workflowSpy, '6', false);
    });

    it('should load the specified workflow for a tester', () => {
      const user = apiClient.type('users').create({ id: '3' });
      wrapper.setProps({ user });
      wrapper.setState({ preferences });
      controller.getSelectedWorkflow(project, preferences);
      sinon.assert.calledOnce(workflowSpy);
      sinon.assert.calledWith(workflowSpy, '6', false);
    });
  });
});

@@ -297,31 +297,10 @@
 y: %7B
-%0A    workflow: '6'%0A  
 %7D%0A
+
 %7D;%0A%0A
@@ -1425,41 +1425,8 @@
 s: %7B
-%0A    selected_workflow_id: '6'%0A  
 %7D,%0A 
@@ -1507,16 +1507,17 @@
 erences)
+;
 %0A  %7D%0A%7D);
@@ -2492,16 +2492,53 @@
 %3E null;%0A
+      location.query.workflow = '6';%0A
     %7D);%0A
@@ -3545,32 +3545,32 @@
 e(workflowSpy);%0A
-
       sinon.asse
@@ -3622,13 +3622,946 @@
 );%0A  %7D);
+%0A%0A  describe('with a workflow saved in preferences', () =%3E %7B%0A    beforeEach(() =%3E %7B%0A      location.query = %7B%7D;%0A      preferences.update(%7B preferences: %7B%7D%7D);%0A      const user = apiClient.type('users').create(%7B id: '4' %7D);%0A      wrapper.setProps(%7B user %7D);%0A    %7D);%0A%0A    it('should try to load the stored workflow', () =%3E %7B%0A      preferences.update(%7B 'preferences.selected_workflow': '4' %7D);%0A      wrapper.setState(%7B preferences %7D);%0A      controller.getSelectedWorkflow(project, preferences);%0A      sinon.assert.calledOnce(workflowSpy);%0A      sinon.assert.calledWith(workflowSpy, '4', true);%0A    %7D);%0A%0A    it('should try to load a stored project workflow', () =%3E %7B%0A      preferences.update(%7B 'settings.workflow_id': '2' %7D);%0A      wrapper.setState(%7B preferences %7D);%0A      controller.getSelectedWorkflow(project, preferences);%0A      sinon.assert.calledOnce(workflowSpy);%0A      sinon.assert.calledWith(workflowSpy, '2', true);%0A    %7D);%0A  %7D);
 %0A%7D);%0A

Add tests for workflows saved in preferences