package com.idle.wall.idleactivity;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.idle.wall.R;

public class IdleRateDialog extends Dialog {
    private ImageView star1, star2, star3, star4, star5;
    private int selectedStars = 0;
    private String packageName;

    public IdleRateDialog(@NonNull Context context) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.packageName = context.getPackageName();
        initView();
    }

    private void initView() {
        setContentView(R.layout.activity_idle_rate_dialog);
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.setLayout(
                    (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.8),
                    android.view.WindowManager.LayoutParams.WRAP_CONTENT
            );
        }

        setCanceledOnTouchOutside(true);
        setCancelable(true);
        star1 = findViewById(R.id.iv_star1);
        star2 = findViewById(R.id.iv_star2);
        star3 = findViewById(R.id.iv_star3);
        star4 = findViewById(R.id.iv_star4);
        star5 = findViewById(R.id.iv_star5);
        star1.setOnClickListener(v -> setStars(1));
        star2.setOnClickListener(v -> setStars(2));
        star3.setOnClickListener(v -> setStars(3));
        star4.setOnClickListener(v -> setStars(4));
        star5.setOnClickListener(v -> setStars(5));

        findViewById(R.id.btn_cancel).setOnClickListener(v -> dismiss());
        findViewById(R.id.btn_rate).setOnClickListener(v -> {
            if (selectedStars > 0) {
                dismiss();
            } else {
                Toast.makeText(getContext(), R.string.star, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setStars(int count) {
        selectedStars = count;
        star1.setImageResource(R.drawable.star_unselected);
        star2.setImageResource(R.drawable.star_unselected);
        star3.setImageResource(R.drawable.star_unselected);
        star4.setImageResource(R.drawable.star_unselected);
        star5.setImageResource(R.drawable.star_unselected);
        for (int i = 1; i <= count; i++) {
            switch (i) {
                case 1:
                    star1.setImageResource(R.drawable.star_selected);
                    break;
                case 2:
                    star2.setImageResource(R.drawable.star_selected);
                    break;
                case 3:
                    star3.setImageResource(R.drawable.star_selected);
                    break;
                case 4:
                    star4.setImageResource(R.drawable.star_selected);
                    break;
                case 5:
                    star5.setImageResource(R.drawable.star_selected);
                    break;
            }
        }
    }
}