/*
 * Copyright (c) 2015  Zvooq LTD.
 * Authors: Renat Sarymsakov, Dmitriy Mozgin, Denis Volyntsev.
 *
 * This file is part of Visum.
 *
 * Visum is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Visum is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Visum.  If not, see <http://www.gnu.org/licenses/>.
 */

package io.reist.visum.view;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;

import butterknife.ButterKnife;
import io.reist.visum.ComponentCache;
import io.reist.visum.ComponentCacheProvider;
import io.reist.visum.presenter.VisumPresenter;

/**
 * Created by Defuera on 29/01/16.
 */
public abstract class VisumActivity<P extends VisumPresenter> extends AppCompatActivity implements VisumView<P> {

    private static final String ARG_STATE_COMPONENT_ID = "ARG_STATE_COMPONENT_ID";

    private Long componentId;
    private boolean stateSaved;

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        inject(getComponent());

        setContentView(getLayoutRes());
        ButterKnife.bind(this);

        final P presenter = getPresenter();
        if (presenter != null) {
            presenter.setView(this);
        }

        ready();
    }

    @LayoutRes
    protected abstract int getLayoutRes();

    @SuppressWarnings("unchecked")
    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (!stateSaved) {
            getComponentCache().invalidateComponentFor(this);
        }
        if (getPresenter() != null)
            getPresenter().setView(null);
    }

    private ComponentCache getComponentCache() {
        ComponentCacheProvider application = (ComponentCacheProvider) getApplicationContext();
        return application.getComponentCache();
    }

    //region VisumView

    @Override
    public Long getComponentId() {
        return componentId;
    }

    @Override
    public void setComponentId(Long componentId) {
        this.componentId = componentId;
    }

    @Override
    public Object getComponent() {
        if (getComponentCache() != null) {
            return getComponentCache().getComponentFor(this);
        } else {
            return null;
        }
    }

    //endregion


    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);

        stateSaved = true;

        Bundle bundle = new Bundle();
        bundle.putLong(ARG_STATE_COMPONENT_ID, componentId);
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        if (savedInstanceState != null) {
            componentId = savedInstanceState.getLong(ARG_STATE_COMPONENT_ID);
        }

        stateSaved = false;
    }


}

@@ -820,45 +820,8 @@
 le;%0A
-import android.os.PersistableBundle;%0A
 impo
@@ -2856,46 +2856,8 @@
 tate
-, PersistableBundle outPersistentState
 ) %7B%0A
@@ -2902,28 +2902,8 @@
 tate
-, outPersistentState
 );%0A%0A

fix pr comments