package com.daya.live_teaching.ui.fragmnet;

import android.view.View;
import android.widget.TextView;

import com.cooleshow.base.ui.fragment.BaseFragment;
import com.daya.live_teaching.R;
import com.daya.live_teaching.databinding.FragmentNotOnlineMembersBinding;
import com.daya.live_teaching.model.ClassMember;
import com.daya.live_teaching.model.Role;
import com.daya.live_teaching.ui.adapter.OnlineMembersListAdapter;
import com.daya.live_teaching.viewmodel.ClassViewModel;
import com.daya.live_teaching.widget.dialog.LiveCommonDialog;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * A simple {@link Fragment} subclass.
 * 在线成员列表
 */
public class OnlineMembersFragment extends BaseFragment<FragmentNotOnlineMembersBinding> {

    RecyclerView recyclerView;
    TextView btnCommit, tv_remove;
    private ClassViewModel classViewModel;
    private OnlineMembersListAdapter adapter;
    boolean isCloseMic = false;

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

    @Override
    protected void initView(View rootView) {
        recyclerView = mViewBinding.recyclerView;
        btnCommit = mViewBinding.btnCommit;
        tv_remove = mViewBinding.tvRemove;
        adapter = new OnlineMembersListAdapter(getContext());
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        adapter.setOnSubViewClickListener((OnlineMembersListAdapter.OnSubViewClickListener) (type, userId) -> {
            showMemListOperteDialog(type, userId);
        });
        btnCommit.setVisibility(View.VISIBLE);
        tv_remove.setVisibility(View.VISIBLE);
        btnCommit.setOnClickListener(view -> {
            LiveCommonDialog dialog = new LiveCommonDialog(getContext());
            dialog.show();
            dialog.setTitle("提示");
            dialog.setContent("确定关闭所有学生的麦克风吗");
            dialog.setOnConfirmClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    classViewModel.batchControlMicrophone(roomId, false);
                    dialog.dismiss();
                }
            });
            dialog.setOnCancelClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        });

        tv_remove.setOnClickListener(v -> {

            LiveCommonDialog dialog = new LiveCommonDialog(getContext());
            dialog.show();
            dialog.setTitle("提示");
            dialog.setContent("确定开启所有学生的麦克风吗");
            dialog.setOnConfirmClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    classViewModel.batchControlMicrophone(roomId, true);
                    dialog.dismiss();
                }
            });
            dialog.setOnCancelClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        });
    }


    private String roomId;

    @Override
    protected void initData() {
        classViewModel = ViewModelProviders.of(getActivity()).get(ClassViewModel.class);
        classViewModel.getRoomId().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                roomId = s;
            }
        });
        classViewModel.getMemberList().observe(this, new Observer<List<ClassMember>>() {
            @Override
            public void onChanged(List<ClassMember> classMembers) {
                List<ClassMember> memberLis = new ArrayList<>();
                memberLis.clear();
                memberLis.addAll(classMembers);
                for (ClassMember member : classMembers) {
                    if (member.getRole() == Role.LECTURER) {
                        memberLis.remove(member);
                        break;
                    }
                }
                adapter.setData(memberLis);
                isCloseMic = false;
                if (memberLis.size() > 0) {
                    for (ClassMember member : memberLis) {
                        if (member.isMicrophone()) {
                            isCloseMic = true;
                            break;
                        }
                    }
                } else {
                    isCloseMic = true;
                }
//                btnCommit.setText(isCloseMic ? "开启全员静音" : "解除全员静音");
//                Drawable drawable = isCloseMic ? getResources().getDrawable(R.drawable.ic_all_off_mic) : getResources().getDrawable(R.drawable.ic_all_on_mic);
//                btnCommit.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);

            }
        });
    }

    private void showMemListOperteDialog(final int type, final String userId) {
        String[] stringArray = getResources().getStringArray(R.array.class_member_list_operate_dialog_content);
        String content = stringArray[type];

        LiveCommonDialog dialog = new LiveCommonDialog(getContext());
        dialog.show();
        dialog.setTitle("提示");
        dialog.setContent(content);
        dialog.setOnConfirmClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (type) {
                    case 0: //
                        classViewModel.controlMicrophone(roomId, userId, true);
                        break;
                    case 1:
                        classViewModel.controlMicrophone(roomId, userId, false);
                        break;
                    case 2:
                        classViewModel.controlCamera(roomId, userId, true);
                        break;
                    case 3:
                        classViewModel.controlCamera(roomId, userId, false);
                        break;
                    case 4:
                        classViewModel.controlMusicMode(roomId, userId, true);
                        break;
                    case 5:
                        classViewModel.controlMusicMode(roomId, userId, false);
                        break;
                    //                    case METRONOME_START:
                    //                        MetronomeFragmentDialog fragment = new MetronomeFragmentDialog();
                    //                        fragment.setOnMetronmeListener((rate, type1) -> classViewModel.sendImPlayMidiMessage(member.getUserId(), roomId, true, rate, type1, new ResultCallback<Boolean>() {
                    //                            @Override
                    //                            public void onSuccess(Boolean aBoolean) {
                    //                            }
                    //
                    //                            @Override
                    //                            public void onFail(int errorCode) {
                    //                            }
                    //                        }));
                    //                        fragment.setGravity(Gravity.CENTER);
                    //                        fragment.show(getChildFragmentManager(), "");
                    //                        break;
                    //                    case METRONOME_STOP:
                    //                        classViewModel.sendImPlayMidiMessage(member.getUserId(), roomId, false, 0, 0, new ResultCallback<Boolean>() {
                    //                            @Override
                    //                            public void onSuccess(Boolean aBoolean) {
                    //
                    //                            }
                    //
                    //                            @Override
                    //                            public void onFail(int errorCode) {
                    //
                    //                            }
                    //                        });
                    //                        break;
                    default:
                        break;
                }
                dialog.dismiss();
            }
        });
        dialog.setOnCancelClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }


}
