const Realm = require('realm');
import Migrations from './Mirgrations';
import uuid from 'react-native-uuid';
import {
  UserSchemea,
  ApartmentSchemea,
  DocSchemea,
  NotificationSchemea,
} from '../../services/realm/schemeas';
let realm = new Realm({
  schema: [UserSchemea, ApartmentSchemea, DocSchemea, NotificationSchemea],
});

const queryUser = () => {
  return realm.objects('User');
};

const modifyApartment = data => {};

const saveEdit = index => {
  var data = this.store.apartmentData;
  let user = realm.objects('User');
  try {
    realm.write(() => {
      let newApartment = {
        id: data.id,
        createDate: data.createDate,
        modifyDate: new Date(),
        name: data.name,
        address: data.address,
        avatar: this.store.avatar,
        signContractDate: data.signContractDate,
        endContractDate: data.endContractDate,
        rent: parseInt(data.rent),
        deposit: parseInt(data.deposit),
        leaseMonth: parseInt(data.leaseMonth),
        country: data.country,
        callingCode: data.callingCode,
        rentNotification: data.rentNotification,
        endContractNotification: data.endContractNotification,
        remark: this.store.apartmentDataRemark,
        doc: this.store.apartmentDataDoc,
      };
      user[0].apartment[index] = newApartment;
      //  user.gender = "M"
    });
  } catch (e) {
    console.log('Error on creation ' + e);
  }
};

const createUser = memberType => {
  try {
    realm.write(() => {
      realm.create('User', {
        memberType: memberType,
        id: 1,
        name: 'new',
        signup: false,
        login: false,
        username: 'null',
        password: 'null',
        email: 'null',
        gender: 'null',
        apartment: [],
      });
    });
  } catch (error) {
    console.log(error);
  }
};

export {queryUser, createUser};
