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

import android.os.Bundle;
import android.support.annotation.NonNull;
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 android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.admin.admin.adapter.AdminMemberTaskListAdapter;
import kr.co.hanwha.hifive.admin.network.AdminNetworkUtil;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberTaskData;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberTaskRoot;
import kr.co.hanwha.hifive.base.BaseActivity;
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;
import kr.co.hanwha.hifive.util.HFActivityUtil;

import static kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkUtil.isNetworkConnected;

/**
 * Created by sanghoonkwon on 2018. 3. 21..
 */

public class AdminMemberTaskListActivity extends BaseActivity {
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private TextView mTextViewEmpty;

    private AdminMemberTaskListAdapter mAdapter;

    private int mMember_id = 0;

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

    @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");
        mMember_id = getIntent().getIntExtra("member_id", 0);
        mAdapter = new AdminMemberTaskListAdapter(name, onAdminTaskListListener);

        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());

        mTextViewEmpty = (TextView) findViewById(R.id.empty_textView);


        initData(true);
    }

    private void setEmptyView(boolean empty) {
        if (empty) {
            mTextViewEmpty.setVisibility(View.VISIBLE);
            mSwipeRefreshLayout.setVisibility(View.GONE);
        } else {
            mTextViewEmpty.setVisibility(View.GONE);
            mSwipeRefreshLayout.setVisibility(View.VISIBLE);
        }
    }

    public void initData(final boolean needLoading) {
        if (isNetworkConnected(this) == false) {
            showAlert(new HanwhaNetworkError(HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR));
            return;
        }

        AdminNetworkUtil networkUtil = new AdminNetworkUtil();
        networkUtil.requestMemberTaskList(this, mMember_id, new HanwhaNetworkCallback<AdminMemberTaskRoot>() {
            @Override
            public void onSuccess(AdminMemberTaskRoot response) {
                if (response != null) {
                    mAdapter.addItems(response.getListMyTasksInfo());
                    mAdapter.notifyDataAll();

                    setEmptyView(response.getListMyTasksInfo().size() == 0);
                }
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                if (error != null && error.getStatusCode() == -2) {
                    setEmptyView(true);
                } else
                    showAlert(error);
            }

            @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);
        }
    };

    /**************************************************
     * AdminMemberTaskListAdapter.OnAdminTaskListListener
     **************************************************/
    AdminMemberTaskListAdapter.OnAdminTaskListListener onAdminTaskListListener = new AdminMemberTaskListAdapter.OnAdminTaskListListener() {
        @Override
        public void onTaskClicked(@NonNull AdminMemberTaskData data) {
            HFActivityUtil.startTaskDetailActivity(AdminMemberTaskListActivity.this, data.getTask_id(), 0, 0);
        }
    };
}
