package com.police.policedatasystem.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.police.policedatasystem.R;
import com.police.policedatasystem.databinding.FragmentMineBinding;

public class MineFragment extends Fragment {
    private FragmentMineBinding binding;
    private PopupWindow popupWindow;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentMineBinding.inflate(inflater);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        setPopupWindow();
        binding.mineTypeGroup.setOnClickListener(view -> {
            popupWindow.setWidth(binding.mineTypeGroup.getWidth());
            popupWindow.showAsDropDown(binding.mineTypeGroup);
        });
    }

    private void setPopupWindow() {
        // 初始化 PopupWindow
        popupWindow = new PopupWindow(getContext());
        // 加载自定义布局
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.dropdown_layout5, null);
        // 设置 PopupWindow 的宽高
        popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.all_7_fff_bg));
        popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置 PopupWindow 的背景
//        popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        // 设置 PopupWindow 显示的位置（相对于按钮）
        popupWindow.showAtLocation(binding.mineTypeGroup, Gravity.NO_GRAVITY, 0, 0);
        // 为每个菜单项设置点击事件
        TextView textView1 = contentView.findViewById(R.id.textView1); // 假设这是菜单项1的ID
        TextView textView2 = contentView.findViewById(R.id.textView2); // 假设这是菜单项2的ID
        TextView textView3 = contentView.findViewById(R.id.textView3); // 假设这是菜单项2的ID
        TextView textView4 = contentView.findViewById(R.id.textView4); // 假设这是菜单项2的ID
        TextView textView5 = contentView.findViewById(R.id.textView5); // 假设这是菜单项2的ID
        textView1.setOnClickListener(v -> {
            // 处理菜单项 1 的点击事件
            binding.tvMineSearchTitle.setText("赋能编码");
            popupWindow.dismiss(); // 关闭 PopupWindow
        });
        textView2.setOnClickListener(v -> {
            // 处理菜单项 2 的点击事件
            binding.tvMineSearchTitle.setText("申请事由");
            popupWindow.dismiss(); // 关闭 PopupWindow
        });
        textView3.setOnClickListener(v -> {
            // 处理菜单项 2 的点击事件
            binding.tvMineSearchTitle.setText("申请时间");
            popupWindow.dismiss(); // 关闭 PopupWindow
        });
        textView4.setOnClickListener(v -> {
            // 处理菜单项 2 的点击事件
            binding.tvMineSearchTitle.setText("信息来源");
            popupWindow.dismiss(); // 关闭 PopupWindow
        });
        textView5.setOnClickListener(v -> {
            // 处理菜单项 2 的点击事件
            binding.tvMineSearchTitle.setText("办理状态");
            popupWindow.dismiss(); // 关闭 PopupWindow
        });
        // 将自定义布局设置为 PopupWindow 的内容视图
        popupWindow.setContentView(contentView);
        // 设置 PopupWindow 的焦点
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true); // 设置点击 PopupWindow 外部区域使其消失
    }
}
