package com.sybotan.android.demo.view;

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

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
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.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

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

import com.sybotan.android.demo.R;
import com.sybotan.android.demo.activities.BaseActivity;
import com.sybotan.android.demo.adapter.SelectPopListAdapter;
import com.sybotan.android.demo.tools.AnimateUtil;

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


public class SelectListPopupWindow extends PopupWindow {
    DisplayMetrics dm;

    private TextView title;
    private Button btn1, btn_cancel;
    private RecyclerView recyclerview;
    private View mMenuView;


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

    private Animation inAnim;
    private Animation outAnim;

    SelectPopListAdapter adapter;
    public ArrayList<Integer> list = new ArrayList<>();
    public int position = -1;
    private Commit commit;

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

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

        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);
        btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
        //取消按钮
        btn_cancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
                mMenuView.startAnimation(outAnim);
            }
        });
        //设置按钮监听
//        btn1.setOnClickListener(itemsOnClick);
        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (commit != null) {
                    commit.onCommit(list);
                }
                dismiss();
            }
        });
        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 SelectPopListAdapter();
        adapter.setOnItemClickListener(new SelectPopListAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, Integer position, boolean isChecked) {
                if (isChecked) {
                    list.add(position);
                } else {
                    list.remove(position);
                }
            }
        });
        recyclerview.setAdapter(adapter);


        //设置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弹出窗体的背景

        this.setBackgroundDrawable(new ColorDrawable(0xb0000000));
        //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;
            }
        });


        mMenuView.startAnimation(inAnim);

    }

    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 setSelect(int position) {
        this.position = position;
        if (position == 0) {
            btn1.setTextColor(context.getResources().getColor(R.color.blue_2f48b7));
        } else {
            btn1.setTextColor(context.getResources().getColor(R.color.black_272727));
        }
    }

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


    private int gravity = Gravity.BOTTOM;


    protected void initAnimation() {
        if (inAnim == null)
            inAnim = getInAnimation();
        if (outAnim == null)
            outAnim = getOutAnimation();
    }


    public Animation getInAnimation() {
        int res = AnimateUtil.getAnimationResource(this.gravity, true);
        return AnimationUtils.loadAnimation(context, res);
    }

    public Animation getOutAnimation() {
        int res = AnimateUtil.getAnimationResource(this.gravity, false);
        return AnimationUtils.loadAnimation(context, res);
    }

    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 OnRecyclerViewItemClickListener mOnItemClickListener = null;

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

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

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

    public interface Commit {
        void onCommit(ArrayList<Integer> list);
    }

    public void setOnCommit(Commit commit) {
        this.commit = commit;
    }

}
