package com.bokecc.livemodule.view.subtitle;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.utils.DensityUtil;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayUrlInfo;

/**
 * SubtitleView
 *
 * @author CC
 */
public class SubtitleView extends RelativeLayout {

    private static final String TAG = "SubtitleView";

    private static final float SHADOW_RADIUS = 1F;
    private static final float SHADOW_DX = 1F;
    private static final float SHADOW_DY = 1F;

    private final Context mContext;
    private final OutlineTextView tvFirstSubtitle;
    private final OutlineTextView tv_second_subtitle;
    private Subtitle firstSubtitle, secondSubtitle;
    private int firstBottom, secondBottom, commonBottom, selectedSubtitle = 3, firstSort, secondSort;
    private String firstSubName, secondSubName;
    private double firstBottomRate, secondBottomRate;
    /**
     * 字幕自适应比例
     */
    private float proportion = 1.00f;


    /**
     * 字幕字体大小模式
     */
    private int subtitleModel;

    /**
     * 当前播放器View宽度
     */
    private int surfaceWidth = 1080;

    /**
     * 服务器设置的第一个字幕字体大小
     */
    private int firstServerSubtitleSize = 16;
    /**
     * 服务器设置的第二个字幕字体大小
     */
    private int secondServerSubtitleSize = 16;

    /**
     * 当前是否为离线视频
     */
    private boolean offline = false;
    /**
     * 存储的离线字幕第一个字幕大小
     */
    private int localFirstSubtitleSize = 16;
    /**
     * 存储的离线字幕第二个字幕大小
     */
    private int localSecondSubtitleSize = 16;

    public SubtitleView(Context context) {
        this(context, null);
    }

