/*
 * Copyright (c) 2015 SatuSatuDua.
 *
 * 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.
 */

package id.satusatudua.sigap.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;

import java.util.List;

import butterknife.Bind;
import id.satusatudua.sigap.R;
import id.satusatudua.sigap.data.model.ActivityHistory;
import id.satusatudua.sigap.data.model.User;
import id.satusatudua.sigap.presenter.HistoriesPresenter;
import id.satusatudua.sigap.ui.MainActivity;
import id.satusatudua.sigap.ui.adapter.HistoryAdapter;
import id.zelory.benih.ui.fragment.BenihFragment;
import id.zelory.benih.ui.view.BenihRecyclerView;

/**
 * Created on : January 17, 2016
 * Author     : zetbaitsu
 * Name       : Zetra
 * Email      : zetra@mail.ugm.ac.id
 * GitHub     : https://github.com/zetbaitsu
 * LinkedIn   : https://id.linkedin.com/in/zetbaitsu
 */
public class OtherHistoriesFragment extends BenihFragment implements HistoriesPresenter.View,
        SwipeRefreshLayout.OnRefreshListener {
    private static final String KEY_USER = "extra_user";

    @Bind(R.id.recycler_view) BenihRecyclerView recyclerView;
    @Bind(R.id.swipe_layout) SwipeRefreshLayout swipeRefreshLayout;

    private HistoriesPresenter presenter;
    private HistoryAdapter adapter;
    private User user;

    public static OtherHistoriesFragment newInstance(User user) {
        OtherHistoriesFragment fragment = new OtherHistoriesFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_USER, user);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getResourceLayout() {
        return R.layout.fragment_histories;
    }

    @Override
    protected void onViewReady(@Nullable Bundle savedInstanceState) {
        resolveUser(savedInstanceState);

        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
        swipeRefreshLayout.setOnRefreshListener(this);

        adapter = new HistoryAdapter(getActivity(), user);
        recyclerView.setAdapter(adapter);
        recyclerView.setUpAsList();

        presenter = new HistoriesPresenter(this, user);
        if (savedInstanceState == null) {
            new Handler().postDelayed(() -> presenter.loadHistories(), 800);
        } else {
            presenter.loadState(savedInstanceState);
        }
    }

    private void resolveUser(Bundle savedInstanceState) {
        user = getArguments().getParcelable(KEY_USER);

        if (user == null && savedInstanceState != null) {
            user = savedInstanceState.getParcelable(KEY_USER);
        }

        if (user == null) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
            getActivity().finish();
        }
    }

    @Override
    public void showHistories(List<ActivityHistory> histories) {
        adapter.clear();
        adapter.add(histories);
    }

    @Override
    public void showError(String errorMessage) {
        Snackbar snackbar = Snackbar.make(recyclerView, errorMessage, Snackbar.LENGTH_LONG);
        snackbar.getView().setBackgroundResource(R.color.colorAccent);
        snackbar.show();
    }

    @Override
    public void showLoading() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void dismissLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        presenter.saveState(outState);
        super.onSaveInstanceState(outState);
        outState.putParcelable(KEY_USER, user);
    }

    @Override
    public void onRefresh() {
        presenter.loadHistories();
    }
}

@@ -1003,16 +1003,61 @@
 istory;%0A
+import id.satusatudua.sigap.data.model.Case;%0A
 import i
@@ -1147,24 +1147,75 @@
 sPresenter;%0A
+import id.satusatudua.sigap.ui.DetailCaseActivity;%0A
 import id.sa
@@ -2870,16 +2870,329 @@
 sList();
+%0A        adapter.setOnItemClickListener((view, position) -%3E %7B%0A            Case theCase = adapter.getData().get(position).getTheCase();%0A            User reporter = adapter.getData().get(position).getUser();%0A            startActivity(DetailCaseActivity.generateIntent(getActivity(), theCase, reporter));%0A        %7D);
 %0A%0A      

Handle item click in histories page of other user