package com.gh.gamecenter.common.base.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

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

import com.gh.gamecenter.common.R;
import com.gh.gamecenter.common.callback.OnDialogBackListener;
import com.gh.gamecenter.core.utils.DisplayUtils;

/**
 * @author CsHeng
 * @Date 17/05/2017
 * @Time 4:27 PM
 */
public class WaitingDialogFragment extends BaseDialogFragment {

    public static final String KEY_MSG = "msg";

    private OnDialogBackListener mBackListener;

    private TextView message;

    public static WaitingDialogFragment newInstance(String message) {
        Bundle args = new Bundle();
        args.putString(KEY_MSG, message);
        WaitingDialogFragment fragment = new WaitingDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static WaitingDialogFragment newInstance(String message, boolean isCancelable) {
        Bundle args = new Bundle();
        args.putString(KEY_MSG, message);
        WaitingDialogFragment fragment = new WaitingDialogFragment();
        fragment.setArguments(args);
        fragment.setCancelable(isCancelable);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.set_wait_dialog, null);
        message = view.findViewById(R.id.set_wait_message);
        message.setText(getArguments().getString(KEY_MSG));
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.width = DisplayUtils.dip2px(160);
        getDialog().getWindow().setAttributes(layoutParams);
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            super.show(manager, tag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void show(FragmentManager manager, String tag, OnDialogBackListener backListener) {
        show(manager, tag);
        this.mBackListener = backListener;
    }

    public void uploadWaitingHint(String hint) {
        if (message != null) message.setText(hint);
    }

    @Override
    public boolean onBack() {
        if (mBackListener != null) {
            mBackListener.onBack();
            return true;
        }
        return super.onBack();
    }

    @Override
    public void dismiss() {
        dismissAllowingStateLoss();
    }

    @Override
    public void dismissAllowingStateLoss() {
        mBackListener = null;
        try {
            super.dismissAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class WaitingDialogData {
        private String msg;

        private boolean isShow;

        public WaitingDialogData(String msg, boolean isShow) {
            this.msg = msg;
            this.isShow = isShow;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public boolean isShow() {
            return isShow;
        }

        public void setShow(boolean show) {
            isShow = show;
        }
    }
}
