package com.cooleshow.teacher.ui.live;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;

import com.cooleshow.base.ui.fragment.BaseFragment;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.databinding.FragmentLiveBeautyLayoutBinding;
import com.cooleshow.teacher.widgets.helper.LiveRoomBeautyHelper;

import androidx.annotation.DrawableRes;

/**
 * Author by pq, Date on 2022/6/13.
 */
public class LiveBeautyOptionsFragment extends BaseFragment<FragmentLiveBeautyLayoutBinding> {
    private OnEventListener mEventListener;

    @Override
    protected void initView(View rootView) {
        mViewBinding.rgOptions.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int type = LiveRoomBeautyHelper.OPTIONS_TYPE1;
                if (checkedId == R.id.rb_options1) {
                    type = LiveRoomBeautyHelper.OPTIONS_TYPE1;
                } else if (checkedId == R.id.rb_options2) {
                    type = LiveRoomBeautyHelper.OPTIONS_TYPE2;
                } else if (checkedId == R.id.rb_options3) {
                    type = LiveRoomBeautyHelper.OPTIONS_TYPE3;
                } else if (checkedId == R.id.rb_options4) {
                    type = LiveRoomBeautyHelper.OPTIONS_TYPE4;
                }
                updateCheckStyle(checkedId);
                if (mEventListener != null) {
                    mEventListener.onChangeOptions(type);
                }
            }
        });
        mViewBinding.ivSwitchLiveBeauty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiveRoomBeautyHelper.getInstance().switchBeauty();
                updateUI();
                if (mEventListener != null) {
                    mEventListener.onSwitchBeautyMode(LiveRoomBeautyHelper.getInstance().isOpenBeauty());
                }
            }
        });
    }

    @Override
    protected void initData() {

    }


    private void updateUI() {
        if (LiveRoomBeautyHelper.getInstance().isOpenBeauty()) {
            mViewBinding.rbOptions1.setClickable(true);
            mViewBinding.rbOptions2.setClickable(true);
            mViewBinding.rbOptions3.setClickable(true);
            mViewBinding.rbOptions4.setClickable(true);
            mViewBinding.rgOptions.clearCheck();
            mViewBinding.ivSwitchLiveBeauty.setImageResource(R.drawable.icon_live_beauty_on);
            mViewBinding.tvSwitchLiveBeauty.setText("已开启");
            mViewBinding.rbOptions1.setCompoundDrawables(null, getTopDrawable(R.drawable.icon_live_beauty_options1_on), null, null);
            mViewBinding.rbOptions2.setCompoundDrawables(null, getTopDrawable(R.drawable.icon_live_beauty_options2_on), null, null);
            mViewBinding.rbOptions3.setCompoundDrawables(null, getTopDrawable(R.drawable.icon_live_beauty_options3_on), null, null);
            mViewBinding.rbOptions4.setCompoundDrawables(null, getTopDrawable(R.drawable.icon_live_beauty_options4_on), null, null);
        } else {
            mViewBinding.ivSwitchLiveBeauty.setImageResource(R.drawable.icon_live_beauty_off);
            mViewBinding.tvSwitchLiveBeauty.setText("已关闭");
            mViewBinding.rbOptions1.setClickable(false);
            mViewBinding.rbOptions2.setClickable(false);
            mViewBinding.rbOptions3.setClickable(false);
            mViewBinding.rbOptions4.setClickable(false);
            mViewBinding.rgOptions.clearCheck();
            mViewBinding.rbOptions1.setCompoundDrawables(null, getTopDrawable(R.drawable.icon_live_beauty_options1_off), null, null);
            mViewBinding.rbOptions2.setCompoundDrawables(null, getTopDrawable(R.drawable.icon_live_beauty_options2_off), null, null);
            mViewBinding.rbOptions3.setCompoundDrawables(null, getTopDrawable(R.drawable.icon_live_beauty_options3_off), null, null);
            mViewBinding.rbOptions4.setCompoundDrawables(null, getTopDrawable(R.drawable.icon_live_beauty_options4_off), null, null);
        }
    }

    private void updateCheckStyle(int viewId) {
        mViewBinding.rbOptions1.setCompoundDrawables(null, getTopDrawable(viewId == mViewBinding.rbOptions1.getId() ? R.drawable.icon_live_beauty_options1 : R.drawable.icon_live_beauty_options1_on), null, null);
        mViewBinding.rbOptions2.setCompoundDrawables(null, getTopDrawable(viewId == mViewBinding.rbOptions2.getId() ? R.drawable.icon_live_beauty_options2 : R.drawable.icon_live_beauty_options2_on), null, null);
        mViewBinding.rbOptions3.setCompoundDrawables(null, getTopDrawable(viewId == mViewBinding.rbOptions3.getId() ? R.drawable.icon_live_beauty_options3 : R.drawable.icon_live_beauty_options3_on), null, null);
        mViewBinding.rbOptions4.setCompoundDrawables(null, getTopDrawable(viewId == mViewBinding.rbOptions4.getId() ? R.drawable.icon_live_beauty_options4 : R.drawable.icon_live_beauty_options4_on), null, null);
    }

    @Override
    protected FragmentLiveBeautyLayoutBinding getLayoutView() {
        return FragmentLiveBeautyLayoutBinding.inflate(getLayoutInflater());
    }

    public void setOnEventListener(OnEventListener listener) {
        this.mEventListener = listener;
    }

    public interface OnEventListener {
        void onChangeOptions(int type);

        void onSwitchBeautyMode(boolean isOpen);
    }

    private Drawable getTopDrawable(@DrawableRes int id) {
        Drawable drawableTop = getResources().getDrawable(id);
        drawableTop.setBounds(0, 0, drawableTop.getMinimumWidth(), drawableTop.getMinimumHeight());
        return drawableTop;
    }
}
