package ir.peivandyar.database.viewmodel;

/*
 * Copyright (C) 2017 Google Inc.
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;

import java.util.List;

import ir.peivandyar.database.entity.SchoolInfo;
import ir.peivandyar.database.repository.SchoolInfoRepository;

/**
 * View Model to keep a reference to the schoolInfo repository and
 * an up-to-date list of all schoolInfos.
 */

public class SchoolInfoViewModel extends AndroidViewModel {

    private SchoolInfoRepository mRepository;
    // Using LiveData and caching what getAlphabetizedSchoolInfos returns has several benefits:
    // - We can put an observer on the data (instead of polling for changes) and only update the
    //   the UI when the data actually changes.
    // - Repository is completely separated from the UI through the ViewModel.
    private LiveData<List<SchoolInfo>> mAllSchoolInfos;

    public SchoolInfoViewModel(Application application) {
        super(application);
        mRepository = new SchoolInfoRepository(application);
        mAllSchoolInfos = mRepository.getAllSchoolInfos();
    }

    public LiveData<List<String>> getProvinces(){
        return mRepository.getProvinces();
    }

    public LiveData<List<String>> getTowns(){
        return mRepository.getTowns();
    }

    public LiveData<List<Integer>> getRegions(){
        return mRepository.getRegions();
    }

    public void insertAll(List<SchoolInfo> schoolInfos){
        mRepository.insertAll(schoolInfos);
    }

    public LiveData<List<SchoolInfo>> getAllSchoolInfos() { return mAllSchoolInfos; }

    public void insert(SchoolInfo schoolInfo) { mRepository.insert(schoolInfo); }
}