package com.secspace.lib.common.dialog;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.secspace.lib.common.R;
import com.secspace.lib.common.adapter.Adapter;

import java.util.List;

/**
 * Created by lichuanzhi@tiankuan.net on 2019/3/19
 */
public class DropPopup {

    private PopupWindow mPopupWindow;
    private ListAdapter mAdapter;

    public DropPopup(Context context) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_list, null);
        contentView.findViewById(R.id.line).setVisibility(View.GONE);
        contentView.findViewById(R.id.tv_title).setVisibility(View.GONE);
        RecyclerView rw = contentView.findViewById(R.id.rw_options);
        rw.setBackgroundResource(R.drawable.dialog_corner_bg);
        mAdapter = new ListAdapter();
        rw.setAdapter(mAdapter);
        mPopupWindow = new PopupWindow(contentView,
                (int) (context.getResources().getDisplayMetrics().widthPixels * 0.4f),
                WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
    }

    public int getWidth() {
        return mPopupWindow.getWidth();
    }

    public void showAsDropDown(View anchor) {
        mPopupWindow.showAsDropDown(anchor);
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        mPopupWindow.showAsDropDown(anchor, xoff, yoff, gravity);
    }

    public void showAtLocation(View parent, int gravity, int x, int y) {
        mPopupWindow.showAtLocation(parent, gravity, x, y);
    }

    public void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        mPopupWindow.setOnDismissListener(onDismissListener);
    }

    public void setOutsideTouchable(boolean touchable) {
        mPopupWindow.setOutsideTouchable(touchable);
    }

    public void setBackgroundDrawable(Drawable background) {
        mPopupWindow.setBackgroundDrawable(background);
    }

    public void dismiss() {
        mPopupWindow.dismiss();
    }

    public boolean isShowing() {
        return mPopupWindow.isShowing();
    }

    public void setWidth(int width) {
        mPopupWindow.setWidth(width);
    }

    public void setHeight(int height) {
        mPopupWindow.setHeight(height);
    }

    public void updateAdapter(List<String> options) {
        mAdapter.update(options);
    }

    public void setOnItemClickListener(Adapter.OnItemClickListener<String> onItemClickListener) {
        mAdapter.setOnItemClickListener(onItemClickListener);
    }

    private class ListAdapter extends Adapter<String, Holder> {

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_option, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int position) {
            holder.mOptionView.setText(getItem(position));
            holder.itemView.setOnClickListener(new OnItemViewClick(position));
            holder.mLine.setVisibility(position == getItemCount() - 1 ? View.GONE : View.VISIBLE);
        }
    }

    private static class Holder extends RecyclerView.ViewHolder {

        private TextView mOptionView;
        private View mLine;

        Holder(@NonNull View itemView) {
            super(itemView);
            mOptionView = itemView.findViewById(R.id.tv_option);
            mLine = itemView.findViewById(R.id.line);
        }
    }

}