    public SubtitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_subtitle, this, true);
        tvFirstSubtitle = view.findViewById(R.id.tv_first_subtitle);
        tv_second_subtitle = view.findViewById(R.id.tv_second_subtitle);
    }

    public SubtitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_subtitle, this, true);
        tvFirstSubtitle = view.findViewById(R.id.tv_first_subtitle);

        tv_second_subtitle = view.findViewById(R.id.tv_second_subtitle);
    }

    public void initStyle(ReplayUrlInfo.SubtitleData data) {
        if (data == null) {
            return;
        }

        float minBottom = 0f;
        if (data.getSubtitle() != null){
            try{
                minBottom = Float.parseFloat(data.getSubtitle().getBottom());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (data.getSubtitle2()!=null){
            try{
                float bottom2 = Float.parseFloat(data.getSubtitle2().getBottom());
                if (minBottom==0f){
                    minBottom = bottom2;
                }
                if (bottom2<minBottom){
                    minBottom = bottom2;
                }else{
                    minBottom = 0.91f;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        if (data.getSubtitle() != null) {
            ReplayUrlInfo.SubtitleData.SubtitleItem subtitleItem = data.getSubtitle();
            if (subtitleItem.getSize() != null) {
                localFirstSubtitleSize = 16;
                try {
                    localFirstSubtitleSize = Integer.parseInt(subtitleItem.getSize());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            String color = "0x333333";
            String surroundColor = "0x333333";
            if (subtitleItem.getColor()!=null){
                color = subtitleItem.getColor();
            }
            if (subtitleItem.getSurroundColor()!=null){
                surroundColor = subtitleItem.getSurroundColor();
            }

            int sort = subtitleItem.getSort();
            setFirstSubtitle(color, surroundColor, minBottom, sort);
        }

        if (data.getSubtitle2() != null) {
            ReplayUrlInfo.SubtitleData.SubtitleItem subtitleItem = data.getSubtitle2();
            if (subtitleItem.getSize() != null) {
                localSecondSubtitleSize = 16;
                try {
                    localSecondSubtitleSize = Integer.parseInt(subtitleItem.getSize());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            String color = "#333333";
            String surroundColor = "#333333";
            if (subtitleItem.getColor()!=null){
                color = subtitleItem.getColor();
            }
            if (subtitleItem.getSurroundColor()!=null){
                surroundColor = subtitleItem.getSurroundColor();
            }

            int sort = subtitleItem.getSort();
            setSecondSubtitle(color, surroundColor, minBottom, sort);
        }
        setDefaultSubtitle(data.getDefaultSubtitle());
        subtitleModel = data.getSubtitlemodel();
        post(new Runnable() {
            @Override
            public void run() {
                resetSubtitleSize(getMeasuredWidth());
            }
        });
    }


    public void loadSubtitle(String subtitleUrl) {
        if (TextUtils.isEmpty(subtitleUrl)) {
            return;
        }
        firstSubtitle = new Subtitle(new Subtitle.OnSubtitleInitedListener() {
            @Override
            public void onInited(Subtitle subtitle) {

            }
        });
        firstSubtitle.initSubtitleResource(subtitleUrl);

    }

    public void loadSubtitle2(String subtitleUrl) {
        if (TextUtils.isEmpty(subtitleUrl)) {
            return;
        }
        secondSubtitle = new Subtitle(new Subtitle.OnSubtitleInitedListener() {
            @Override
            public void onInited(Subtitle subtitle) {

            }
        });
        secondSubtitle.initSubtitleResource(subtitleUrl);

    }


    private void resetSubtitleSize(int width) {
        if (width == 0) {
            return;
        }
        proportion = (width / 480.00f) * 1.2f;
        Log.i(TAG,"接口字体大小："+localFirstSubtitleSize);
        float dp1 = DensityUtil.px2dp(getContext(),localFirstSubtitleSize);
        Log.i(TAG,"dp字体大小："+dp1);

        if (dp1<14f){
            dp1 = 14f;
        }else if (dp1>34f){
            dp1 = 34f;
        }
        Log.i(TAG,"最终dp字体大小："+dp1);

        tvFirstSubtitle.setTextSize(dp1);

        float dp2 = DensityUtil.px2dp(getContext(),localSecondSubtitleSize);
        if (dp2<14f){
            dp2 = 14f;
        }else if (dp2>34f){
            dp2 = 34f;
        }
        tv_second_subtitle.setTextSize(dp2);


    }

    private void setDefaultSubtitle(int defaultSubtitle) {
        if (defaultSubtitle == 0) {
            tvFirstSubtitle.setVisibility(View.VISIBLE);
            tv_second_subtitle.setVisibility(View.GONE);
        } else if (defaultSubtitle == 1) {
            tvFirstSubtitle.setVisibility(View.GONE);
            tv_second_subtitle.setVisibility(View.VISIBLE);
        } else {
            tvFirstSubtitle.setVisibility(View.VISIBLE);
            tv_second_subtitle.setVisibility(View.VISIBLE);
        }

    }

    private void setSecondSubtitle(String color, String surroundColor, double bottom, int sort) {
        try {
            if (!TextUtils.isEmpty(color)) {
                String newColor = color;
                if (color.contains("0x")) {
                    newColor = color.replace("0x", "#");
                }
                tv_second_subtitle.setTextColor(Color.parseColor(newColor));
                tv_second_subtitle.setShadowLayer(SHADOW_RADIUS, SHADOW_DX, SHADOW_DY, Color.YELLOW);
            }

            if (!TextUtils.isEmpty(surroundColor)) {
                String newSurroundColor = surroundColor;
                if (surroundColor.contains("0x")) {
                    newSurroundColor = surroundColor.replace("0x", "#");
                }
                tv_second_subtitle.setOutlineColor(Color.parseColor(newSurroundColor));
                tv_second_subtitle.setStrokeWidth(DensityUtil.dp2px(getContext(),2));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        secondBottomRate = bottom;
        secondSort = sort;
        if (bottom > 0) {
            int paddingBottom;
            Resources resources = getResources();
            if (resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                paddingBottom = (int) ((getHeight()-localSecondSubtitleSize) * bottom);
            } else {
                paddingBottom = (int) ((getHeight()-localSecondSubtitleSize) * bottom);
            }
            if (sort == 2) {
                commonBottom = paddingBottom;
            }
            secondBottom = paddingBottom;
            tv_second_subtitle.setPadding(10,10,10,paddingBottom);

        }
    }

    private void setFirstSubtitle(String color, String surroundColor, double bottom, int sort) {
        try {
            if (!TextUtils.isEmpty(color)) {
                String newColor = color;
                if (color.contains("0x")) {
                    newColor = color.replace("0x", "#");
                }
                tvFirstSubtitle.setTextColor(Color.parseColor(newColor));

            }
            if (!TextUtils.isEmpty(surroundColor)) {
                String newSurroundColor = surroundColor;
                if (surroundColor.contains("0x")) {
                    newSurroundColor = surroundColor.replace("0x", "#");
                }
                tvFirstSubtitle.setOutlineColor(Color.parseColor(newSurroundColor));
                tvFirstSubtitle.setStrokeWidth(DensityUtil.dp2px(getContext(),2));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        firstBottomRate = bottom;
        firstSort = sort;
        if (bottom > 0) {
            int paddingBottom;
            Resources resources = getResources();
            if (resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                paddingBottom = (int) ((getHeight()-localFirstSubtitleSize) * bottom);
            } else {
                paddingBottom = (int) ((getHeight()-localFirstSubtitleSize) * bottom);
            }
            if (sort == 1) {
                commonBottom = paddingBottom;
            }
            firstBottom = paddingBottom;
            tvFirstSubtitle.setPadding(10,10,10,paddingBottom);
            Log.i(TAG,"bottom:"+bottom);
            Log.i(TAG,"getHeight():"+getHeight());
            Log.i(TAG,"paddingBottom:"+paddingBottom);


        }
    }

    public void setLandScape(Context context, final boolean isLandScape) {
        post(new Runnable() {
            @Override
            public void run() {
                int baseHeight =  getHeight();
                if (firstBottomRate > 0) {
                    int paddingBottom = (int) (baseHeight * firstBottomRate);
                    firstBottom = paddingBottom;
                    tvFirstSubtitle.setPadding(10,10,10,paddingBottom);

                }

                if (secondBottomRate > 0) {
                    int paddingBottom = (int) (baseHeight * secondBottomRate);
                    secondBottom = paddingBottom;
                    tv_second_subtitle.setPadding(10,10,10,paddingBottom);
                }

            }
        });

    }



    /**
     * 更新字幕
     */
    public void refreshSubTitle(long currentPosition) {
        if (firstSubtitle != null) {
            String text = firstSubtitle.getSubtitleByTime(currentPosition);

            tvFirstSubtitle.setText(text);
        } else {
            tvFirstSubtitle.setVisibility(INVISIBLE);
        }

        if (secondSubtitle != null) {
            String text = secondSubtitle.getSubtitleByTime(currentPosition);

            tv_second_subtitle.setText(text);
        } else {
            tv_second_subtitle.setVisibility(INVISIBLE);
        }
    }



    /**
     * 获得第一种字幕名字
     */
    public String getFirstSubName() {
        return firstSubName;
    }

    /**
     * 获得第二种字幕名字
     */
    public String getSecondSubName() {
        return secondSubName;
    }

    /**
     * 获得当前选中的字幕
     */
    public int getSelectedSubtitle() {
        return selectedSubtitle;
    }

    /**
     * 控制字幕显示
     */
    public void setSubtitle(int selectedSub) {
        selectedSubtitle = selectedSub;
        if (selectedSub == 0) {
            tvFirstSubtitle.setVisibility(VISIBLE);
            tv_second_subtitle.setVisibility(GONE);
        } else if (selectedSub == 1) {
            tvFirstSubtitle.setVisibility(GONE);
            tv_second_subtitle.setVisibility(VISIBLE);
        } else if (selectedSub == 2) {
            tvFirstSubtitle.setVisibility(View.VISIBLE);
            tv_second_subtitle.setVisibility(View.VISIBLE);
        } else if (selectedSub == 3) {
            tvFirstSubtitle.setVisibility(View.GONE);
            tv_second_subtitle.setVisibility(View.GONE);
        }
    }


}
