/**
 * Copyright (C) 2016 Marc Gonzalez Diez Open Source Project
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package repository.datasource;

import android.content.Context;

import com.mgdiez.data.bean.vo.tweet.StatisticVo;
import com.mgdiez.data.bean.vo.tweet.mapper.StatisticVoMapper;
import com.mgdiez.domain.bean.StatisticBo;

import java.util.List;
import java.util.Random;
import java.util.UUID;

import io.realm.Realm;
import io.realm.RealmResults;
import repository.RealmHelper;
import rx.Observable;

/**
 * Implementation of {@link StatisticDatastore} which represents the local data stored in the
 * mobile.
 */
public class LocalStatisticDatastore implements StatisticDatastore {

    private Realm realm;

    public LocalStatisticDatastore(Context context) {
        this.realm = RealmHelper.getInstance(context);
    }

    /**
     * @return Observable List of Statistics generated by Timeline
     */
    @Override
    public Observable<List<StatisticBo>> getStatisticsTimeline() {
        RealmResults<StatisticVo> statisticVos = realm.where(StatisticVo.class) //Statistics Type
                .equalTo(StatisticVo.TYPE, "TIMELINE") //generated by Timeline
                .findAll(); //get all results

        List<StatisticBo> statisticBos = StatisticVoMapper.toBo(statisticVos); //map from Vo to Bo.

        if (statisticBos != null && !statisticBos.isEmpty()) {
            return Observable.just(statisticBos);
        }
        return Observable.empty();
    }

    /**
     * @return Observable List of Statistics generated by Home Timeline
     */
    @Override
    public Observable<List<StatisticBo>> getStatisticsHomeTimeline() {
        RealmResults<StatisticVo> statisticVos = realm.where(StatisticVo.class) //Statistics Type
                .equalTo(StatisticVo.TYPE, "MY_TWEETS") //generated by Home Timeline
                .findAll(); //get all results
        List<StatisticBo> statisticBos = StatisticVoMapper.toBo(statisticVos); //map from Vo to Bo.

        if (statisticBos != null && !statisticBos.isEmpty()) {
            return Observable.just(statisticBos);
        }
        return Observable.empty();
    }

    /**
     * @return Observable List of Statistics generated by Search
     */
    @Override
    public Observable<List<StatisticBo>> getStatisticsSearch() {
        RealmResults<StatisticVo> statisticVos = realm.where(StatisticVo.class) //Statistics Type
                .equalTo(StatisticVo.TYPE, "SEARCH") //generated by Search
                .findAll(); //get all results
        List<StatisticBo> statisticBos = StatisticVoMapper.toBo(statisticVos); //map from Vo to Bo

        if (statisticBos != null && !statisticBos.isEmpty()) {
            return Observable.just(statisticBos);
        }
        return Observable.empty();
    }

    /**
     * @return Observable List of Statistics generated by Hashtags
     */
    @Override
    public Observable<List<StatisticBo>> getStatisticsHashtags() {
        RealmResults<StatisticVo> statisticVos = realm.where(StatisticVo.class) //Statistics Type
                .equalTo(StatisticVo.TYPE, "HASHTAGS") //generated by Hashtags
                .findAll(); //get all results
        List<StatisticBo> statisticBos = StatisticVoMapper.toBo(statisticVos); //map from Vo to Bo

        if (statisticBos != null && !statisticBos.isEmpty()) {
            return Observable.just(statisticBos);
        }
        return Observable.empty();
    }

    /**
     * @param id of the Statistic to be retrieved
     * @return Observable of Statistic requested.
     */
    @Override
    public Observable<StatisticBo> getStatisticById(long id) {
        StatisticVo statisticVo = realm.where(StatisticVo.class) //Statistic Type
                .equalTo(StatisticVo.ID, id) //with the id needed
                .findFirst(); // Only one result will be aviable
        StatisticBo statisticBo = StatisticVoMapper.toBo(statisticVo); //map from Vo to Bo

        if (statisticBo != null) {
            return Observable.just(statisticBo);
        }
        return Observable.empty();
    }

    /**
     * @param bo to be saved
     * @return Observable of Void
     */
    @Override
    public Observable<Void> persistStatistic(StatisticBo bo) {
        StatisticVo vo = StatisticVoMapper.toVo(bo); //map from Bo to Vo.
        vo.setId(new Random().nextInt()); // generate random Id.
        realm.beginTransaction();
        realm.copyToRealmOrUpdate(vo);
        realm.commitTransaction();

        realm.close();

        return Observable.empty();
    }
}
