package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.DialogTitle;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lcp.arecyclerview.widget.ARecycleView;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.user.UserInfoTool;
import com.tofly.ghrq_route.module.user.controller.UserCallBack;
import com.tofly.ghrq_route.module.user.domain.DepartmentUserBean;
import com.tofly.ghrq_route.common.ui.adapter.MultiUserListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/2/13.
 */

public class MultiUserSelectDailog extends AlertDialog {
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.text_hint1)
    TextView textHint1;
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;

    private Context mContext;

    private MultiUserListAdapter userListAdapter;

    private ClickListenerInterface mClickListenerInterface;

    private String mDepartId;

    public MultiUserSelectDailog(Context context) {
        super(context, R.style.AlertDialog);
        mContext = context;
    }

    public void setDepartment(String departId) {
        mDepartId = departId;
        getAllUsers();
    }

    public interface ClickListenerInterface {
        void doOk(List<DepartmentUserBean> userBeans);
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.mClickListenerInterface = clickListenerInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_user_bind_car);
        ButterKnife.bind(this);

        initView();
        initData();
    }


    private void initView() {
        alertTitle.setText(R.string.user_select_title);
        textHint1.setText(R.string.user_select_second_title);
        button1.setText(android.R.string.ok);
        button2.setText(android.R.string.cancel);
        recyclerView.setEnabled(false);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.height = (int) (d.heightPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    private void initData(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        List<DepartmentUserBean> userList = new ArrayList<>();
        userListAdapter = new MultiUserListAdapter(mContext, userList);
        recyclerView.setAdapter(userListAdapter);
        recyclerView.setRefreshing(true);
        String noData = mContext.getResources().getString(R.string.user_select_empty_data);
        recyclerView.setEmptyViewText(noData);

        getAllUsers();
    }

    private void stopRefreshing() {
        recyclerView.postDelayed(() -> {
            recyclerView.setRefreshing(false);
            recyclerView.hideMoreProgress();
        }, 2000);
    }

    private void resetWindowHeight(List<DepartmentUserBean> userList) {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.height = (int) (d.heightPixels * 0.9); // 高度设置为屏幕的0.6
        if (userList.size() < 4){
            lp.height = (int) (d.heightPixels * 0.5); // 高度设置为屏幕的0.6
        }
        dialogWindow.setAttributes(lp);
    }

    private void getAllUsers() {
        UserInfoTool userInfoTool = UserInfoTool.getInstance();
        userInfoTool.getDepartmentUsers(mContext, mDepartId, new UserCallBack() {
            @Override
            public void onGetDepartmentUserList(boolean isSuccess, int errorCode, String errMsg,
                                                List<DepartmentUserBean> userList) {
                super.onGetDepartmentUserList(isSuccess, errorCode, errMsg, userList);
                stopRefreshing();
                if (isSuccess && userList != null) {
                    if (userList.size() >= 0){
                        recyclerView.setTotal(userList.size());
                        userListAdapter.clear();
                        userListAdapter.cancelSelected();
                        userListAdapter.addAll(userList);

                        resetWindowHeight(userList);
                    }
                } else {
                    ToastUtil.showToast(mContext, errMsg);
                }
            }
        });
    }

    @OnClick({android.R.id.button1, android.R.id.button2})
    public void onClick(View view) {
        switch (view.getId()) {
            /*Button OK*/
            case android.R.id.button1:
                if (mClickListenerInterface != null){
                    List<DepartmentUserBean> userBeans = userListAdapter.getSelectedItems();
                    mClickListenerInterface.doOk(userBeans);
                }
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
            case android.R.id.button2:
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
        }
    }
}
