package com.philisense.rdgo.db;

import android.content.Context;

import com.philisense.rdgo.bean.response.LoginBean;
import com.philisense.rdgo.bean.response.UserInfoBean;
import com.philisense.rdgo.http.cache.ACache;
import com.philisense.rdgo.util.Constant;

import java.io.FileNotFoundException;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

/**
 * Created by shaop on 2017/12/13.
 */

public class DBFactory {
    static RealmConfiguration realmConfiguration = null;

    public static Realm getRealm() {
        if(realmConfiguration == null) {
            realmConfiguration = new RealmConfiguration.Builder()
                    .name("user_version_1.realm")
                    .schemaVersion(1)
                    .build();
            try {
                Realm.migrateRealm(realmConfiguration, new Migration());
            } catch (FileNotFoundException ignored) {
                // If the Realm file doesn't exist, just ignore.
            }
        }


        return Realm.getInstance(realmConfiguration);
    }

    public static void UpdateUser(LoginBean bean,DBImpl listener) {
        Realm realm = getRealm();

        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                UserDb userDb = new UserDb();
                userDb.setId(bean.getData().get_id());
                userDb.setAccount(bean.getData().getAccount());
                userDb.setDeleted(bean.getData().getDeleted());
                userDb.setFactory(bean.getData().getFactory());
                userDb.setWarehouse(bean.getData().getWarehouse());
                userDb.setWarehouseCode(bean.getData().getWarehouseCode());
                userDb.setIDCard(bean.getData().getIDCard());
                userDb.setIsUse(bean.getData().getIsUse());
                userDb.setSex(bean.getData().getSex());
                userDb.setLine(bean.getData().getLine());
                userDb.setName(bean.getData().getName());
                bgRealm.copyToRealmOrUpdate(userDb);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                listener.loadSuccess();
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                listener.loadFailed(error.getMessage());
            }
        });
    }

    public static void UpdateHomeUser(UserInfoBean bean, DBImpl listener) {
        Realm realm = getRealm();

        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                UserDb userDb = new UserDb();
                userDb.setId(bean.getData().get_id());
                userDb.setAccount(bean.getData().getAccount());
                userDb.setDeleted(bean.getData().getDeleted());
                userDb.setFactory(bean.getData().getFactory());
                userDb.setIDCard(bean.getData().getIDCard());
                userDb.setIsUse(bean.getData().getIsUse());
                userDb.setSex(bean.getData().getSex());
                userDb.setLine(bean.getData().getLine());
                userDb.setName(bean.getData().getName());
                userDb.setWarehouse(bean.getData().getWarehouse());
                userDb.setWarehouseCode(bean.getData().getWarehouseCode());
                bgRealm.copyToRealmOrUpdate(userDb);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                listener.loadSuccess();
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                listener.loadFailed(error.getMessage());
            }
        });
    }


    public static UserDb getUserInfo(Context context) {
        Realm realm = getRealm();

        ACache aCache = ACache.get(context);
        RealmResults<UserDb> result = realm.where(UserDb.class).equalTo("id",aCache.getAsString(Constant.USERID)).findAll();
        if (result.size() > 0) {
            return result.first();
        }else {
            realm.close();
            return null;
        }
    }

}
