package com.cooleshow.teacher.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
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.TextView;

import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.LiveBeautyPagerAdapter;
import com.cooleshow.teacher.ui.live.LiveApplyMicFragment;
import com.cooleshow.teacher.ui.live.LiveOnMicFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.rong.io.live.bean.User;

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 LiveMicManagerDialog extends Dialog implements View.OnClickListener {
    private TabLayout mTabLayout;
    private ViewPager2 mViewPager;
    private LiveApplyMicFragment mApplyMicFragment;
    private LiveOnMicFragment onMicFragment;
    private OnEventListener mEventListener;
    private FragmentActivity mFragmentActivity;
    private String[] titles = new String[]{"连麦中", "申请中"};


    public LiveMicManagerDialog(@NonNull Context context) {
        super(context, R.style.MyBottomDialogStyle);
        if (context instanceof FragmentActivity) {
            mFragmentActivity = (FragmentActivity) context;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_live_mic_manager_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);

        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(mTabLayout, mViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//                createTab(tab, titles[position]);
//                tab.setText(titles[position]);
            }
        });
        LiveBeautyPagerAdapter beautyPagerAdapter = new LiveBeautyPagerAdapter(mFragmentActivity);
        onMicFragment = new LiveOnMicFragment();
        mApplyMicFragment = new LiveApplyMicFragment();
        if (mEventListener != null) {
            mApplyMicFragment.setOnEventListener(mEventListener);
            onMicFragment.setOnEventListener(mEventListener);
        }
        ArrayList<Fragment> fragments = new ArrayList();
        fragments.add(onMicFragment);
        fragments.add(mApplyMicFragment);
        beautyPagerAdapter.setData(fragments);
        mViewPager.setAdapter(beautyPagerAdapter);
        tabLayoutMediator.attach();
        initListener();
    }

    private void initListener() {
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(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.color_333333));
                }
            }

            @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.color_666666));
                }
            }

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

            }
        });
    }

    public void setFragmentActivity(FragmentActivity fragmentActivity) {
        this.mFragmentActivity = fragmentActivity;
    }

    public void setOnMicListData(ArrayList<User> onMicListData) {
        if (onMicFragment != null) {
            onMicFragment.refresh(onMicListData);
        }
    }

    public void setApplyListData(ArrayList<User> applyListData) {
        if (mApplyMicFragment != null) {
            mApplyMicFragment.refresh(applyListData);
        }
    }

    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.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_666666));
        tv_text.setText(text);
        tab.setCustomView(view);
        return tab;
    }


    @Override
    public void onClick(View v) {

    }

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

    public interface OnEventListener {
        //同意连麦申请
        void onAgreeApply(User user);

        //拒绝全部连麦申请
        void onRefuseAllMicApply();

        //连麦模式控制
        void onSwitchMicMode(boolean isEnable);
    }
}
