package com.wisewoods.eduandroid.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.blankj.utilcode.util.ScreenUtils;
import com.wisewoods.base.utils.$;
import com.wisewoods.base.utils.DialogUtils;
import com.wisewoods.eduandroid.databinding.DialogResInputBinding;

public class InputDialog extends DialogFragment {

    private DialogResInputBinding binding;
    private DialogListener listener;

    public static InputDialog show(FragmentManager manager,InputDialog dialog){
        DialogUtils.dissmiss(dialog);
        dialog = new InputDialog();
        dialog.show(manager, "输入框");
        return dialog;
    }

    public interface DialogListener {
        void click(String text);
    }

    public void setListener(DialogListener listener) {
        this.listener = listener;
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.tvOk.setOnClickListener(v -> {
            dismiss();
            String text = binding.etDir.getText().toString().trim();
            if (TextUtils.isEmpty(text)) {
                $.toast("目录名称不能为空");
                return;
            }
            if (listener != null) {
                listener.click(text);
            }
        });
        binding.tvCancel.setOnClickListener(v -> {
            dismiss();
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setLayout((int) (ScreenUtils.getAppScreenWidth() * 0.75),
                ViewGroup.LayoutParams.WRAP_CONTENT);
        getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }


}
