package com.prank.sound.funnyapp.dialog;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.prank.sound.funnyapp.myinterface.OnClickItemTimer;
import com.prank.sound.funnyapp.databinding.DialogStartTimerBinding;
import com.prank.sound.funnyapp.mytool.MyText;

public class DialogStartTimer implements View.OnClickListener {
    private DialogStartTimerBinding binding;
    private PopupWindow popupWindow;
    private OnClickItemTimer mListener;

    public DialogStartTimer(Activity context, OnClickItemTimer listener) {
        mListener = listener;
        binding = DialogStartTimerBinding.inflate(context.getLayoutInflater());
        popupWindow = new PopupWindow(binding.getRoot(), ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        binding.tvOff.setSelected(true);
        binding.tvOff.setOnClickListener(this);
        binding.tv15s.setOnClickListener(this);
        binding.tv30s.setOnClickListener(this);
        binding.tv1m.setOnClickListener(this);
        binding.tv5m.setOnClickListener(this);
    }

    public void showTop(View view) {

        int[] location = new int[2];
        view.getLocationOnScreen(location);

        int x = location[0];
        int y = location[1];


        int popupHeight = popupWindow.getHeight();
        int height = view.getHeight();
        int width = view.getWidth();
        Log.d("-------------", "--------x=" + x + "---y=" + y + "----popupHeight=" + popupHeight);
//        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x-30, y - popupHeight+height);
        popupWindow.showAsDropDown(view, -50,
                0,
                Gravity.NO_GRAVITY);
    }

    private void setSelected(MyText tv) {
        int childCount = binding.layoutTime.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = binding.layoutTime.getChildAt(i);
            if (child instanceof MyText) {
                MyText tvTimer = (MyText) child;
                if (tvTimer.equals(tv)) {
                    tvTimer.setSelected(true);
                } else {
                    tvTimer.setSelected(false);
                }
                System.out.println("TextView: " + tvTimer.getText());
            }
        }

    }
    public void hide() {
        popupWindow.dismiss();

    }
    public void setOffSelected() {
        setSelected(binding.tvOff);

    }

    @Override
    public void onClick(View v) {
        if (v instanceof MyText) {
            MyText v1 = (MyText) v;
            String string = v1.getText().toString();
            switch (string){
                case "off":
                    mListener.onClickTimerListener(-1);
                    break;
                case "15 sec":
                    mListener.onClickTimerListener(15*1000L);
                    break;
                case "30 sec":
                    mListener.onClickTimerListener(30*1000L);
                    break;
                case "1 min":
                    mListener.onClickTimerListener(60*1000L);
                    break;
                case "5 min":
                    mListener.onClickTimerListener(5*60*1000L);
                    break;
            }
            setSelected(v1);
        }
    }
}
