package com.tencent.liteav.live.ui.widget;

import android.graphics.drawable.Drawable;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.luck.picture.lib.camera.listener.ClickListener;
import com.tencent.imsdk.v2.V2TIMGroupInfo;
import com.tencent.imsdk.v2.V2TIMGroupInfoResult;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMValueCallback;
import com.tencent.liteav.live.R;
import com.tencent.liteav.live.databinding.ViewRoomSettingBinding;
import com.tencent.liteav.live.ui.widget.msg.TCConstants;
import com.tencent.liteav.live.ui.widget.msg.V2TIMSimpleCallback;
import com.wisewoods.base.utils.$;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoomSettingDialog extends BottomSheetDialog {


    private ViewRoomSettingBinding binding;
    ClickListener listener;
    boolean isCanShare = false;
    boolean isAudVideo = false;
    boolean isAudText = false;
    boolean isStuVideo = false;

    public void setListener(ClickListener listener) {
        this.listener = listener;
    }

    public void setData(String key, boolean isCheck) {
        switch (key) {
            case TCConstants.KEY_IS_CAN_SHARE:
                isCanShare = isCheck;
                if (isCanShare) {
                    setTextRight(binding.tvShare, R.drawable.ic_metting_people_check);
                } else {
                    setTextRight(binding.tvShare, R.drawable.ic_metting_default);
                }
                break;
            case TCConstants.KEY_IS_AUD_TEXT:
                isAudText = isCheck;
                if (isAudText) {
                    setTextRight(binding.tvAduText, R.drawable.ic_metting_people_check);
                } else {
                    setTextRight(binding.tvAduText, R.drawable.ic_metting_default);
                }
                break;
            case TCConstants.KEY_IS_AUD_VIDEO:
                isAudVideo = isCheck;
                if (isAudVideo) {
                    setTextRight(binding.tvAduVideo, R.drawable.ic_metting_people_check);
                } else {
                    setTextRight(binding.tvAduVideo, R.drawable.ic_metting_default);
                }
                break;
            case TCConstants.KEY_IS_STU_VIDEO:
                isStuVideo = isCheck;
                if (isStuVideo) {
                    setTextRight(binding.tvStuVideo, R.drawable.ic_metting_people_check);
                } else {
                    setTextRight(binding.tvStuVideo, R.drawable.ic_metting_default);
                }
                break;
        }
    }

    public RoomSettingDialog(String groupId) {
        super(ActivityUtils.getTopActivity(), R.style.BottomSheetDialog);

        binding = ViewRoomSettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.tvShare.setOnClickListener(v -> {
            try {
                HashMap<String, byte[]> map = new HashMap<>();
                map.put(TCConstants.KEY_IS_CAN_SHARE, (isCanShare ? "0" : "1").getBytes("UTF-8"));
                V2TIMGroupInfo info = new V2TIMGroupInfo();
                info.setGroupID(groupId);
                info.setCustomInfo(map);
                V2TIMManager.getGroupManager().setGroupInfo(info, new V2TIMSimpleCallback<>("更新群信息"));
                isCanShare = !isCanShare;
                if (isCanShare) {
                    setTextRight(binding.tvShare, R.drawable.ic_metting_people_check);
                } else {
                    setTextRight(binding.tvShare, R.drawable.ic_metting_default);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                $.toast("设置失败" + e.getMessage());
                LogUtils.e("发送自定义群聊消息异常", e.getMessage());
            }
        });

        binding.tvAduText.setOnClickListener(v -> {
            try {
                HashMap<String, byte[]> map = new HashMap<>();
                map.put(TCConstants.KEY_IS_AUD_TEXT, (isAudText ? "0" : "1").getBytes("UTF-8"));
                V2TIMGroupInfo info = new V2TIMGroupInfo();
                info.setGroupID(groupId);
                info.setCustomInfo(map);
                V2TIMManager.getGroupManager().setGroupInfo(info, new V2TIMSimpleCallback<>("更新群信息"));
                isAudText = !isAudText;
                if (isAudText) {
                    setTextRight(binding.tvAduText, R.drawable.ic_metting_people_check);
                } else {
                    setTextRight(binding.tvAduText, R.drawable.ic_metting_default);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                $.toast("设置失败" + e.getMessage());
                LogUtils.e("发送自定义群聊消息异常", e.getMessage());
            }
        });


        binding.tvAduVideo.setOnClickListener(v -> {
            try {
                HashMap<String, byte[]> map = new HashMap<>();
                map.put(TCConstants.KEY_IS_AUD_VIDEO, (isAudVideo ? "0" : "1").getBytes("UTF-8"));
                V2TIMGroupInfo info = new V2TIMGroupInfo();
                info.setGroupID(groupId);
                info.setCustomInfo(map);
                V2TIMManager.getGroupManager().setGroupInfo(info, new V2TIMSimpleCallback<>("更新群信息"));
                isAudVideo = !isAudVideo;
                if (isAudVideo) {
                    setTextRight(binding.tvAduVideo, R.drawable.ic_metting_people_check);
                } else {
                    setTextRight(binding.tvAduVideo, R.drawable.ic_metting_default);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                $.toast("设置失败" + e.getMessage());
                LogUtils.e("发送自定义群聊消息异常", e.getMessage());
            }
        });


        binding.tvStuVideo.setOnClickListener(v -> {
            try {
                HashMap<String, byte[]> map = new HashMap<>();
                map.put(TCConstants.KEY_IS_STU_VIDEO, (isStuVideo ? "0" : "1").getBytes("UTF-8"));
                V2TIMGroupInfo info = new V2TIMGroupInfo();
                info.setGroupID(groupId);
                info.setCustomInfo(map);
                V2TIMManager.getGroupManager().setGroupInfo(info, new V2TIMSimpleCallback<>("更新群信息"));
                isStuVideo = !isStuVideo;
                if (isStuVideo) {
                    setTextRight(binding.tvStuVideo, R.drawable.ic_metting_people_check);
                } else {
                    setTextRight(binding.tvStuVideo, R.drawable.ic_metting_default);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                $.toast("设置失败" + e.getMessage());
                LogUtils.e("发送自定义群聊消息异常", e.getMessage());
            }
        });


        V2TIMManager.getGroupManager().getGroupsInfo(getList(groupId), new V2TIMValueCallback<List<V2TIMGroupInfoResult>>() {
            @Override
            public void onError(int i, String s) {
                $.toast(s);
                dismiss();
            }

            @Override
            public void onSuccess(List<V2TIMGroupInfoResult> v2TIMGroupInfoResults) {
                if (v2TIMGroupInfoResults != null && v2TIMGroupInfoResults.size() == 1) {
                    final V2TIMGroupInfoResult v2TIMGroupInfoResult = v2TIMGroupInfoResults.get(0);
                    if (v2TIMGroupInfoResult != null) {
                        Map<String, byte[]> customInfo = v2TIMGroupInfoResult.getGroupInfo().getCustomInfo();
                        if (customInfo != null && customInfo.size() > 0) {
                            setKeyData(customInfo, TCConstants.KEY_IS_CAN_SHARE, binding.tvShare);
                            setKeyData(customInfo, TCConstants.KEY_IS_AUD_TEXT, binding.tvAduText);
                            setKeyData(customInfo, TCConstants.KEY_IS_AUD_VIDEO, binding.tvAduVideo);
                            setKeyData(customInfo, TCConstants.KEY_IS_STU_VIDEO, binding.tvStuVideo);
                        }
                    } else {
                        onError(7666, "群信息获取异常");
                    }
                }
            }
        });

    }

    private void setKeyData(Map<String, byte[]> customInfo, String keyIsCanShare, TextView tvShare) {
        if (customInfo.get(keyIsCanShare) != null && customInfo.get(keyIsCanShare).length > 0) {
            if ("1".equals(new String(customInfo.get(keyIsCanShare)))) {
                setTextRight(tvShare, R.drawable.ic_metting_people_check);
                switch (keyIsCanShare) {
                    case TCConstants.KEY_IS_CAN_SHARE:
                        isCanShare = true;
                        break;
                    case TCConstants.KEY_IS_AUD_TEXT:
                        isAudText = true;
                        break;
                    case TCConstants.KEY_IS_AUD_VIDEO:
                        isAudVideo = true;
                        break;
                    case TCConstants.KEY_IS_STU_VIDEO:
                        isStuVideo = true;
                        break;
                }
            }
        } else {
            setTextRight(tvShare, R.drawable.ic_metting_default);
        }
    }

    public void setTextRight(TextView view, int imageResId) {
        Drawable drawable = view.getResources().getDrawable(imageResId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        view.setCompoundDrawables(null, null, drawable, null);
    }

    public static ArrayList<String> getList(String... groupId) {
        ArrayList<String> al = new ArrayList<>();
        for (String s : groupId) {
            al.add(s);
        }
        return al;
    }

}