package com.sheep.gamegroup.util;

import android.text.Layout;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.ShowAll;
import com.sheep.jiuyan.samllsheep.utils.G;

import rx.functions.Action1;

/**
 * Created by realicing on 2018/11/1.
 * realicing@sina.com
 * TextView 帮助类
 */
public class TextViewUtil {
    //监听TextView是否有省略号，返回数据 > 0 代表有省略
    public static void listenEllipsizeChange(final TextView textView, final Action1<Integer> action1) {
//        textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                Layout layout = textView.getLayout();
//                if (layout != null) {
//                    int lines = layout.getLineCount();
//                    action1.call(layout.getEllipsisCount(lines - 1));
//                }
//            }
//        });
        textView.postDelayed(new Runnable() {
            @Override
            public void run() {
                Layout layout = textView.getLayout();
                if (layout != null) {
                    int lines = layout.getLineCount();
                    action1.call(layout.getEllipsisCount(lines - 1));
                } else {
                    listenEllipsizeChange(textView, action1);
                }
            }
        }, 100L);

    }

    //初始化文本介绍，必须有 intro_content 和 intro_show_all
    public static void initIntroView(View itemView, final ShowAll showAll) {
        final TextView intro_content = itemView.findViewById(showAll.getIntroContentId());
        final TextView intro_show_all = itemView.findViewById(showAll.getIntroShowAllId());
        final View intro_show_all_no = itemView.findViewById(showAll.getIntroShowAllNoId());
        boolean isFirst = showAll.isFirst();

        if(showAll.isHasExp())
            ViewUtil.setH5ImgText(intro_content, showAll.getContent(), G.WIDTH - G.getRealPix(50));
        else
            ViewUtil.setText(intro_content, showAll.getContent());
        if (isFirst) {
            intro_content.setMaxLines(showAll.getMaxLine());
            TextViewUtil.listenEllipsizeChange(intro_content, new Action1<Integer>() {
                @Override
                public void call(Integer integer) {
                    boolean canShowAll = integer > 0;
                    showAll.setYes(false);
                    showAll.setCanShowAll(canShowAll);
                    intro_show_all.setText(showAll.getShowAllText());
                    if (canShowAll) {
                        ViewUtil.setVisibility(intro_show_all, true);
                        ViewUtil.setVisibility(intro_show_all_no, false);
                    }
                }
            });
        } else {
            boolean hasShowAll = showAll.isYes();
            intro_content.setMaxLines(hasShowAll ? Integer.MAX_VALUE : showAll.getMaxLine());
            intro_content.setEllipsize(hasShowAll ? null : TextUtils.TruncateAt.END);
            intro_show_all.setText(hasShowAll ? "收起" : showAll.getShowAllText());
            ViewUtil.setVisibility(intro_show_all, showAll.isCanShowAll());
            ViewUtil.setVisibility(intro_show_all_no, !showAll.isCanShowAll());
        }
        intro_show_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAll.setYes(!showAll.isYes());
                boolean hasShowAll = showAll.isYes();
                intro_content.setMaxLines(hasShowAll ? Integer.MAX_VALUE : showAll.getMaxLine());
                intro_content.setEllipsize(hasShowAll ? null : TextUtils.TruncateAt.END);
                intro_show_all.setText(hasShowAll ? "收起" : showAll.getShowAllText());
            }
        });
    }
}
