package com.daya.live_teaching.ui.fragmnet;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.cooleshow.base.widgets.dialog.BaseDialogFragment;
import com.cooleshow.base.widgets.dialog.DialogFragmentViewHolder;
import com.daya.live_teaching.R;
import com.daya.live_teaching.model.MetronomeInfo;
import com.daya.live_teaching.viewmodel.ClassViewModel;
import com.daya.live_teaching.widget.MCircleSeekBar;
import com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBar;
import com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

/**
 * A simple {@link Fragment} subclass.
 * 调整节拍器
 */
public class MetronomeFragmentDialog extends BaseDialogFragment {
    public OnMetronmeListener onMetronmeListener;

    public void setOnMetronmeListener(OnMetronmeListener onMetronmeListener) {
        this.onMetronmeListener = onMetronmeListener;
    }

    public interface OnMetronmeListener {
        void onMetronme(int rate, int customType, int volume);
    }

    public MetronomeFragmentDialog() {
        // Required empty public constructor
    }

    private String rhythns[] = {"1  |  4", "2  |  4", "3  |  4", "4  |  4", "6  |  8"};

    private int rate       = 90;
    private int customType = 3;


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

    @Override
    public void convertView(DialogFragmentViewHolder holder, BaseDialogFragment dialog) {
        ClassViewModel classViewModel = ViewModelProviders.of(getActivity()).get(ClassViewModel.class);
        LinearLayout ll_group = holder.getView(R.id.ll_group);
        ConstraintLayout cl_group = holder.getView(R.id.cl_group);
        MCircleSeekBar seekBar = holder.getView(R.id.seekbar);
        TextView tvRhythm = holder.getView(R.id.tv_rhythm);
        TextView tv_cancel = holder.getView(R.id.tv_cancel);
        TextView tvConfirm = holder.getView(R.id.tv_confirm);
        //音量数字
        TextView tvSeekBarProcess = holder.getView(R.id.tv_seekbar_process);
        ImageView ivPlus = holder.getView(R.id.iv_plus);
        ImageView ivReduce = holder.getView(R.id.iv_reduce);
        VerticalSeekBarWrapper seekerBarWrapper = holder.getView(R.id.seekbar_wrapper);
        ImageView ivVolume = holder.getView(R.id.iv_volume);
        VerticalSeekBar volumeSeekerBar = holder.getView(R.id.seeker_bar);
        ivVolume.setOnClickListener(view -> {
            seekerBarWrapper.setVisibility(seekerBarWrapper.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
            tvSeekBarProcess.setVisibility(tvSeekBarProcess.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        });

        ll_group.setOnClickListener(view -> dismissAllowingStateLoss());
        cl_group.setOnClickListener(view -> {
        });
        ivPlus.setOnClickListener(view -> {
            int progress = seekBar.getProgress();
            if (progress < 200) {
                progress++;
                seekBar.setProgress(progress);
            }
        });
        ivReduce.setOnClickListener(view -> {
            int progress = seekBar.getProgress();
            if (progress > 50) {
                progress--;
                seekBar.setProgress(progress);
            }
        });
        tvRhythm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (customType == 4) {
                    customType = -1;
                }
                customType++;
                tvRhythm.setText(rhythns[customType]);
            }
        });
        seekBar.setSeekBarChangeListener(new MCircleSeekBar.OnSeekChangeListener() {
            @Override
            public void onProgressChange(MCircleSeekBar view, int newProgress) {

            }

            @Override
            public void onRhythm(MCircleSeekBar view, String rhythm) {

            }
        });

        volumeSeekerBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvSeekBarProcess.setText(progress+"");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                classViewModel.setMetronomeInfo(new MetronomeInfo(seekBar.getProgress(), customType, volumeSeekerBar.getProgress()));
                dismiss();
            }
        });
        MetronomeInfo metronomeInfo = classViewModel.getMetronomeInfo().getValue();
        if (metronomeInfo != null) {
            seekBar.setProgress(metronomeInfo.getRate());
            volumeSeekerBar.setProgress(metronomeInfo.getVolume());
            tvRhythm.setText(rhythns[metronomeInfo.getCustomType()]);
            customType = metronomeInfo.getCustomType();
            rate = metronomeInfo.getRate();
        }
    }


}
