package com.bokecc.livemodule.live.chat;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.chat.adapter.LandLiveChatTopAdapter;
import com.bokecc.livemodule.live.chat.module.ChatTopEntity;
import com.bokecc.livemodule.view.BaseRelativeLayout;
import com.bokecc.livemodule.view.PagerLayoutManager;
import com.bokecc.sdk.mobile.live.common.network.model.CCChatTopBean;

import java.util.ArrayList;

/**
 * Copyright (C), 20105-2021, 创盛视联数码科技（北京）有限公司
 * FileName:
 * Author:
 * Date: 2023/1/11
 * Description:
 */
public class LandChatTopLayout extends BaseRelativeLayout {


    View rootView;
    private ImageView ivChatTopPreBtn;
    private TextView tvChatTopNumber;
    private ImageView ivChatTopNextBtn;

    private Handler mHandler = new Handler(Looper.getMainLooper());


    private ArrayList<ChatTopEntity> chatTopBeanArrayList = new ArrayList<>();
    private RecyclerView viewPager;
    private LandLiveChatTopAdapter adapter;
    private RelativeLayout llChatTopLayout;
    private PagerLayoutManager layoutManager;
    private boolean isExpand = false;

    private int currentIndex = 0;


    public LandChatTopLayout(Context context) {
        super(context);
    }

    public LandChatTopLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LandChatTopLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {

        rootView = LayoutInflater.from(getContext()).inflate(R.layout.layout_chat_top_land,this);

        llChatTopLayout = rootView.findViewById(R.id.rl_chat_top_layout);

        viewPager = rootView.findViewById(R.id.view_pager);


        ivChatTopPreBtn = rootView.findViewById(R.id.iv_chat_top_pre_btn);
        tvChatTopNumber = rootView.findViewById(R.id.tv_chat_top_number);
        ivChatTopNextBtn = rootView.findViewById(R.id.iv_chat_top_next_btn);
        ivChatTopPreBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.collapseAll();
                int lastPosition = currentIndex;
                //收缩
                startLoop();
                viewPager.smoothScrollToPosition(Math.max(0,lastPosition-1));
                currentIndex = Math.max(0,lastPosition-1);
                tvChatTopNumber.setText(((currentIndex+1)+"/"+chatTopBeanArrayList.size()));

            }
        });
        ivChatTopNextBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.collapseAll();
                int lastPosition = currentIndex;
                //收缩
                startLoop();
                viewPager.smoothScrollToPosition(Math.min(chatTopBeanArrayList.size()-1,lastPosition+1));
                currentIndex = Math.min(chatTopBeanArrayList.size()-1,lastPosition+1);
                tvChatTopNumber.setText(((currentIndex+1)+"/"+chatTopBeanArrayList.size()));
            }
        });

        viewPager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        layoutManager = new PagerLayoutManager(mContext, LinearLayoutManager.HORIZONTAL);
        viewPager.setLayoutManager(layoutManager);
        adapter = new LandLiveChatTopAdapter(getContext());

        adapter.setOnExpandChangeListener(new LandLiveChatTopAdapter.OnExpandChangeListener() {
            @Override
            public void onExpand(int position, boolean expand, int height) {
                int currentPos = currentIndex;

                //只处理当前显示的item
                if (currentPos!=position){
                    return;
                }
                if (expand){
                    stopLoop();
                }else {
                    startLoop();
                }
            }
        });

        viewPager.setAdapter(adapter);

        layoutManager.setOnViewPagerListener(new PagerLayoutManager.OnViewPagerListener() {
            @Override
            public void onPageSelected(int position, boolean isTop, boolean isBottom) {
                tvChatTopNumber.setText(((position+1)+"/"+chatTopBeanArrayList.size()));
            }
        });




    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopLoop();
    }

    private boolean visible = false;

    public void setData(ArrayList<CCChatTopBean> chatTopBeanArrayList) {
        if (chatTopBeanArrayList==null||chatTopBeanArrayList.size()==0){
            visible = false;
            //停止滚动
            stopLoop();
            this.setVisibility(GONE);
            return;
        }else {
            visible = true;
            this.setVisibility(VISIBLE);
        }
        ArrayList<ChatTopEntity> list = new ArrayList<>();
        for (CCChatTopBean ccChatTopBean:chatTopBeanArrayList){
            ChatTopEntity chatTopEntity = new ChatTopEntity(ccChatTopBean.getId(), ccChatTopBean.getContent(), ccChatTopBean.getFromViewerId(),ccChatTopBean.getFromViewerName(),ccChatTopBean.getFromViewerAvatar(),ccChatTopBean.getFromViewerGroupId(),ccChatTopBean.getFromViewerRole());
            list.add(chatTopEntity);
        }
        this.chatTopBeanArrayList = list;
        //停止滚动
        stopLoop();

        tvChatTopNumber.setText(((1)+"/"+list.size()));

        adapter.setList(list);
        viewPager.scrollToPosition(0);
        currentIndex = 0;

        startLoop();
    }

    private void stopLoop() {
        mHandler.removeCallbacksAndMessages(null);
    }

    private void loop(){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!visible){
                    return;
                }
                if (LandChatTopLayout.this.currentIndex<chatTopBeanArrayList.size()-1){
                    currentIndex++;
                    viewPager.smoothScrollToPosition(currentIndex);
                }else {
                    LandChatTopLayout.this.currentIndex = 0;
                    viewPager.scrollToPosition(currentIndex);
                }

                tvChatTopNumber.setText(((currentIndex+1)+"/"+chatTopBeanArrayList.size()));
                loop();
            }
        },10000);
    }

    private void startLoop(){
        stopLoop();
        loop();
    }
}
