import {observable, action, computed} from 'mobx';

export default class User {

  @observable userGeolocation = {
    ip: "",
    country_code: "",
    country_name: "",
    region_code: "",
    region_name: "",
    city: "",
    zip_code: "",
    time_zone: "",
    latitude: "",
    longitude: "",
    metro_code: "",
    timezone_city: ""
  };
  @observable userList = [];
  @observable postDataAdmin = {
    name: '',
    phone_number: '',
    email: '',
    address: '',
    id: '',
    created_at: '',
    user_id: '',
  };

  constructor(context) {
    this.http = context.http;
    this.context = context;
  }

  @action
  getUserGeolocation() {
    fetch('https://freegeoip.net/json/', {headers: {}}).then((res) => {
      return res.json();
    }).then((data) => {
      this.userGeolocation = data;
      this.userGeolocation.timezone_city = (data.time_zone) ? data.time_zone.split('/')[1] : data.time_zone;
    });
  }

  @action
  getAllUser() {
    this.http.get(`agents`).then(res => {
      res.map(item => {
        item.role = "Agent";
      });
      this.userList = res;
    });
    this.http.get(`customers`).then(res => {
      res.map(item => {
        item.role = "Customer";
        this.userList.push(item);
      });
    });
    this.http.get(`admins`).then(res => {
      res.map(item => {
        item.role = "Admin";
        this.userList.push(item);
      });
    });
  }

  // @action
  // postCustomer(){
  //     this.http.post(`customers`, this.postData).then(res => {
  //         return res;
  //     });
  //     this.postData = {};
  // }

  // @action
  // postAgent(){
  //     this.http.post(`agents`, this.postData).then(res => {
  //         return res;
  //     });
  //     this.postData = {};
  // }

  @action
  postAdmin() {
    return this.http.post(`admins`, this.postDataAdmin).then(res => {
      return res;
    });
  }

  @computed
  get isUserEmpty() {
    if (this.userList.length > 0) {
      return false;
    }
    return true;
  }

}
