package com.sybotan.android.demo.view;

/**
 * Created by cunwen on 16/3/25.
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

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


import com.framework.mvvm.model.db.entity.EquipmentEntity;
import com.framework.mvvm.model.db.entity.ObjectEntity;
import com.sybotan.android.demo.R;
import com.sybotan.android.demo.activities.BaseActivity;
import com.sybotan.android.demo.adapter.PersonalSelectSingleAdapter;
import com.sybotan.android.demo.retrofit.Constant;

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


public class SelectListSinglePopupWindow extends PopupWindow {
    DisplayMetrics dm;

    private TextView title;
    private Button btn1, btn_cancel;
    private RecyclerView recyclerview;
    private LinearLayout searchLinear;
    private EditText searchEdt;
    private ImageView searchDelete;
    private View mMenuView;
    private List<ObjectEntity> itemList = new ArrayList<>();

    private Context context;
    private static final long DISMISSDELAYED = 2000;

    private Animation inAnim;
    private Animation outAnim;

    PersonalSelectSingleAdapter adapter;
    public ArrayList<String> list = new ArrayList<String>();
    public int position = -1;
    private List<ObjectEntity> dataList = new ArrayList<>();

    public SelectListSinglePopupWindow(Context context) {
        super(context);
        this.context = context;

        dm = new DisplayMetrics();
        ((BaseActivity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_list_choice, null);
        title = (TextView) mMenuView.findViewById(R.id.title);
        btn1 = (Button) mMenuView.findViewById(R.id.btn1);
        btn1.setVisibility(View.GONE);
        btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
        searchLinear = (LinearLayout) mMenuView.findViewById(R.id.search_linear);
        searchEdt = (EditText) mMenuView.findViewById(R.id.search_edt);
        searchDelete = (ImageView) mMenuView.findViewById(R.id.search_delete);
        searchLinear.setVisibility(View.VISIBLE);
        //取消按钮
        btn_cancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
                mMenuView.startAnimation(outAnim);
            }
        });
        //设置按钮监听
//        btn1.setOnClickListener(itemsOnClick);
        btn1.setTag(0);

        recyclerview = (RecyclerView) mMenuView.findViewById(R.id.recyclerview);

        recyclerview.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerview.setFocusable(false);
        recyclerview.setVerticalScrollBarEnabled(true);
        recyclerview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        adapter = new PersonalSelectSingleAdapter();
        adapter.setOnItemClickListener(new PersonalSelectSingleAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(itemList.get(position));
                }
                dismiss();
            }
        });
        recyclerview.setAdapter(adapter);
        searchDelete.setOnClickListener(v -> {
            searchEdt.setText("");
            itemList.clear();
            itemList.addAll(dataList);
            adapter.notifyDataSetChanged();
        });
        searchEdt.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {//修改回车键功能
                if (!TextUtils.isEmpty(searchEdt.getText().toString().trim())) {
                    String s = searchEdt.getText().toString().trim();
                    itemList.clear();
                    for (int i = 0; i < dataList.size(); i++) {
                        if ((!TextUtils.isEmpty(dataList.get(i).getLocalId()) && Objects.requireNonNull(dataList.get(i).getLocalId()).contains(s)) || (!TextUtils.isEmpty(dataList.get(i).getLocalName()) && Objects.requireNonNull(dataList.get(i).getLocalName()).contains(s)) || dataList.get(i).getClassCode().contains(s)) {
                            itemList.add(dataList.get(i));
                        }
                    }
                    adapter.setItems(itemList, context, position, Constant.TYPE2);
                    adapter.notifyDataSetChanged();
                }
            }
            return false;
        });

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景

        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });


    }

    public void setBtn(Boolean flag, String textBtn1) {
        if (flag) {
            btn1.setVisibility(View.VISIBLE);
            btn1.setText(textBtn1);
        } else {
            btn1.setVisibility(View.GONE);
        }
    }

    public void setBtnbottom(Boolean flag, String textBtn1) {
        if (flag) {
            btn_cancel.setVisibility(View.VISIBLE);
            btn_cancel.setText(textBtn1);
        } else {
            btn_cancel.setVisibility(View.GONE);
        }
    }

    public void setTitle(Boolean flag, String text) {
        adapter.setFlag(flag);
        if (flag) {
            title.setVisibility(View.VISIBLE);
            title.setText(text);
            btn1.setGravity(Gravity.START);
        } else {
            title.setVisibility(View.GONE);
            btn1.setGravity(Gravity.CENTER);
        }
    }

    public void setList(List<ObjectEntity> list, String type) {
        this.dataList = list;
        itemList.clear();
        itemList.addAll(dataList);
        adapter.setItems(itemList, context, position, type);
        LayoutParams mParams1 = recyclerview.getLayoutParams();
        mParams1.height = (int) (55 * (list.size() >= 6 ? 6 : list.size()) * dm.density);
        recyclerview.setLayoutParams(mParams1);
        recyclerview.setVerticalScrollBarEnabled(true);
        recyclerview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        adapter.notifyDataSetChanged();
    }


    private int gravity = Gravity.BOTTOM;


    Animation.AnimationListener outAnimListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            dismissImmediately();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };

    public void dismissImmediately() {
        dismiss();
        context = null;
    }


    private SelectListPopupWindow.OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(SelectListPopupWindow.OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setRedText(int i) {
        adapter.setRedText(i, true);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int position);
    }

}
