package com.bokecc.livemodule.live.intro;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.DWLiveCoreHandler;
import com.bokecc.livemodule.live.DWLiveIntroListener;
import com.bokecc.livemodule.utils.ImageLoader;
import com.bokecc.sdk.mobile.live.DWLive;
import com.bumptech.glide.Glide;

/**
 * 直播间简介展示控件
 */
public class VerticalLiveIntroComponent extends LinearLayout implements DWLiveIntroListener {

    private Context mContext;

    TextView mTitle;
    private WebView webView;
    private TextView tvVerticalOnAirTitle;
    private TextView tvVerticalUserCount;
    private ImageView verticalIntroLogo;
    private ImageView verticalOnAirTitle;
    private ImageView ivVerticalLiveUnAir;
    private boolean isOnAir = false;
    private ImageView ivUserCount;
    private ImageView ivVerticalIntroClose;
    private View viewVerticalIntroSplit;

    public VerticalLiveIntroComponent(Context context) {
        super(context);
        mContext = context;
        initIntroView();
    }

    public VerticalLiveIntroComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initIntroView();
    }

    // 设置直播间标题和简介
    public void initIntroView() {
        //

        DWLiveCoreHandler.getInstance().setDwLiveIntroListener(this);
        LayoutInflater.from(mContext).inflate(R.layout.vertical_portrait_intro_layout, this, true);
        mTitle = findViewById(R.id.tv_intro_title);
        //直播中
        tvVerticalOnAirTitle = findViewById(R.id.tv_vertical_on_air_title);
        verticalOnAirTitle = findViewById(R.id.anim_vertical_live_on_air);
        ivVerticalLiveUnAir = findViewById(R.id.iv_vertical_live_un_air);
        ivVerticalIntroClose = findViewById(R.id.iv_vertical_intro_close);
        viewVerticalIntroSplit = findViewById(R.id.view_vertical_intro_split);
        //人数
        tvVerticalUserCount = findViewById(R.id.tv_vertical_user_count);
        ivUserCount = findViewById(R.id.iv_user_count);
        boolean showCount = DWLiveCoreHandler.getInstance().isShowUserCount();
        if (showCount) {

            viewVerticalIntroSplit.setVisibility(VISIBLE);
            ivUserCount.setVisibility(View.VISIBLE);
            tvVerticalUserCount.setVisibility(View.VISIBLE);
        } else {
            ivUserCount.setVisibility(GONE);
            viewVerticalIntroSplit.setVisibility(GONE);
            tvVerticalUserCount.setVisibility(GONE);
        }
        //logo
        verticalIntroLogo = findViewById(R.id.iv_vertical_intro_logo);
        String iconUrl = DWLiveCoreHandler.getInstance().getLiveIcon();
        if (!TextUtils.isEmpty(iconUrl)){
            verticalIntroLogo.setVisibility(VISIBLE);
            Glide.with(mContext).load(iconUrl).into(verticalIntroLogo);
        }else {
            verticalIntroLogo.setVisibility(View.GONE);
        }

        ImageLoader.loadGifIntoImageView(getContext(),"file:///android_asset/video_play.gif",verticalOnAirTitle);

        ivVerticalIntroClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(View.GONE);
            }
        });


        webView = findViewById(R.id.intro_webview);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
// 设置支持Javascript
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
//设置背景颜色
        webView.setBackgroundColor(0);
        if (DWLive.getInstance() != null && DWLive.getInstance().getRoomInfo() != null) {
            if (!TextUtils.isEmpty(DWLive.getInstance().getRoomInfo().getDesc())){
                webView.setVisibility(View.VISIBLE);
                webView.loadDataWithBaseURL(null, "<html><head><meta name='viewport' content=width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0><style type='text/css' >body{padding: 0;margin: 0;} img{height: auto;max-width: 100%;max-height: 100%;}</style></head><body>"+DWLive.getInstance().getRoomInfo().getDesc()+"</body></html>","text/html","utf-8", null);
            }else {
                webView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onStreamEnd(boolean isNormal) {
        tvVerticalOnAirTitle.setText("待直播");
        updateLiveStatus(false);
    }

    @Override
    public void onStreamStart() {
        tvVerticalOnAirTitle.setText("直播中");
        updateLiveStatus(true);
    }

    @Override
    public void onLiveStatus(DWLive.PlayStatus status) {
        if (DWLive.PlayStatus.PLAYING==status){
            updateLiveStatus(true);
        }else {
            updateLiveStatus(false);
        }
    }

    private void updateLiveStatus(boolean onAir) {
        if (isOnAir==onAir){
            return;
        }
        if (onAir){
            isOnAir = true;
            tvVerticalOnAirTitle.setText("直播中");
            tvVerticalOnAirTitle.setTextColor(getResources().getColor(R.color.vertical_live_primary_orange));
            verticalOnAirTitle.setVisibility(View.VISIBLE);
            ivVerticalLiveUnAir.setVisibility(View.GONE);
        }else {
            isOnAir = false;
            tvVerticalOnAirTitle.setText("待直播");
            tvVerticalOnAirTitle.setTextColor(Color.BLACK);
            verticalOnAirTitle.setVisibility(View.GONE);
            ivVerticalLiveUnAir.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showRoomTitle(String title) {
        if (title==null){
            return;
        }
        mTitle.setText(title);
    }

    @Override
    public void showRoomUserNum(int number) {
        tvVerticalUserCount.setText(""+number);
    }
}

