/*
 *      Copyright 2015-2016 GetSocial B.V.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

  var User = require('./User')

  var NATIVE_BRIDGE_NAME = "GetSocialCurrentUser";

  var CurrentUser = function(serializedUser) {
    User.call(this, serializedUser);

    this.onErrorPlugin = function(err) {
        console.log(err);
    }

    this.onSerializeDataReceive = function(data) {
      this.deserialize(JSON.parse(data));
    }
  }

  CurrentUser.prototype.resolveUserIdentityConflict = function(resolution, onSuccess, onFailure) {
    cordova.exec(onSuccess, onFailure, NATIVE_BRIDGE_NAME, "resolveUserIdentityConflict", [resolution]);
  }

  CurrentUser.prototype.setDisplayName = function(displayName, onSuccess, onFailure) {
        var scope = this;
        cordova.exec(function(serializedUser) {
            scope.onSerializeDataReceive(serializedUser);
            onSuccess(scope.displayName());
        }, onFailure, NATIVE_BRIDGE_NAME, "setDisplayName", [displayName]);
  }

  CurrentUser.prototype.setAvatarUrl = function(avatarUrl, onSuccess, onFailure) {
          var scope = this;
          cordova.exec(function(serializedUser) {
              scope.onSerializeDataReceive(serializedUser);
              onSuccess(scope.avatarUrl());
          }, onFailure, NATIVE_BRIDGE_NAME, "setAvatarUrl", [avatarUrl]);
  }

  CurrentUser.prototype.addFbUserIdentity = function(token, onSuccess, onConflict, onFailure) {
          var scope = this;
          cordova.exec(function(data) {
              if(data.isConflict) {
                onConflict();
              } else {
                scope.onSerializeDataReceive(data.serializedUser);
                onSuccess(scope, data.resolveStatus);
              }
          }, onFailure, NATIVE_BRIDGE_NAME, "addFbUserIdentity", [token]);
  }

  CurrentUser.prototype.addGooglePlusUserIdentity = function(token, onSuccess, onConflict, onFailure) {
            var scope = this;
            cordova.exec(function(data) {
                if(data.isConflict) {
                  onConflict();
                } else {
                  scope.onSerializeDataReceive(data.serializedUser);
                  onSuccess(scope, data.resolveStatus);
                }
            }, onFailure, NATIVE_BRIDGE_NAME, "addGooglePlusUserIdentity", [token]);
  }

  CurrentUser.prototype.addGooglePlayUserIdentity = function(token, onSuccess, onConflict, onFailure) {
              var scope = this;
              cordova.exec(function(data) {
                  if(data.isConflict) {
                    onConflict();
                  } else {
                    scope.onSerializeDataReceive(data.serializedUser);
                    onSuccess(scope, data.resolveStatus);
                  }
              }, onFailure, NATIVE_BRIDGE_NAME, "addGooglePlayUserIdentity", [token]);
  }

  CurrentUser.prototype.addUserIdentity = function(userId, token, onSuccess, onConflict, onFailure) {
                var scope = this;
                cordova.exec(function(data) {
                    if(data.isConflict) {
                      onConflict();
                    } else {
                      scope.onSerializeDataReceive(data.serializedUser);
                      onSuccess(scope, data.resolveStatus);
                    }
                }, onFailure, NATIVE_BRIDGE_NAME, "addUserIdentity", [userId, token]);
  }

  CurrentUser.prototype.removeFbUserIdentity = function(onSuccess, onFailure) {
          var scope = this;
          cordova.exec(function(serializedUser) {
              scope.onSerializeDataReceive(serializedUser);
              onSuccess(scope);
          }, onFailure, NATIVE_BRIDGE_NAME, "removeFbUserIdentity", []);
  }

  CurrentUser.prototype.removeGooglePlusUserIdentity = function(onSuccess, onFailure) {
          var scope = this;
          cordova.exec(function(serializedUser) {
              scope.onSerializeDataReceive(serializedUser);
              onSuccess(scope);
          }, onFailure, NATIVE_BRIDGE_NAME, "removeGooglePlusUserIdentity", []);
  }

  CurrentUser.prototype.removeGooglePlayUserIdentity = function(onSuccess, onFailure) {
          var scope = this;
          cordova.exec(function(serializedUser) {
              scope.onSerializeDataReceive(serializedUser);
              onSuccess(scope);
          }, onFailure, NATIVE_BRIDGE_NAME, "removeGooglePlayUserIdentity", []);
  }

  CurrentUser.prototype.removeUserIdentity = function(providerId, onSuccess, onFailure) {
          var scope = this;
          cordova.exec(function(serializedUser) {
              scope.onSerializeDataReceive(serializedUser);
              onSuccess(scope);
          }, onFailure, NATIVE_BRIDGE_NAME, "removeUserIdentity", [providerId]);
  }

  CurrentUser.prototype.reset = function(onSuccess, onFailure) {
          var scope = this;
          cordova.exec(function(serializedUser) {
              scope.onSerializeDataReceive(serializedUser);
              onSuccess(scope);
          }, onFailure, NATIVE_BRIDGE_NAME, "reset", []);
  }

  CurrentUser.prototype.__proto__ = User.prototype;
  CurrentUser.prototype.constructor = CurrentUser;

  module.exports = CurrentUser;
h