package com.kfzs.duanduan.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  TextViewList</p>
 * @ <p>Description:轮播功能的TextView</p>0
 * @ date:  2017/12/14 16:21
 * @ QQ:    315096953
 */

public class TextViewList extends LinearLayout {
    private Context mContext;
    private TextView textViews[] = new TextView[3];

    private LinearLayout llayout;

    private String curText = null;

    /***
     * 动画时间
     */
    private int mAnimTime = 500;

    /**
     * 停留时间
     */
    private int mStillTime = 3500;

    /***
     * 轮播的string
     */
    private List<String> mTextList;

    /***
     * 当前轮播的索引
     */
    private int currentIndex = 0;

    /***
     * 动画模式
     */
    private int animMode = 0;// 默认向上 0--向上，1--向下

    private boolean isAuto = true;//是否全自动

    private boolean isAttach = false;

    public final static int ANIM_MODE_UP = 0;
    public final static int ANIM_MODE_DOWN = 1;
    private final static int HANDLE_MSG = 1;

    private TranslateAnimation animationDown, animationUp;

    public TextViewList(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        if (!isInEditMode())
            initViews();
    }

    private void initViews() {
        llayout = new LinearLayout(mContext);
        llayout.setOrientation(LinearLayout.VERTICAL);
        this.addView(llayout);

        textViews[0] = addText();
        textViews[1] = addText();
        textViews[2] = addText();
    }

    /***
     * 当界面销毁时
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isAttach = false;
        //handler.removeMessages(HANDLE_MSG);// 防止内存泄漏的操作
        Log.e("滚动文本", "界面销毁移动handler" + this.toString());
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        isAttach = true;
        if (isAuto) {
            startAutoScroll();
        }
        Log.e("滚动文本", "onAttachedToWindow" + this.toString());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (!isInEditMode()) {
            post(new Runnable() {//7.0以后，需要界面可见时，设置宽高才有效果
                @Override
                public void run() {
                    setViewsHeight();
                }
            });
        }
    }

    public boolean isAuto() {
        return isAuto;
    }

    /**
     * 是否自动滚动
     *
     * @param auto
     */
    public void setAuto(boolean auto) {
        isAuto = auto;
    }

    /***
     * 重新设置VIEW的高度
     */
    private void setViewsHeight() {
        for (TextView tv : textViews) {
            LayoutParams lp = (LayoutParams) tv.getLayoutParams();
            lp.height = getHeight();
            lp.width = getWidth();
            tv.setLayoutParams(lp);
        }

        LayoutParams lp2 = (LayoutParams) llayout.getLayoutParams();
        lp2.height = getHeight() * (llayout.getChildCount());
        lp2.setMargins(0, -getHeight(), 0, 0);// 使向上偏移一定的高度，用padding,scrollTo都分有问题
        llayout.setLayoutParams(lp2);
    }

    // /////////////////////以下是一些基本的方法textView要用到///////////////////////////////////

    public void setGravity(int graty) {
        for (TextView tv : textViews) {
            tv.setGravity(graty);
        }
    }

    public void setTextSize(int dpSize) {
        for (TextView tv : textViews) {
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, dpSize);
        }
    }

    public void setTextColor(int color) {
        for (TextView tv : textViews) {
            tv.setTextColor(color);
        }
    }

    private TextView addText() {
        TextView tv = new TextView(mContext);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        llayout.addView(tv);
        return tv;
    }

    /***
     * 设置初始的字
     *
     * @param curText
     */
    public void setText(String curText) {
        this.curText = curText;
        textViews[1].setText(curText);
    }

    /***
     * 开始自动滚动
     */
    public void startAutoScroll() {
        handler.removeMessages(HANDLE_MSG);
        handler.sendEmptyMessageDelayed(HANDLE_MSG, mStillTime + mAnimTime);// 可用runnable来代替hander或者 timer
    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (mTextList != null && mTextList.size() > 0) {
                currentIndex++;
                currentIndex = (currentIndex) % mTextList.size();
                switch (animMode) {
                    case ANIM_MODE_UP:
                        setTextUpAnim(mTextList.get(currentIndex));
                        break;
                    case ANIM_MODE_DOWN:
                        setTextDownAnim(mTextList.get(currentIndex));
                        break;
                }
            }
            if (isAttach) {
                removeMessages(HANDLE_MSG);
                sendEmptyMessageDelayed(HANDLE_MSG, mStillTime + mAnimTime);
                if (onTextScrollListener != null) {
                    onTextScrollListener.onTextScroll();
                }
            }
        }
    };

    /***
     * 向上弹动画
     *
     * @param curText
     */
    public void setTextUpAnim(String curText) {
        this.curText = curText;
        textViews[2].setText(curText);
        up();// 向上的动画
    }

    public void setTextDownAnim(String text) {
        this.curText = text;
        textViews[0].setText(text);
        down();// 向上的动画
    }

    public void setDuring(int during) {
        this.mAnimTime = during;
    }

    /***
     * 向上动画
     */
    private void up() {
        llayout.clearAnimation();
        if (animationUp == null)
            animationUp = new TranslateAnimation(0, 0, 0, -getHeight());
        animationUp.setDuration(mAnimTime);
        llayout.startAnimation(animationUp);
        animationUp.setAnimationListener(listener);
    }

    /***
     * 向下动画
     */
    public void down() {
        llayout.clearAnimation();
        if (animationDown == null)
            animationDown = new TranslateAnimation(0, 0, 0, getHeight());
        animationDown.setDuration(mAnimTime);
        llayout.startAnimation(animationDown);
        animationDown.setAnimationListener(listener);
    }

    /***
     * 动画监听，动画完成后，动画恢复，设置文本
     */
    private AnimationListener listener = new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            setText(curText);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };

    public int getAnimTime() {
        return mAnimTime;
    }

    public void setAnimTime(int mAnimTime) {
        this.mAnimTime = mAnimTime;
    }

    public int getStillTime() {
        return mStillTime;
    }

    public void setStillTime(int mStillTime) {
        this.mStillTime = mStillTime;
    }

    public List<String> getTextList() {
        return mTextList;
    }

    public void setTextList(List<String> mTextList) {
        this.mTextList = mTextList;
    }

    public void addTextList(String mTextList) {
        if (this.mTextList == null) {
            this.mTextList = new ArrayList<>();
        }
        this.mTextList.add(mTextList);
    }

    public void clearTextList() {
        if (this.mTextList == null) {
            this.mTextList = new ArrayList<>();
            return;
        }
        this.mTextList.clear();
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    public int getAnimMode() {
        return animMode;
    }

    public void setAnimMode(int animMode) {
        this.animMode = animMode;
    }

    public void setSingleLine() {
        for (TextView tv : textViews) {
            tv.setSingleLine();
            tv.setEllipsize(TextUtils.TruncateAt.END);//尾部打省略号
        }
    }

    OnTextScrollListener onTextScrollListener;

    public void setOnTextScrollListener(OnTextScrollListener onTextScrollListener) {
        this.onTextScrollListener = onTextScrollListener;
    }

    public interface OnTextScrollListener {
        void onTextScroll();
    }

}