'use strict';
/**
 * @ngdoc service
 * @name Dealer
 * @description
 *  dealer 관련 기능
 */
app.service('Dealer', function ($rootScope, $http, $q, $window, jwtHelper, Auth) {

  this.login = function(data) {
    var self = this,
      d = $q.defer();

    // 로그인 API 호출
    $http.post('/api/dealer/login', data).success(function(response) {
      if (response.result) {
        var token = jwtHelper.decodeToken(response.token);
        var company = token.company;
        var user = {
          company: company,
          token: response.token,
          optMenuShow: response.opt_menu_show,
          optHelpShow: response.opt_help_show,
          admin: response.admin,
          dealer_username: (JSON.parse(data)).username,
          timestamp: parseInt(+new Date() / 1000, 10)
        };
        Auth.setCurrentUser(user, 'dealer');
      }
      d.resolve(response);
    }).error(function (error) {
      d.reject(error);
    });

    return d.promise;
  };

  this.logout = function() {
    var d = $q.defer();
    Auth.removeCurrentUser();
    d.resolve();
    return d.promise;
  };

  // dealer company 불러오기
  this.getDealerCompany = function() {
    var dealer = JSON.parse($window.localStorage.getItem('hcm_editor_dealer'));

    if (dealer && typeof dealer.company !== 'undefined') {
      return dealer.company;
    } else {
      return '';
    }
  };

  this.getJobs = function() {
    var  d = $q.defer(),
      url = '/api/dealer/get_jobs';

    $http.post(url)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getJob = function(data) {
    var  d = $q.defer(),
      url = '/api/dealer/get_job';

    $http.post(url, data)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getSystemToken = function(data) {
    var  d = $q.defer(),
      url = '/api/dealer/get_system_token';

    $http.post(url, data)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.setUser = function(systemId, token, username, email, dealertoken) {
    var dealer = JSON.parse($window.localStorage.getItem('hcm_editor_dealer')),
      user;

    if (dealertoken && !dealer.dealertoken) {
      dealer.dealertoken = dealer.token;
    }

    dealer.system_id = systemId;
    dealer.token = token;

    Auth.setCurrentUser(dealer, 'dealer');

    if (username && email) {
      user = angular.copy(dealer);
      user.username = username;
      user.email = email;

      Auth.setCurrentUser(user);
    } else {
      Auth.setCurrentUser(dealer);
    }
  };

  this.createFromExisting = function(data) {
    var  d = $q.defer(),
      url = '/api/dealer/room/create_from_existing';

    $http.post(url, data)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.customDevCreate = function(data) {
    var  d = $q.defer(),
      url = '/api/dealer/custom_dev/create';

    $http.post(url, data)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getTemplates = function() {
    var  d = $q.defer(),
      url = '/api/dealer/room/list';

    $http.post(url)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.deleteTemplate = function(id) {
    var  d = $q.defer(),
      url = '/api/dealer/room/delete/' + id;

    $http.post(url)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.templateApplyToNew = function (id, data) {
    var d = $q.defer(),
      url = '/api/dealer/room/apply_to_new/' + id;

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.templateApplyToExisting = function(id, data) {
    var  d = $q.defer(),
      url = '/api/dealer/room/apply_to_existing/' + id;

    $http.post(url, data)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getDevices = function() {
    var  d = $q.defer(),
      url = '/api/dealer/custom_dev/list';

    $http.post(url)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.deleteDevice = function(id) {
    var  d = $q.defer(),
      url = '/api/dealer/custom_dev/delete/' + id;

    $http.post(url)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getDeviceInfo = function(id) {
    var  d = $q.defer(),
      url = '/api/dealer/custom_dev/get_device_info/' + id;

    $http.post(url)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.deviceApply = function(id, data) {
    var  d = $q.defer(),
      url = '/api/dealer/custom_dev/apply/' + id;

    $http.post(url, data)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };
  
  this.createNewTemplate = function(data) {
    var  d = $q.defer(),
      url = '/api/dealer/room/create';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getRoom = function (id) {
    var d = $q.defer(),
      url = '/api/dealer/room/get/' + id;

    $http.post(url)
      .success(function(response) {
        if(response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error( function(error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.copyFromTemplate = function (id, data) {
    var d = $q.defer(),
      url = '/api/dealer/room/copy_from_template/' + id;

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.customDevCreateFromTemplate = function (data) {
    var d = $q.defer(),
      url = '/api/dealer/custom_dev/create_from_template';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.deviceApplyToTemplate = function (id, data) {
    var d = $q.defer(),
      url = '/api/dealer/custom_dev/apply_to_template/' + id;

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.addUserCheck = function (data) {
    var d = $q.defer(),
      url = '/api/dealer/add_user_check';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.addUserFinal = function (data) {
    var d = $q.defer(),
      url = '/api/dealer/add_user_final';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.newEmail = function (data) {
    var d = $q.defer(),
      url = '/api/dealer/new_email';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.newPassword = function (data) {
    var d = $q.defer(),
      url = '/api/dealer/new_password';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getUsers = function () {
    var d = $q.defer(),
      url = '/api/dealer/get_users';

    $http.post(url)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getUserData = function (data) {
    var d = $q.defer(),
      url = '/api/dealer/get_user_data';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.deleteUser = function (data) {
    var d = $q.defer(),
      url = '/api/dealer/delete_user';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.getUserCategories = function () {
    var d = $q.defer(),
      url = '/api/dealer/get_user_categories';

    $http.post(url)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

  this.addUserPrepare = function (data) {
    var d = $q.defer(),
      url = '/api/dealer/add_user_prepare';

    $http.post(url, data)
      .success(function (response) {
        if (response.result) {
          d.resolve(response);
        } else {
          d.reject(response.error);
        }
      })
      .error(function (error) {
        d.reject(error);
      });

    return d.promise;
  };

});