package com.naz.sdkdemo.dialog;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.naz.sdkdemo.R;
import com.shehuan.nicedialog.BaseNiceDialog;
import com.shehuan.nicedialog.ViewHolder;

/**
 * @Date: 2022/6/12 4:31 下午
 * @author: sunjichang
 * @Description:
 */
public class CommonDialog extends BaseNiceDialog {
    private TextView tvTitle;
    private TextView tvMsg;
    private FrameLayout flContentCustom;
    private Button btnSure;
    private Button btnCancel;
    private OnDialogUiReadyListener uiReadyListener;
    private boolean isInited;
    private View.OnClickListener sureListener;
    private View.OnClickListener cancelListener;
    private Params params = new Params();

    @Override
    public int intLayoutId() {
        return R.layout.dialog_common;
    }

    @Override
    public void convertView(ViewHolder holder, BaseNiceDialog dialog) {
        tvTitle = holder.getView(R.id.tv_dialog_title);
        tvMsg = holder.getView(R.id.tv_dialog_msg);
        flContentCustom = holder.getView(R.id.fl_dialog_content_custom);
        btnSure = holder.getView(R.id.btn_dialog_confirm);
        btnCancel = holder.getView(R.id.btn_dialog_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel(v);
            }
        });
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sure(v);
            }
        });
        setupViews();
        setMargin(20);
    }

    protected void setupViews() {

    }

    private void updateView() {
        tvTitle.setText(params.title);
        if (params.titleTextSize != -1) {
            tvTitle.setTextSize(params.titleTextSize);
        }
        if (params.titleTextColor != -1) {
            tvTitle.setTextColor(params.titleTextColor);
        }

        tvMsg.setText(params.content);
        if (params.contentTextSize != -1) {
            tvMsg.setTextSize(params.contentTextSize);
        }
        if (params.contentTextColor != -1) {
            tvMsg.setTextColor(params.contentTextColor);
        }
        if (null != params.customContentView) {
            flContentCustom.setVisibility(View.VISIBLE);
            tvMsg.setVisibility(View.GONE);
            flContentCustom.removeAllViews();
            ViewParent parent = params.customContentView.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(params.customContentView);
            }
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.gravity = Gravity.CENTER;
            flContentCustom.addView(params.customContentView, lp);
        } else {
            flContentCustom.setVisibility(View.GONE);
            tvMsg.setVisibility(View.VISIBLE);
        }
    }

    public void setUiReadyListener(OnDialogUiReadyListener uiReadyListener) {
        this.uiReadyListener = uiReadyListener;
    }

    public void setSureListener(View.OnClickListener sureListener) {
        this.sureListener = sureListener;
    }

    public void setCancelListener(View.OnClickListener cancelListener) {
        this.cancelListener = cancelListener;
    }

    public CommonDialog setTitleTextSize(int textSizeSp) {
        params.titleTextSize = textSizeSp;
        return this;
    }

    public CommonDialog setTitleTextColor(int textColor) {
        params.titleTextColor = textColor;
        return this;
    }

    public CommonDialog setTitle(String title) {
        params.title = title;
        return this;
    }

    public CommonDialog setContent(String content) {
        params.content = content;
        return this;
    }

    public CommonDialog setContentTextSize(int textSizeSp) {
        params.contentTextSize = textSizeSp;
        return this;
    }

    public CommonDialog setContentTextColor(int textColor) {
        params.contentTextColor = textColor;
        return this;
    }

    public CommonDialog setCustomContentView(View view) {
        params.customContentView = view;
        return this;
    }

    @Override
    public void onStart() {
        super.onStart();
        isInited = true;
        updateView();
        if (null != uiReadyListener) {
            uiReadyListener.onUiReady();
        }
    }

    protected void sure(View v) {
        dismiss();
        if (null != sureListener) {
            sureListener.onClick(v);
        }
    }

    protected void cancel(View v) {
        dismiss();
        if (null != cancelListener) {
            cancelListener.onClick(v);
        }
    }

    private static class Params {
        public String title;
        public float titleTextSize = -1;
        public int titleTextColor = -1;
        public String content;
        public float contentTextSize = -1;
        public int contentTextColor = -1;
        public View customContentView;
    }
}
