package com.tofly.ghrq_route.module.emergencyHandle.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.appcompat.widget.AppCompatSpinner;

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
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.DepartmentBean;
import com.tofly.ghrq_route.module.user.domain.DepartmentUserBean;
import com.tofly.ghrq_route.common.util.SnackBarUtil;
import com.tofly.ghrq_route.common.util.SpinnerUtils;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemSelected;

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

public class EmergencyScheduleAssignReportModuleLayout extends LinearLayout {
    @BindView(R.id.spinner_handle_depart)
    AppCompatSpinner spinnerHandleDepart;
    @BindView(R.id.spinner_handle_user)
    AppCompatSpinner spinnerHandleUser;
    @BindView(R.id.edit_handle_note)
    EditText editHandleNote;

    private Context mContext;

    private boolean isEditable;

    private List<DepartmentBean> mDepartmentList;

    private List<DepartmentUserBean> mUserList;

    private int indexHandleDepart = -1;

    private int indexHandleUser = -1;

    public EmergencyScheduleAssignReportModuleLayout(Context context) {
        this(context, null);
    }

    public EmergencyScheduleAssignReportModuleLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmergencyScheduleAssignReportModuleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        initView();
        initData();
        initListener();
    }

    private void initView() {
        inflate(mContext, R.layout.layout_emergency_schedule_assign_report_module, this);
        ButterKnife.bind(this);
    }

    private void initData() {
        getDepartments();
    }

    private void initListener() {

    }

    @OnItemSelected({R.id.spinner_handle_depart, R.id.spinner_handle_user})
    public void onItemSelected(AdapterView<?> parent, int position){
        switch (parent.getId()) {
            case R.id.spinner_handle_depart:
                indexHandleDepart = position;
                DepartmentBean departmentBean = mDepartmentList.get(position);
                if (departmentBean != null) {
                    getDepartmentUsers(departmentBean.getId());
                }
                break;
            case R.id.spinner_handle_user:
                indexHandleUser = position;
                break;
        }
    }

    private void getDepartments() {
        View parent = (View) getParent();
        UserInfoTool userInfoTool = UserInfoTool.getInstance();
        userInfoTool.getDepartments(mContext, false, new UserCallBack(){
            @Override
            public void onGetDepartmentList(boolean isSuccess, int errorCode, String errMsg, List<DepartmentBean> departmentList) {
                super.onGetDepartmentList(isSuccess, errorCode, errMsg, departmentList);
                if (isSuccess) {
                    mDepartmentList = departmentList;
                    setDepartmentAdapter();
                }else {
                    SnackBarUtil.showSnackAlways(parent, "获取部门失败", "重试", view -> getDepartments());
                }
            }
        });
    }

    private void setDepartmentAdapter(){
        if (mDepartmentList != null) {
            List<String> typeNameList = new ArrayList<>();
            for (DepartmentBean departmentBean : mDepartmentList) {
                typeNameList.add(departmentBean.getName());
            }
            SpinnerUtils.setAdapter(mContext, spinnerHandleDepart, typeNameList);
        }
    }

    private void getDepartmentUsers(String departId) {
        View parent = (View) getParent();
        UserInfoTool userInfoTool = UserInfoTool.getInstance();
        userInfoTool.getDepartmentUsers(mContext, departId, new UserCallBack(){
            @Override
            public void onGetDepartmentUserList(boolean isSuccess, int errorCode, String errMsg,
                                                List<DepartmentUserBean> userList) {
                super.onGetDepartmentUserList(isSuccess, errorCode, errMsg, userList);
                if (isSuccess) {
                    mUserList = userList;
                    setUserAdapter();
                }else {
                    SnackBarUtil.showSnackAlways(parent, "获取部门用户失败", "重试",
                            view -> getDepartmentUsers(departId));
                }
            }
        });
    }

    private void setUserAdapter(){
        List<String> userNameList = new ArrayList<>();
        for (DepartmentUserBean userBean : mUserList) {
            userNameList.add(userBean.getRealName());
        }
        SpinnerUtils.setAdapter(mContext, spinnerHandleUser, userNameList);
    }

    public boolean isValid() {
        /*处理人员*/
        if (indexHandleDepart == -1 || mDepartmentList.isEmpty()) {
            ToastUtil.showToast(mContext, mContext.getString(R.string.emergency_schedule_assign_empty_depart));
            return false;
        }

        if (indexHandleUser == -1 || mUserList.isEmpty()) {
            ToastUtil.showToast(mContext, mContext.getString(R.string.emergency_schedule_assign_empty_user));
            return false;
        }

        /*处理意见*/
        if (editHandleNote.getText().toString().isEmpty()) {
            editHandleNote.requestFocus();
            ToastUtil.showToast(mContext, mContext.getString(R.string.emergency_schedule_assign_empty_note));
            return false;
        }

        return true;
    }

    public void collectData(EmergencyHandleData handleData) {
        /*处理人员*/
        DepartmentBean departBean = mDepartmentList.get(indexHandleDepart);
        handleData.setDispatchDisposeDeptId(departBean.getId());

        DepartmentUserBean userBean = mUserList.get(indexHandleUser);
        handleData.setDispatchDisposeUserId(userBean.getId());

        /*处理意见*/
        String note = editHandleNote.getText().toString();
        handleData.setDispatchDisposeNote(note);
    }
}
