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


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.beans.UserLoginBean;
import com.tenlionsoft.baselib.utils.UserInfoSPUtils;

import java.lang.reflect.Type;
import java.util.List;

public class BaseAreaListDialog extends Dialog {
    private Context mContext;
    private View mView;
    private RecyclerView mRlvFunction;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = LayoutInflater.from(mContext).inflate(R.layout.dialog_function, null);
        mRlvFunction = mView.findViewById(R.id.rlv_base_function);
        mRlvFunction.setLayoutManager(new GridLayoutManager(mContext, 3));
        String appList = UserInfoSPUtils.getAppList();
        if (TextUtils.isEmpty(appList)) {
            ToastUtils.show("未授权任何功能,请联系管理员.");
        } else {
            Gson gson = new Gson();
            Type type = new TypeToken<List<UserLoginBean.UseAppList>>() {
            }.getType();
            List<UserLoginBean.UseAppList> appLists = gson.fromJson(appList, type);
            BaseFunctionAdapter functionAdapter = new BaseFunctionAdapter(mContext, appLists);
            if (mChooseListener != null) {
                functionAdapter.addOnItemClickListener(bean -> mChooseListener.onChoose(bean));
            }
            mRlvFunction.setAdapter(functionAdapter);
        }
        setContentView(mView);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
//        Window window = this.getWindow();
//        window.setGravity(Gravity.CENTER);
//        window.setWindowAnimations(R.style.Dialog_ChoseFile);
//        WindowManager.LayoutParams params = window.getAttributes();
//        WindowManager m = ((Activity) mContext).getWindowManager();
//        Display d = m.getDefaultDisplay();
//        params.height = 300;
//        params.width = 300;
//        params.width = WindowManager.LayoutParams.MATCH_PARENT;
//        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        window.setBackgroundDrawable(new ColorDrawable());
//        window.setAttributes(params);

        Window dialogWindow = getWindow();
        WindowManager m = ((Activity) mContext).getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        p.width = (int) (d.getWidth() * 0.7);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialogWindow.setAttributes(p);
    }

    private OnChooseListener mChooseListener;

    public void addOnChooseListener(OnChooseListener listener) {
        this.mChooseListener = listener;
    }

    public interface OnChooseListener {
        void onChoose(UserLoginBean.UseAppList bean);
    }
}
