package com.tenlionsoft.baselib.core.widget.views;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.beans.FuncBean;
import com.tenlionsoft.baselib.core.widget.base.BaseRecyclerAdapter;
import com.tenlionsoft.baselib.core.widget.base.FunctionNumAdapter;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 从底部弹出的选择文件的dialog
 */
public class ChooseFuncDialogView extends Dialog {

    private boolean iscancelable = true;
    private boolean isBackCancelable = true;
    private View view;
    private Context context;
    private int gravity;
    private OnChoseListener mListener;
    private RecyclerView mRlvFuncs;
    private FunctionNumAdapter mBtnAdapter;

    private ChooseFuncDialogView(Context context, FunctionNumAdapter adapter) {
        super(context, R.style.Dialog_ChoseFile);
        this.context = context;
        this.mBtnAdapter = adapter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_funcs, null);
        setContentView(view);
        setCancelable(iscancelable);
        setCanceledOnTouchOutside(isBackCancelable);
        mRlvFuncs = view.findViewById(R.id.rlv_funcs);
        mRlvFuncs.setLayoutManager(new GridLayoutManager(context, 2));
        mRlvFuncs.setAdapter(mBtnAdapter);
        mBtnAdapter.addOnItemClickListener(funcBean -> mListener.onChoose(funcBean));
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Dialog_ChoseFile);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = (int) (displayMetrics.widthPixels * 0.9);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    public void addOnChoseListener(OnChoseListener listener) {
        this.mListener = listener;
    }


    public interface OnChoseListener {
        void onChoose(FuncBean bean);
    }

    public static class DialogBuilder {
        private Context mContext;
        private FunctionNumAdapter mAdapter;

        public DialogBuilder(Context context) {
            this.mContext = context;
        }

        public DialogBuilder setAdapter(FunctionNumAdapter adapter) {
            this.mAdapter = adapter;
            return this;
        }


        public ChooseFuncDialogView build() {
            return new ChooseFuncDialogView(this.mContext, mAdapter);
        }
    }
}