package com.miraclegarden.smsmessage;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.miraclegarden.smsmessage.databinding.DialogActionConfirmBinding;

/**
 * 通用弹窗
 */
public class ActionConfirmDialog extends Dialog {
    private final Context context;
    OnToActionListener onToActionListener;
    AppInfo appInfo;
    int index;
    DialogActionConfirmBinding actionConfirmBinding;
    public interface OnToActionListener {
        void toSumbit(AppInfo appInfo);
        void toCancel();

    }

    public void setOnToActionListener(OnToActionListener onNextCallListener) {
        this.onToActionListener = onNextCallListener;
    }


    public ActionConfirmDialog(Context context, AppInfo appInfo) {
        super(context, R.style.MaterialDesignDialog);
        this.context = context;
        this.appInfo = appInfo;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionConfirmBinding = DialogActionConfirmBinding.inflate(getLayoutInflater());
        setContentView(actionConfirmBinding.getRoot());
        actionConfirmBinding.ivIcon.setImageDrawable(appInfo.getIcon());
        actionConfirmBinding.tvAppname.setText(appInfo.getAppName());
        actionConfirmBinding.tvPackage.setText(appInfo.getPackageName());
        if(!TextUtils.isEmpty(appInfo.getName())){
            actionConfirmBinding.tvName.setText(appInfo.getName());
        }
        if(!TextUtils.isEmpty(appInfo.getCode())){
            actionConfirmBinding.tvCode.setText(appInfo.getCode());
        }
        if(!TextUtils.isEmpty(appInfo.getRemark())){
            actionConfirmBinding.tvRemark.setText(appInfo.getRemark());
        }

        actionConfirmBinding.sumbitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(actionConfirmBinding.tvName.getText().toString().trim())){
                    Toast.makeText(context,"名称不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(actionConfirmBinding.tvCode.getText().toString().trim())){
                    Toast.makeText(context,"Code不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                appInfo.setName(actionConfirmBinding.tvName.getText().toString().trim());
                appInfo.setCode(actionConfirmBinding.tvCode.getText().toString().trim());
                appInfo.setRemark(actionConfirmBinding.tvRemark.getText().toString().trim());
                if(onToActionListener!=null){
                    dismiss();
                    onToActionListener.toSumbit(appInfo);
                }
            }
        });
        actionConfirmBinding.cancelTv.setOnClickListener(view -> {
            dismiss();
            if(onToActionListener!=null){
                onToActionListener.toCancel();
            }
        });

        Window window = getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.CENTER;
        wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setAttributes(wlp);
    }

}
