package kr.co.hanwha.hifive.admin.admin;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.MainOKRListAdapter;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.ProjectCampaignData;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.dialog.HFSingleDialog;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;

public class AdminMemberOkrListActivity extends BaseActivity {
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;

    private MainOKRListAdapter mAdapter;

    private int mMember_id = 0;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_main_okr;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return BaseToolbarType.ONLY_BACK;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.RIGHT;
    }

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

        String name = getIntent().getStringExtra("name");
        setTitle(name + " " + getString(R.string.main_okr_title));

        mAdapter = new MainOKRListAdapter();
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(onRefreshListener);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mMember_id = getIntent().getIntExtra("member_id", 0);

        initData(true);
    }

    public void setEmptyView(boolean isEmpty) {
        if (isEmpty) {
            findViewById(R.id.empty_textView).setVisibility(View.VISIBLE);
            findViewById(R.id.swipeRefreshLayout).setVisibility(View.GONE);
        } else {
            findViewById(R.id.empty_textView).setVisibility(View.GONE);
            findViewById(R.id.swipeRefreshLayout).setVisibility(View.VISIBLE);
        }
    }

    public void initData(boolean needLoading) {
        ArrayList<Integer> listMemberInfo = new ArrayList<>();
        listMemberInfo.add(mMember_id);

        TaskDataRepository.getInstance().requestCampaignListByMemberId(this, listMemberInfo, new HanwhaNetworkCallback<ArrayList<ProjectCampaignData>>() {
            @Override
            public void onSuccess(ArrayList<ProjectCampaignData> response) {
                mAdapter.addItems(response);
                mAdapter.notifyDataAll();

                setEmptyView(response == null || response.size() == 0);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                showAlert(error, new HFSingleDialog.OnDialogCallback() {
                    @Override
                    public void onPositive() {
                        finish();
                    }
                });
            }

            @Override
            public void onPreExecute() {
                showLoadingDialog(true);
            }

            @Override
            public void onPostExecute() {
                showLoadingDialog(false);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    /**************************************************
     * SwipeRefreshLayout.OnRefreshListener
     **************************************************/
    SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            initData(false);
        }
    };
}
