'use strict';

var angular = require('angular');

require('../modules');

var module = angular.module('devicemanager.services');

module.factory('DeviceLoginRecordsService',function($http, AppConfig) {

  var GetdeviceService = {
    /**
     * 保存登录设备信息
     */
    saveUserLoginOnDeviceInfo: function(userId, userName) {
      var device = window.device, deviceInfo = {};
      //主键
      deviceInfo.devId = '';
      //登录用户userId
      deviceInfo.userId = userId;
      //用户姓名
      deviceInfo.userName = userName;
      //登录方式
      deviceInfo.loginType = 'pwd';
      //终端类型
      deviceInfo.terminalType = 'app';
      //登录时间
      deviceInfo.loginTime = '';
      //设备唯一uuid
      deviceInfo.devSn = device.uuid;
      //手机设备型号
      deviceInfo.devModel = device.model;
      //设备名称
      deviceInfo.devName = device.model;
      //操作系统平台
      deviceInfo.osType = device.platform;
      //对应操作系统的版本
      deviceInfo.osVersion = device.version;
      //浏览器版本号
      deviceInfo.browserVersion = '';
      //app应用的版本号
      deviceInfo.appVersion = AppConfig.version;

      return this.saveRecordOnDeviceWhenRead(deviceInfo);
    },
    saveRecordOnDeviceWhenRead: function (data) {
      return $http({
        method: 'POST',
        url: '/device',
        data: data
      }).then(function(response) {
        return response.data;
      });
    },

    //保存浏览器登录
    saveLoginRecordsOnBrowser: function(userId) {
      var loginOnBrowserInfo = {};
      loginOnBrowserInfo.devId = '';
      loginOnBrowserInfo.userId = userId;
      loginOnBrowserInfo.loginType = 'pwd';
      loginOnBrowserInfo.terminalType = 'browser';
      loginOnBrowserInfo.loginTime = '';
      loginOnBrowserInfo.devSn = 'cdfd024dd60bcbc1';
      loginOnBrowserInfo.devModel = '';
      loginOnBrowserInfo.devName = '';
      //操作系统类型
      loginOnBrowserInfo.osType = this.getOs();
      loginOnBrowserInfo.osVersion = this.getOs();
      //浏览器平台
      loginOnBrowserInfo.browserVersion = '';
      loginOnBrowserInfo.appVersion = AppConfig.version;
      return this.saveRecordOnDeviceWhenRead(loginOnBrowserInfo);
    },
    //获取操作系统类型
    getOs: function() {
      var isWin = (window.navigator.platform === 'Win32') || (window.navigator.platform === 'Windows');
      var isMac = (window.navigator.platform === 'Mac68K') || (window.navigator.platform === 'MacPPC') ||
        (window.navigator.platform === 'Macintosh') || (window.navigator.platform === 'MacIntel');
      if (isMac) {
        return 'Mac';
      }
      var isUnix = (window.navigator.platform === 'X11') && !isWin && !isMac;
      if (isUnix) {
        return 'Unix';
      }
      var isLinux =(String(window.navigator.platform).indexOf('Linux') > -1);
      if (isLinux) {
        return 'Linux';
      }
      if (isWin) {
        return 'Windows';
      }

      return 'otherOS';
    },

    getCurrentDevice: function() {
      var device = window.device;
      return device;
    },

    /**
     * 获取用户登录过的设备
     */
    getUserLoginRecordsOnDevice: function(userId) {
      var url = '/device/' + userId;
      return $http.get(url).then(function(response) {
        return response.data.content;
      });
    },

    /**
     * 查询登录历史记录
     */
    getLoginHistory: function(userId) {
      var url = '/loginLog/' + userId;
      return $http.get(url).then(function(response) {
        return response.data.content;
      });
    },

    /**
     * 绑定与解锁登录设备
     */
    lockDevice: function(userId, lockStatus, devSn) {
      return $http({
        method: 'PUT',
        url: '/device/',
        data: {
          userId: userId,
          lockStatus: lockStatus,
          devSn:devSn
        }
      }).then(function(response) {
        return response.data;
      });
    },

    /**
     * 查询设备信息
     */
    serchDeviceInfo: function(devSn) {
      var url = '/device/find/' + devSn;
      return $http.get(url).then(function(response) {
        return response.data;
      });
    },

    /**
     * 用户启动应用之前
     * 查询设备是否已授权
     */
    searchDeviceIsAuthorized: function(devSn) {
      var url = '/device/authorization/isAuthorized/' + devSn;
      return $http.get(url).then(function(response) {
        return response.data;
      });
    },
    /**
     * 用户提交请求授权信息
     */
    applyAuthorization: function(devSn, userId, userName) {
      return $http({
        url: '/device/authorization/apply',
        method: 'POST',
        data: {
          devSn: devSn,
          userId: userId,
          userName: userName
        }
      }).then(function(response) {
        return response.data;
      });
    },
    /**
     * 管理员
     * 对设备进行授权
     */
    toDeviceAuthorize: function(userId, devSn) {
      return $http({
        method: 'PUT',
        url: '/device/authorization/',
        data: {
          userId: userId,
          devSn: devSn
        }
      }).then(function(response) {
        return response.data;
      });
    },

    applyBind: function(devBindingId) {
      var url = '/device/binding/' + devBindingId;
      return $http({
        method: 'PUT',
        url: url
      }).then(function(response) {
        return response.data;
      });
    },

    rejectBind: function(devBindingId) {
      var url = '/device/binding/notPermit/' + devBindingId;
      return $http({
        method: 'PUT',
        url: url
      }).then(function(response) {
        return response.data;
      });
    },

    unBind: function(devBindingId) {
      var url = '/device/binding/' + devBindingId;
      return $http({
        method: 'DELETE',
        url: url
      }).then(function(response) {
        return response.data;
      });
    },

    /**
     * 获取与人员相关的设备。相关的设备就是申请过绑定的设备。
     *
     * @param {String} userId 人员
     *
     * @returns {Promise<RelatedDevice[]>} 返回相关设备列表的promise。
     */
    getRelatedDevices: function(userId) {
      var url = '/device/related/' + userId;
      return $http.get(url).then(function(response) {
        return response.data;
      });
    },

    getPersonalDevices: function(userId) {
      var url = '/device/binding/personalDeviceInfos/' + userId;
      return $http.get(url).then(function(response) {
        return response.data;
      });
    }
  };
  return GetdeviceService;
});
