package com.ez.ertc.meeting.ui;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.ez.ertc.meeting.R2;
import com.ez.ertc.meeting.utils.ViewUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public
/**
 *
 *
 * @author xiezuoyuan
 * @time 2021/11/6 15:08
 */
class BeautySeekBarHolder {
    @BindView(R2.id.ivProgress)
    ImageView ivProgress;
    @BindView(R2.id.ivIndicatorDefault)
    ImageView ivIndicatorDefault;
    @BindView(R2.id.tvProgress)
    TextView tvProgress;
    @BindView(R2.id.seekBar)
    SeekBar seekBar;
    @BindView(R2.id.flProgress)
    FrameLayout flProgress;
    private View contentView;
    private int width, offsetRing, offsetFix;
    private BeautyConfigHolder.BaseDTO selectedDTO;
    private boolean waitMeasure = false;

    @SuppressLint("NewApi")
    public BeautySeekBarHolder(View contentView, final ProgressListener progressListener) {
        ButterKnife.bind(this, contentView);
        this.contentView = contentView;
        flProgress.setVisibility(View.GONE);
        offsetFix = ViewUtil.dp2px(contentView.getContext(), 1.5f);
        offsetRing = ViewUtil.dp2px(contentView.getContext(), 7f);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @SuppressLint("NewApi")
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                setProgress(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                flProgress.setVisibility(View.VISIBLE);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (selectedDTO != null)
                    progressListener.onProgressChanged(seekBar.getProgress(), selectedDTO);
                flProgress.setVisibility(View.GONE);
            }
        });
    }

    /**
     * UI可见时才能获取到
     */
    private void calSeekBarWidth() {
        width = seekBar.getProgressDrawable().getBounds().width();
    }

    public void onShow() {
        if (width == 0) {
            seekBar.post(new Runnable() {
                @Override
                public void run() {
                    calSeekBarWidth();
                    if (waitMeasure && selectedDTO != null){
                        waitMeasure = false;
                        setDefaultProgress(selectedDTO);
                    }
                }
            });
        }
    }

    @SuppressLint("NewApi")
    public void setDefaultProgress(final BeautyConfigHolder.BaseDTO baseDTO) {
        this.selectedDTO = baseDTO;
        if (baseDTO.defaultProgress == -1) {
            hideSeekBar();
        } else {
            if (width == 0) {
                //此时UI还不可见，需要等计算完成后重新设置
                waitMeasure = true;
            } else {
                int offset = width * baseDTO.defaultProgress / 100 + offsetRing;
                ivIndicatorDefault.setTranslationX(offset);
            }
            showSeekBar();
        }
    }

    @SuppressLint("NewApi")
    private void setProgress(int progress) {
        tvProgress.setText("" + progress);
        int offset = width * progress / 100 + offsetFix;//微调，加上1.5dp的偏移量
        flProgress.setTranslationX(offset);
    }

    public void updateProgress(int progress) {
        setProgress(progress);
        seekBar.setProgress(progress);
    }

    public void hideSeekBar() {
        contentView.setVisibility(View.GONE);
    }

    public void showSeekBar() {
        contentView.setVisibility(View.VISIBLE);
    }

    public interface ProgressListener {
        void onProgressChanged(int progress, BeautyConfigHolder.BaseDTO baseDTO);
    }
}
