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

import android.content.Context;
import android.content.Intent;
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 kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.admin.admin.adapter.AdminMemberListAdapter;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberHrInfo;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberRoot;
import kr.co.hanwha.hifive.admin.network.AdminNetworkUtil;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
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.view.HFSearchMemberEditTextView;

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

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

public class AdminMemberListActivity extends BaseActivity {
    private HFSearchMemberEditTextView mSearchEditTextView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;

    private AdminMemberListAdapter mAdapter;
    private final int REQUEST_ADD_MEMBER = 1001;

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

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

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

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

        mSearchEditTextView = (HFSearchMemberEditTextView) findViewById(R.id.searchMemberEditTextView);
        mSearchEditTextView.setOnSearchEditTextListener(onSearchEditTextListener);

        mAdapter = new AdminMemberListAdapter(this, onAdminMemberListListener);
        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());

        findViewById(R.id.back_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.add_textView).setOnClickListener(onClickListener);

        initData(true);
    }

    public void initData(final boolean needLoading) {
        mSearchEditTextView.setText("");
        getMemberList(this, new HanwhaNetworkCallback<AdminMemberRoot>() {
            @Override
            public void onSuccess(AdminMemberRoot response) {
                if (response != null) {
                    mAdapter.addItems(response.getListMemberHrSrchInfo());
                    mAdapter.notifyDataAll();
                }
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                showAlert(error);
            }

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

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

    public void getMemberList(@NonNull Context context, @NonNull final HanwhaNetworkCallback callback) {
        if (isNetworkConnected(context) == false) {
            callback.onFailure(new HanwhaNetworkError(HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR));
            return;
        }

        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        AdminNetworkUtil networkUtil = new AdminNetworkUtil();
        networkUtil.requestMemberList(context, encryption_member_id, new HanwhaNetworkCallback<AdminMemberRoot>() {
            @Override
            public void onSuccess(AdminMemberRoot response) {
                callback.onSuccess(response);
            }

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

            @Override
            public void onPreExecute() {
                callback.onPreExecute();
            }

            @Override
            public void onPostExecute() {
                callback.onPostExecute();
            }
        });
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.back_imageButton:
                    finish();
                    break;
                case R.id.add_textView:
                    Intent intent = new Intent(AdminMemberListActivity.this, AdminMemberAddActivity.class);
                    startActivityForResult(intent, REQUEST_ADD_MEMBER);
                    break;
            }
        }
    };

    /**************************************************
     * AlarmListContract.OnAlarmListListener
     **************************************************/
    AdminMemberListAdapter.OnAdminMemberListListener onAdminMemberListListener = new AdminMemberListAdapter.OnAdminMemberListListener() {
        @Override
        public void onItemClicked(@NonNull AdminMemberHrInfo adminMemberHrInfo) {
            Intent intent = new Intent(AdminMemberListActivity.this, AdminMemberDetailActivity.class);
            intent.putExtra("member_id", adminMemberHrInfo.getId());
            intent.putExtra("name", adminMemberHrInfo.getName());
            intent.putExtra("company", adminMemberHrInfo.getCompany());
            intent.putExtra("department", adminMemberHrInfo.getDepartment());
            startActivity(intent);
        }
    };

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

    /**************************************************
     * HFSearchMemberEditTextView.OnSearchEditTextListener
     **************************************************/
    HFSearchMemberEditTextView.OnSearchEditTextListener onSearchEditTextListener = new HFSearchMemberEditTextView.OnSearchEditTextListener() {
        @Override
        public void onImeActionSearch(@NonNull HFSearchMemberEditTextView v) {

        }

        @Override
        public void onValueChanged(@NonNull HFSearchMemberEditTextView v) {
            String keyword = v.getText();
            mAdapter.getFilter().filter(keyword);
        }
    };
}
