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

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.text.TextUtils;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.admin.AdminInputDialog;
import kr.co.hanwha.hifive.admin.admin.adapter.AdminMemberDetailAdapter;
import kr.co.hanwha.hifive.admin.admin.data.AdminDataRepostiory;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberDetailRoot;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.BaseData;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
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;

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

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

    private AdminMemberDetailAdapter mAdapter;

    private int mMember_id = 0;
    private String mName = "";

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

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

        mAdapter = new AdminMemberDetailAdapter(this, onAdminMemberDetailListener);

        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);
        mName = getIntent().getStringExtra("name");
        String company = getIntent().getStringExtra("company");
        String department = getIntent().getStringExtra("department");
        setTitle(mName + " | " + company + " | " + department);

        initData(true);
    }

    public void initData(final boolean needLoading) {
        AdminDataRepostiory.getInstance().getMemberDetail(this, mMember_id, new HanwhaNetworkCallback<AdminMemberDetailRoot>() {
            @Override
            public void onSuccess(AdminMemberDetailRoot response) {
                if (response != null) {
                    mAdapter.addItems(response.getListMemberHrInfo());
                    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);
            }
        });
    }

    /**
     * Reset password
     */
    public void doResetPsssword(@NonNull String password) {
        AdminDataRepostiory.getInstance().requestResetPassword(this, mMember_id, password, new HanwhaNetworkCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData response) {
                showToast("비밀번호가 변경되었습니다.");
                initData(true);
            }

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

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

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

    /**
     * Reset Simple password
     */
    public void doResetSimplePsssword() {
        AdminDataRepostiory.getInstance().requestResetSimplePassword(this, mMember_id, new HanwhaNetworkCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData response) {
                showToast("간편 비밀번호가 초기화되었습니다.");
                initData(true);
            }

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

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

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

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

    /**************************************************
     * AdminMemberDetailAdapter.OnAdminMemberDetailListener
     **************************************************/
    AdminMemberDetailAdapter.OnAdminMemberDetailListener onAdminMemberDetailListener = new AdminMemberDetailAdapter.OnAdminMemberDetailListener() {

        @Override
        public void onTaskListClicked() {
            Intent intent = new Intent(AdminMemberDetailActivity.this, AdminMemberTaskListActivity.class);
            intent.putExtra("member_id", mMember_id);
            intent.putExtra("name", mName);
            startActivity(intent);
        }

        @Override
        public void onResetPasswordClicked() {
            new AdminInputDialog(AdminMemberDetailActivity.this, AdminInputDialog.ADMIN_TYPE.RESET_PASSWORD, new AdminInputDialog.OnDialogCallback() {
                @Override
                public void onPositive(@NonNull String message) {
                    if (TextUtils.isEmpty(message)) {
                        showToast("비밀번호를 입력해주세요.");
                    } else {
                        doResetPsssword(message);
                    }
                }

                @Override
                public void onWrongPassword() {
                    showToast("Wrong password");
                }
            }).show();
        }

        @Override
        public void onResetSimplePasswordClicked() {
            showAlert("간펴비밀번호", "간편비밀번호를 초기화 하시겠습니까?", new HFDefaultDialog.OnDialogCallback() {
                @Override
                public void onNegative() {

                }

                @Override
                public void onPositive() {
                    doResetSimplePsssword();
                }
            });
        }
    };
}
