package com.sybotan.android.demo.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.framework.mvvm.model.db.entity.EquipmentEntity;
import com.sybotan.android.demo.R;
import com.sybotan.android.demo.adapter.BaseRecyclerViewAdapter;
import com.sybotan.android.demo.adapter.EquipListAdapter;

import org.jetbrains.annotations.NotNull;

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

/**
 * 选择设备列表
 */

public class EquipListDialog extends Dialog {

    private Submit submit;
    private EditText searchEdt;
    public EquipListAdapter adapter;
    private List<EquipmentEntity> equips = new ArrayList<>();
    private List<EquipmentEntity> itemList = new ArrayList<>();

    public EquipListDialog(@NonNull Context context) {
        super(context, R.style.style_dialog);
        setDisplay(context);
        init(context);
    }

    private void init(Context context) {
        findViewById(R.id.search_delete).setOnClickListener(v -> {
            searchEdt.setText("");
            itemList.clear();
            itemList.addAll(equips);
            adapter.notifyDataSetChanged();
        });
        searchEdt = findViewById(R.id.search_edt);
        searchEdt.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {//修改回车键功能
                if (!TextUtils.isEmpty(searchEdt.getText().toString().trim())) {
                    itemList.clear();
                    for (int i = 0; i < equips.size(); i++) {
                        if ((!TextUtils.isEmpty(equips.get(i).getName()) && equips.get(i).getName().contains(searchEdt.getText().toString())) || (!TextUtils.isEmpty(equips.get(i).getSystemName()) && equips.get(i).getSystemName().contains(searchEdt.getText().toString()))) {
                            itemList.add(equips.get(i));
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            }
            return false;
        });
        RecyclerView recycler = findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new EquipListAdapter(getContext(), itemList);
        recycler.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseRecyclerViewAdapter adapter, View view, int position) {
                if (submit!=null){
                    submit.onSubmit(itemList.get(position));
                    dismiss();
                }
            }
        });
    }

    public void setDisplay(Context context) {
        setContentView(R.layout.dialog_equip_list);
        setProperty();
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
            }
        });
    }

    public void setProperty() {
        Window window = getWindow();//得到对话框的窗口．
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;// 设置对话框的位置．0为中间
        wl.y = 0;
        wl.alpha = 1;// 设置对话框的透明度
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
    }

    public void setList(@NotNull List<EquipmentEntity> equips) {
        this.equips = equips;
        itemList.addAll(equips);
        adapter.notifyDataSetChanged();
    }


    public interface Submit {
        void onSubmit(EquipmentEntity entity);
    }

    public void setOnSubmit(Submit submit) {
        this.submit = submit;
    }

}