/// <reference path="./../typings/index.d.ts" />

import 'es6-shim';
import {Plugin, Cordova} from './../src/plugins/plugin';

declare const window: any;
window.plugins = {
  test: {}
};

const testPluginMeta = {
  plugin: 'cordova-plugin-test',
  pluginRef: 'plugins.test',
  repo: 'https://github.com/apache/cordova-plugin-test',
  platforms: ['Android', 'iOS']
};

describe('plugin', () => {

  it('sync method', () => {

    window.plugins.test.syncMethod = () => {
      return 'syncResult';
    };

    @Plugin(testPluginMeta)
    class Test {

      @Cordova({
        sync: true
      })
      static syncMethod(arg: any): boolean { return; };

    }

    const spy = spyOn(window.plugins.test, 'syncMethod').and.callThrough();
    const result = Test.syncMethod('foo');
    expect(result).toEqual('syncResult');
    expect(spy).toHaveBeenCalledWith('foo', undefined, undefined);

  });

  it('normal order callback', done => {

    window.plugins.test.normalOrderCallback = (args, success, error) => {
      success('normalOrderCallback');
    };

    @Plugin(testPluginMeta)
    class Test {
      @Cordova()
      static normalOrderCallback(args: any): Promise<any> { return; }
    }

    const spy = spyOn(window.plugins.test, 'normalOrderCallback').and.callThrough();
    Test.normalOrderCallback('foo').then(result => {
      expect(result).toEqual('normalOrderCallback');
      done();
    });
    expect(spy.calls.mostRecent().args[0]).toEqual('foo');

  });

  it('reverse order callback', done => {

    window.plugins.test.reverseOrderCallback = (success, error, args) => {
      success('reverseOrderCallback');
    };

    @Plugin(testPluginMeta)
    class Test {

      @Cordova({
        callbackOrder: 'reverse'
      })
      static reverseOrderCallback(args: any): Promise<any> { return; }

    }

    const spy = spyOn(window.plugins.test, 'reverseOrderCallback').and.callThrough();
    Test.reverseOrderCallback('foo').then(result => {
      expect(result).toEqual('reverseOrderCallback');
      done();
    });
    expect(spy.calls.mostRecent().args[2]).toEqual('foo');

  });

  it('node style callback', done => {

    window.plugins.test.nodeStyleCallback = (args, done) => {
      done(null, 'nodeStyleCallback');
    };

    @Plugin(testPluginMeta)
    class Test {

      @Cordova({
        callbackStyle: 'node'
      })
      static nodeStyleCallback(args: any): Promise<any> { return; }

    }

    const spy = spyOn(window.plugins.test, 'nodeStyleCallback').and.callThrough();
    Test.nodeStyleCallback('foo').then(result => {
      expect(result).toEqual('nodeStyleCallback');
      done();
    });
    expect(spy.calls.mostRecent().args[0]).toEqual('foo');

  });

  it('object style callback', done => {

    window.plugins.test.objectStyleCallback = (args, {success}) => {
      success('objectStyleCallback');
    };

    @Plugin(testPluginMeta)
    class Test {

      @Cordova({
        callbackStyle: 'object',
        successName: 'success',
        errorName: 'error'
      })
      static objectStyleCallback(args: any): Promise<any> { return; }

    }

    const spy = spyOn(window.plugins.test, 'objectStyleCallback').and.callThrough();
    Test.objectStyleCallback('foo').then(result => {
      expect(result).toEqual('objectStyleCallback');
      done();
    });
    expect(spy.calls.mostRecent().args[0]).toEqual('foo');

  });

});