package com.cooleshow.teacher.ui.homepage;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.im.bean.ContactRoomListBean;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.fragment.BaseMVPFragment;
import com.cooleshow.base.ui.video.VideoPlayActivity;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.MineStyleFansGroupListAdapter;
import com.cooleshow.teacher.adapter.MineStyleVideoListAdapter;
import com.cooleshow.teacher.bean.TeacherSelfStyleInfoBean;
import com.cooleshow.teacher.contract.MineStyleContract;
import com.cooleshow.teacher.databinding.FragmentMineStyleLayoutBinding;
import com.cooleshow.teacher.presenter.homePage.MineStylePresenter;
import com.cooleshow.teacher.widgets.MineStyleVideoListItemDecoration;
import com.cooleshow.teacher.widgets.StyleEmptyView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import io.rong.imkit.RongIM;

/**
 * Author by pq, Date on 2022/5/9.
 */
public class MineStyleFragment extends BaseMVPFragment<FragmentMineStyleLayoutBinding, MineStylePresenter> implements MineStyleContract.MineStylePageView {

    private MineStyleFansGroupListAdapter mFansGroupListAdapter;
    private MineStyleVideoListAdapter mStyleVideoListAdapter;

    @Override
    protected void initView(View rootView) {
        mStyleVideoListAdapter = new MineStyleVideoListAdapter(R.layout.item_mine_style_video_layout);
        MineStyleVideoListItemDecoration itemDecoration = new MineStyleVideoListItemDecoration(SizeUtils.dp2px(11), 0, 0, 0, SizeUtils.dp2px(5));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        mViewBinding.recyclerView.setLayoutManager(gridLayoutManager);
        mViewBinding.recyclerView.addItemDecoration(itemDecoration);
        mViewBinding.recyclerView.setAdapter(mStyleVideoListAdapter);
        StyleEmptyView styleEmptyView = StyleEmptyView.getInstance(getActivity()).setEmptyText(getString(R.string.style_video_empty_text))
                .setOpeBtn(getString(R.string.goto_upload))
                .setEmptyIcon(R.mipmap.teacher_style_empty_icon)
                .setOpeBtnClickListener(v -> {
                    ARouter.getInstance().build(RouterPath.MineCenter.MINE_STYLE_PAGE)
                            .navigation();
                });
        mStyleVideoListAdapter.setEmptyView(styleEmptyView);

        mFansGroupListAdapter = new MineStyleFansGroupListAdapter();
        mViewBinding.recyclerViewFans.setLayoutManager(new LinearLayoutManager(getContext()));
        mViewBinding.recyclerViewFans.setAdapter(mFansGroupListAdapter);
        mViewBinding.recyclerView.setNestedScrollingEnabled(false);
        mViewBinding.recyclerViewFans.setNestedScrollingEnabled(false);
        StyleEmptyView fansEmptyView = StyleEmptyView.getInstance(getActivity())
                .setEmptyIcon(R.mipmap.teacher_fans_empty_icon)
                .setEmptyText(getString(R.string.fans_empty_text))
                .setOpeBtn(getString(R.string.goto_create))
                .setOpeBtnClickListener(v -> {
                    ARouter.getInstance().build(RouterPath.ChatCenter.CHAT_CREATE_GROUP)
                            .navigation();
                });
        mFansGroupListAdapter.setEmptyView(fansEmptyView);
        mFansGroupListAdapter.setOnItemClickListener((adapter, view, position) -> {
            ContactRoomListBean item = (ContactRoomListBean) adapter.getItem(position);
            goChat(item.getId() + "", item.getName());
        });

        mStyleVideoListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                TeacherSelfStyleInfoBean.StyleVideoBean styleVideoBean = mStyleVideoListAdapter.getData().get(position);
                if (styleVideoBean != null) {
                    VideoPlayActivity.start(getContext(), styleVideoBean.videoUrl);
                }
            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getTeacherSelfStyle();
        presenter.getTeaGroupList();
    }

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

    @Override
    protected MineStylePresenter createPresenter() {
        return new MineStylePresenter();
    }

    @Override
    public void getSelfStyleSuccess(TeacherSelfStyleInfoBean styleInfoBean) {
        if (isDetached()) {
            return;
        }
        if (styleInfoBean == null) {
            return;
        }
        //个人介绍
        if (TextUtils.isEmpty(styleInfoBean.introduction)) {
            mViewBinding.tvSelfIntroduction.setVisibility(View.GONE);
            mViewBinding.selfIntroductionEmpty.setVisibility(View.VISIBLE);
            mViewBinding.selfIntroductionEmpty.setOpeBtnClickListener(v -> {
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_STYLE_PAGE)
                        .navigation();
            });
        } else {
            mViewBinding.tvSelfIntroduction.setVisibility(View.VISIBLE);
            mViewBinding.selfIntroductionEmpty.setVisibility(View.GONE);
            mViewBinding.tvSelfIntroduction.setText(styleInfoBean.introduction);
        }
        //视频信息
        if (mStyleVideoListAdapter != null) {
            mStyleVideoListAdapter.setNewInstance(styleInfoBean.styleVideo);
        }

    }

    /**
     * 获取粉丝群
     *
     * @param list
     */
    @Override
    public void getFunGroupList(List<ContactRoomListBean> list) {
        mFansGroupListAdapter.setNewInstance(list);
    }

    private void goChat(String groupId, String groupName) {
        RongIM.getInstance().startGroupChat(getContext(), groupId, groupName);
    }
}
