package com.gh.gamecenter.energy.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.gh.gamecenter.common.callback.ConfirmListener;
import com.gh.gamecenter.energy.R;
import com.gh.gamecenter.energy.databinding.DialogEnergySignBinding;
import com.lightgame.utils.AppManager;

public class DialogUtils {

    private DialogUtils() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * @param context may be is application context
     * @return activity context
     */
    private static Context checkDialogContext(Context context) {
        if (context == null) {
            throw new NullPointerException("dialog context is null");
        }

        if (context instanceof Activity) {
            return context;
        }

        // currentActivity 是否存在 isDestroyed 的情况?
        return AppManager.getInstance().currentActivity();
    }

    public static void showSignSuccessDialog(Context context, ConfirmListener listener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_sign_success, null);

        ImageView signBg = contentView.findViewById(R.id.signBg);
        RelativeLayout signContainer = contentView.findViewById(R.id.signContainer);

        Animation animation1 = AnimationUtils.loadAnimation(context, R.anim.anim_badge_light_bg);
        signBg.startAnimation(animation1);

        Animation animation2 = AnimationUtils.loadAnimation(context, R.anim.anim_sign_dialog_dismiss);
        signContainer.postDelayed(() -> signContainer.startAnimation(animation2), 2000);

        signContainer.postDelayed(dialog::dismiss, 3000);

        dialog.setOnDismissListener(dialogInterface -> listener.onConfirm());

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showEnergyTaskNoticeDialog(Context context, String title, String content) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_notice, null);
        TextView titleTv = contentView.findViewById(R.id.dialog_title);
        TextView contentTv = contentView.findViewById(R.id.dialog_content);
        TextView okTv = contentView.findViewById(R.id.dialog_ok);

        if (title == null) {
            titleTv.setVisibility(View.GONE);
        } else {
            titleTv.setVisibility(View.VISIBLE);
            titleTv.setText(title);
        }
        contentTv.setText(content);

        okTv.setTextColor(ContextCompat.getColor(context, R.color.theme_font));
        okTv.setOnClickListener(v -> dialog.dismiss());

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showEnergySignDialog(Context context, int sevenDaySerialSign) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);
        DialogEnergySignBinding binding = DialogEnergySignBinding.inflate(LayoutInflater.from(context));

        if (sevenDaySerialSign > 7) sevenDaySerialSign = 7;

        for (int i = 1; i <= sevenDaySerialSign; i++) {
            int index = (i - 1) * 2;
            LinearLayout dayContainer = (LinearLayout) binding.signDaysContainer.getChildAt(index);
            ImageView dayIv = (ImageView) dayContainer.getChildAt(1);
            dayIv.setImageResource(R.drawable.ic_energy_center_signed);

            if (i != 7) {
                int rIndex = (i - 1) * 2 + 1;
                LinearLayout lineContainer = (LinearLayout) binding.signDaysContainer.getChildAt(rIndex);
                View straightLine = lineContainer.getChildAt(0);
                View dottedLine = lineContainer.getChildAt(1);
                if (i != sevenDaySerialSign) {
                    straightLine.setVisibility(View.VISIBLE);
                } else {
                    dottedLine.setVisibility(View.VISIBLE);
                }
            }
        }

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(binding.getRoot());
        dialog.show();
    }

}
