package com.cooleshow.teacher.widgets.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;

import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.LiveBeautyPagerAdapter;
import com.cooleshow.teacher.ui.live.LiveBeautyFilterFragment;
import com.cooleshow.teacher.ui.live.LiveBeautyOptionsFragment;
import com.cooleshow.teacher.widgets.helper.LiveRoomBeautyHelper;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;

/**
 * Author by pq, Date on 2022/6/11.
 */
public class LiveBeautyOptionsDialog extends Dialog implements LiveBeautyOptionsFragment.OnEventListener, View.OnClickListener {
    public static final String[] titles = new String[]{"美颜", "滤镜"};
    private TabLayout mTabLayout;
    private ViewPager2 mViewPager;
    private SeekBar mSeekBar;
    private FragmentActivity mActivity;

    public LiveBeautyOptionsDialog(@NonNull Context context) {
        super(context, R.style.liveBeautyDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_live_beauty_layout);
        Window window = getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        window.setWindowAnimations(com.cooleshow.base.R.style.BottomAnimation);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.horizontalMargin = 0;
        lp.verticalMargin = 0;
        //将设置好的属性set回去
        window.setAttributes(lp);
        mTabLayout = findViewById(R.id.tab_layout);
        mViewPager = findViewById(R.id.viewPager);
        mSeekBar = findViewById(R.id.seek_bar);
        findViewById(R.id.tv_reset).setOnClickListener(this);
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(mTabLayout, mViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                createTab(tab, titles[position]);
            }
        });

        LiveBeautyPagerAdapter beautyPagerAdapter = new LiveBeautyPagerAdapter(mActivity);
        LiveBeautyOptionsFragment optionsFragment = new LiveBeautyOptionsFragment();
        LiveBeautyFilterFragment optionsFragment2 = new LiveBeautyFilterFragment();
        optionsFragment.setOnEventListener(this);
        ArrayList<Fragment> fragments = new ArrayList();
        fragments.add(optionsFragment);
        fragments.add(optionsFragment2);
        beautyPagerAdapter.setData(fragments);
        mViewPager.setAdapter(beautyPagerAdapter);
        tabLayoutMediator.attach();

        initListener();
    }

    public void setFragmentActivity(FragmentActivity fragmentActivity) {
        mActivity = fragmentActivity;
    }

    private void initListener() {
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() != 0) {
                    mSeekBar.setVisibility(View.GONE);
                } else {
                    mSeekBar.setVisibility(View.VISIBLE);
                }
                if (tab != null && tab.getCustomView() != null) {
                    View customView = tab.getCustomView();
                    TextView tv_text = customView.findViewById(R.id.tv_text);
                    tv_text.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.white));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    View customView = tab.getCustomView();
                    TextView tv_text = customView.findViewById(R.id.tv_text);
                    tv_text.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.white_translucent));
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                LiveRoomBeautyHelper.getInstance().update(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private TabLayout.Tab createTab(TabLayout.Tab tab, String text) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_live_beauty_tab_layout, null);
        TextView tv_text = view.findViewById(R.id.tv_text);
        tv_text.setText(text);
        tab.setCustomView(view);
        return tab;
    }

    @Override
    public void onChangeOptions(int type) {
        if (mSeekBar != null && mSeekBar.getVisibility() != View.VISIBLE) {
            mSeekBar.setVisibility(View.VISIBLE);
        }
        int currentProgress = LiveRoomBeautyHelper.getInstance().getCurrentProgress(type);
        mSeekBar.setProgress(currentProgress);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_reset) {
            //重置
            if (mSeekBar != null) {
                mSeekBar.setProgress(0);
            }
            LiveRoomBeautyHelper.getInstance().reset();
            return;
        }
    }
}
