'use strict';
/**
 * @ngdoc service
 * @name Registration
 * @description
 *  system 생성 관련 기능
 */
app.service('Registration', function ($http, $q) {
	this.systemKey = null;
	this.email = null;
	this.mac = null;
  this.systemType = null;

  /**
   * base station 의 type 들을 얻기
   * @memberof Registration
   * @returns {Promise} Future object
   */
  this.getModelTypes = function() {
    // base station 의 type 들을 얻는 API 호출
    return $http.post('/api/system/types').then(function(response) {
      return response;
    });
  };

  /**
   * base station type 저장
   * @memberof Registration
   * @param {String} type base station type
   */
  this.setSystemType = function(type) {
    this.systemType = type;
  };

  /**
   * base station type 얻기
   * @memberof Registration
   * @returns {String|null} base station type
   */
  this.getSystemType = function(){
    return this.systemType;
  };

  /**
   * programming key 저장
   * @memberof Registration
   * @param {String} key programming key
   */
	this.setSystemKey = function(key) {
		this.systemKey = key;
	};

  /**
   * programming key 얻기
   * @memberof Registration
   * @returns {String|null} programming key
   */
	this.getSystemKey = function(){
		return this.systemKey;
	};

  /**
   * email 저장
   * @memberof Registration
   * @param {String} email email
   */
	this.setEmail = function(email) {
		this.email = email;
	};

  /**
   * email 얻기
   * @memberof Registration
   * @returns {String|null} email
   */
	this.getEmail = function() {
		return this.email;
	};

  /**
   * base station MAC address 저장
   * @memberof Registration
   * @param {String} mac MAC address
   */
	this.setBStationMac = function(mac) {
		this.mac = mac;
	};

  /**
   * base station MAC address 얻기
   * @memberof Registration
   * @returns {String|null} MAC address
   */
	this.getBStationMac = function() {
		return this.mac;
	};

  /**
   * 사용자 등록시 temporary password 를 요청한 email 로 보내기
   * @memberof Registration
   * @param {String} data base station type/programming key/email json string
   * @returns {Promise} Future object
   */
  this.sendTempPswd = function(data) {
    var d = $q.defer(), 
        self = this;

    // 사용자 등록시 temporary password 를 요청한 email 로 보내기 API 호출
    $http.post('/api/email/register_email', data)
      .success(function(response) {
        if(response.result) {
          d.resolve();
        }
        else {
          d.reject(response.error);
        }
      })
      .error(function(error) {
        d.reject('Please try again');
      });
    
    return d.promise;
  };

  /**
   * 저장된 정보 reset
   * @memberof Registration
   */
	this.clearData = function() {
		this.systemKey = null;
		this.email = null;
		this.mac = null;
	};


});
