package cn.muchinfo.rma.view.base.chart.old.timeCharts;


import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.desfate.chart.ChartConstant;
import com.desfate.chart.GridChart;
import com.desfate.chart.MACandleStickChart;
import com.desfate.chart.MTPEnums;
import com.desfate.chart.MTPTabHost;
import com.desfate.chart.MTPTabHostListener;
import com.desfate.chart.MinusStickChart;
import com.desfate.chart.data.BidChartTotalDatas;
import com.desfate.chart.data.ChartBIASDatas;
import com.desfate.chart.data.ChartDMADatas;
import com.desfate.chart.data.ChartData;
import com.desfate.chart.data.ChartDataContainer;
import com.desfate.chart.data.ChartKDJDatas;
import com.desfate.chart.data.ChartPSYDatas;
import com.desfate.chart.data.ChartRSIDatas;
import com.desfate.chart.entity.LineEntity;
import com.desfate.chart.entity.StickEntity;
import com.desfate.chart.event.ITouchEventResponse;
import com.desfate.chart.ui.old.OTC.controls.TargetControl;
import com.desfate.chart.util.MathUtil;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import cn.muchinfo.rma.R;
import cn.muchinfo.rma.view.base.chart.old.OTCChartActivity;
import cn.muchinfo.rma.view.base.chart.old.controls.OTCChartControl;

/**
 * K线图对应的碎片
 */
public class KChartFragment extends Fragment {
    private View view;
    public int THEME_TEXT_COLOR = R.color.p_global_write_color;//
    private int mDecimalNum;// 小数点位数

    private String currentLine = "";// 当前选择的k线
    private PopupWindow popupWindow;

    public MACandleStickChart mtp_chart_k;// 蜡烛图
    public MinusStickChart mtp_chart_target;// 指标图
    private TextView mtp_target_btn;// 指标切换
    private TextView mtp_target_show;// 指标
    private ImageView imageView;
    private TextView loading_view_text;
    private long delay = 30 * 1000;// 定时器的时间
    private String currentMessage = "";// 当前显示的转圈的message
    private CountDownTimer progressDownTimer = null;
    private View loading_view;
    private Animation animation;
    private FrameLayout layout_bg;
    private List<HandleMessage> handleList = new ArrayList<HandleMessage>();
    private boolean initComplete = false;
    private static final int LOADLOCAL = 0;
    private static final int SHOW = 1;
    private MTPTabHost mtp_chart_k_tab;

    private class HandleMessage {
        public int funcode;
        public Object data;
    }

    public int getIndex() {
        return mtp_chart_k.getStartIndex();
    }

    public void setIndex(int index) {
        mtp_chart_k.setStartIndex(index);
    }

    private void handleMessage() {
        while (handleList.size() > 0) {
            HandleMessage message = handleList.get(0);
            switch (message.funcode) {
                case LOADLOCAL: {
                    Object[] datas = (Object[]) message.data;
                    TextView textView = (TextView) datas[0];
                    int reset = (Integer) datas[1];
                    loadLocalData(textView, reset == 1, false);
                }
                break;
                case SHOW: {
                    String msString = (String) message.data;
                    show(msString, false);
                }
                break;
                default:
                    break;
            }
            handleList.remove(0);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.otc_k_chart_framgent, container, false);
//        if(Config.MANAGEMENT){
//            THEME_TEXT_COLOR = R.color.manager_line_color;
//        }
        initViews();
        initListeners();
        initDatas();
        handleMessage();
        initComplete = true;
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden && initComplete) {
            handleMessage();
        }
        super.onHiddenChanged(hidden);
    }

    private void initViews() {
        mtp_chart_k_tab = view.findViewById(R.id.mtp_chart_k_tab);
        layout_bg = view.findViewById(R.id.layout_bg);

        layout_bg.setBackgroundColor(getResources().getColor(R.color.p_global_bg_color));
        mtp_target_show = view.findViewById(R.id.mtp_target_show);
        mtp_chart_k = view.findViewById(R.id.mtp_chart_k);
//        if(getActivity() instanceof OTCChartActivity) {
            mtp_chart_k.setCrossStarColor(((OTCChartActivity) getActivity()).getMtpChartControl().getNormalTextColor());
//        }else{
//            mtp_chart_k.setCrossStarColor(((ManagerChartAcitivity) getActivity()).getMtpChartControl().getNormalTextColor());
//        }

        loading_view = view.findViewById(R.id.loading_view);
        imageView = view.findViewById(R.id.loading_view_circle);
        loading_view_text = view.findViewById(R.id.loading_view_text);

        mtp_chart_target = view.findViewById(R.id.mtp_chart_target);
//        if(getActivity() instanceof OTCChartActivity) {
            mtp_chart_target.setCrossStarColor(((OTCChartActivity) getActivity()).getMtpChartControl().getNormalTextColor());
//        }else{
//            mtp_chart_target.setCrossStarColor(((ManagerChartAcitivity) getActivity()).getMtpChartControl().getNormalTextColor());
//        }
    }

    /**
     * 实例化指标控件
     *
     * @param minusstickchart <li>下
     */
    private void initMinusStickChart(MinusStickChart minusstickchart) {
        minusstickchart.setMaxSticksNum(ChartConstant.MAXSTICKNUM);
        minusstickchart.setAxisMarginRight(1);
        minusstickchart.setAxisMarginTop(5);
        int fontSize = getResources().getDimensionPixelSize(R.dimen.chart_font_size);
        minusstickchart.setLongitudeFontSize(fontSize);
        minusstickchart.setLatitudeFontSize(fontSize);

        minusstickchart.setBorderColor(Color.GRAY);
        minusstickchart.setAxisXColor(THEME_TEXT_COLOR);
        minusstickchart.setAxisYColor(THEME_TEXT_COLOR);
        minusstickchart.setLatitudeFontColor(Color.GRAY);
        minusstickchart.setLatitudeColor(Color.GRAY);
        minusstickchart.setLongitudeFontColor(Color.GRAY);
        minusstickchart.setLongitudeColor(Color.GRAY);

        minusstickchart.setBackgroundColor(getActivity().getResources().getColor(R.color.p_global_write_color));


        // 最大纬线数
        minusstickchart.setLatitudeNum(2);
        // 最大经线数
        minusstickchart.setLongitudeNum(1);
        // minusstickchart.setMaxValue(100);
        // minusstickchart.setMinValue(-100);
        minusstickchart.setStartIndex(-1);
        minusstickchart.setAxisMarginBottom(0);// 不绘制x轴坐标设为0
        minusstickchart.setDisplayAxisXTitle(false);
        minusstickchart.setDisplayAxisYTitle(true);
        minusstickchart.setDisplayLatitude(true);
        minusstickchart.setDisplayLongitude(true);
        minusstickchart.setStickBorderColor(Color.TRANSPARENT);
        minusstickchart.setStickFillColor(Color.GREEN);
        minusstickchart.setDisplayCrossXOnTouch(true);

        minusstickchart.setEnableCrossOnTouch(false);
    }

    /**
     * 实例化蜡烛图图表控件 上
     */
    private void initMACandleStickChart(MACandleStickChart macandlestickchart) {
        // macandlestickchart.cle
        int fontSize = getResources().getDimensionPixelSize(R.dimen.chart_font_size);
        macandlestickchart.setLongitudeFontSize(fontSize);
        macandlestickchart.setLatitudeFontSize(fontSize);

        macandlestickchart.setAxisXColor(Color.LTGRAY);
        macandlestickchart.setAxisYColor(Color.LTGRAY);
        macandlestickchart.setLatitudeColor(Color.GRAY);
        macandlestickchart.setLongitudeColor(Color.GRAY);
        macandlestickchart.setBorderColor(Color.LTGRAY);
        macandlestickchart.setLongitudeFontColor(Color.GRAY);
        macandlestickchart.setLatitudeFontColor(Color.GRAY);
        macandlestickchart.setAxisMarginRight(1);
        macandlestickchart.setAxisMarginTop(5);
        // 最大显示足数
        macandlestickchart.setMaxSticksNum(ChartConstant.MAXSTICKNUM);
        // 最大纬线数
        macandlestickchart.setLatitudeNum(4);
        // 最大经线数
        macandlestickchart.setLongitudeNum(1);
        macandlestickchart.setStartIndex(-1);

        macandlestickchart.setDisplayAxisXTitle(true);
        macandlestickchart.setDisplayAxisYTitle(true);
        macandlestickchart.setDisplayLatitude(true);
        macandlestickchart.setDisplayLongitude(true);
        macandlestickchart.setBackgroundColor(getActivity().getResources().getColor(R.color.p_global_write_color));

        macandlestickchart.setDisplayCrossXOnTouch(true);
        macandlestickchart.setDisplayCrossYOnTouch(true);
    }

    private void initListeners() {
        mtp_chart_k_tab.setOnMtpTabHostListener(new MTPTabHostListener() {
            @Override
            public void onTabChanged(int current, int last, View view) {
                if (current == last) {
                    return;
                }
                setStickChart();
                showSum(mtp_chart_target, 0);
            }
        });
    }

    public void startAnimation() {
        imageView.startAnimation(animation);
    }

    private void initDatas() {
        animation = AnimationUtils.loadAnimation(getActivity(), R.anim.loading_dialog_anim);
        animation.setInterpolator(new LinearInterpolator());
        startAnimation();
        if (OTCChartControl.getInstance(getActivity()).getGoodsInfo() != null) {
            mDecimalNum = OTCChartControl.getInstance(getActivity()).getGoodsInfo().getGoodsInfo().getDecimalplace();
        }

        initMACandleStickChart(mtp_chart_k);
        initMinusStickChart(mtp_chart_target);

        // 下面三行代码主要是同步上下两个图表的动作，
        mtp_chart_k.addNotify(mtp_chart_target);
        mtp_chart_k.addNotify((ITouchEventResponse) getActivity());// FIXME :
        // 这里比较关键，某些事件OTCChartActivity.java需要监控到
        mtp_chart_target.addNotify(mtp_chart_k);

        String[] tem = null;
        if(getActivity() instanceof OTCChartActivity){
//            if (((OTCChartActivity) getActivity()).getTradeMode() == MTPEnums.TRADEMODE_BIDDING) {// 竞价
//                tem = getResources().getStringArray(R.array.chart_k_tab_bid);
//            } else if (((OTCChartActivity) getActivity()).getTradeMode() == MTPEnums.TRADEMODE_MARKETMAKE) {// 做市--连续现货
//                tem = getResources().getStringArray(R.array.chart_k_tab_market);
//            } else if (((OTCChartActivity) getActivity()).getTradeMode() == MTPEnums.TRADEMODE_OPTION) {
//                tem = getResources().getStringArray(R.array.chart_k_tab_market);
//            } else {
//                tem = getResources().getStringArray(R.array.chart_k_tab_market);
//            }
        }

        mtp_chart_k_tab.show(tem, false);
        mtp_chart_k_tab.selectTab(mtp_chart_k_tab.getCurrentIndxe());
    }

    /**
     * 加载本地数据绘制
     *
     * @param titleView
     * @param reset
     */
    public void loadLocalData(TextView titleView, boolean reset, boolean delay) {
        if (delay && !initComplete) {
            HandleMessage message = new HandleMessage();
            message.data = new Object[]{titleView, reset ? 1 : 0};
            handleList.add(message);
        } else {
            onHistoryDataCallBack(titleView, reset, true);
        }
    }

    /**
     *
     */
    public void resetMaxStickNumber() {
        if (mtp_chart_k != null && mtp_chart_target != null) {
            this.mtp_chart_k.setMaxSticksNum(ChartConstant.MAXSTICKNUM);
            this.mtp_chart_target.setMaxSticksNum(ChartConstant.MAXSTICKNUM);
        }
    }

    public void showKChart() {
        if (mtp_chart_k != null && mtp_chart_target != null) {
            mtp_chart_k.setVisibility(View.VISIBLE);
            mtp_chart_target.setVisibility(View.VISIBLE);
        }
    }

    public void hideKChart() {
        if (mtp_chart_k != null && mtp_chart_target != null) {
            mtp_chart_k.setVisibility(View.INVISIBLE);
            mtp_chart_target.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 历史行情返回
     */
    public void onHistoryDataCallBack(TextView titleView, boolean reset, boolean needSet) {
        try {
            ChartDataContainer container = OTCChartControl.getInstance(getActivity()).get();
            if (container == null || container.mOHLCList == null || container.mOHLCList.isEmpty()) {
                container = new ChartDataContainer();
            }
            if (mtp_chart_k == null) {
                return;
            }
            if (container.mOHLCList.size() > mtp_chart_k.getMaxSticksNum()) {
                mtp_chart_k.setMaxSticksNum(mtp_chart_k.getMaxSticksNum());
                mtp_chart_target.setMaxSticksNum(mtp_chart_k.getMaxSticksNum());
            } else {
                // if (container.mOHLCList.size() > 0) {
                // mtp_chart_k.setMaxSticksNum(container.mOHLCList.size());
                // mtp_chart_target.setMaxSticksNum(container.mOHLCList.size());
                // }
            }
            if (needSet) {
                if (reset) {
                    mtp_chart_k.setNotifyAxisMarginLeft(GridChart.DEFAULT_AXIS_MARGIN_LEFT);
                    mtp_chart_k.setStartIndex(-1);
                    mtp_chart_target.setNotifyAxisMarginLeft(GridChart.DEFAULT_AXIS_MARGIN_LEFT);
                    mtp_chart_target.setStartIndex(-1);
                } else {
                    mtp_chart_k.setStartIndex(OTCChartControl.getInstance(getActivity()).getStartIndex());
                    mtp_chart_target.setStartIndex(OTCChartControl.getInstance(getActivity()).getStartIndex());
                }
            }
            // 记录位置
            container.mSize = container.mOHLCList.size();
            setMACandleStickData(container);
            LineEntity MA5 = mtp_chart_k.getLineData().get(0);
            LineEntity MA10 = mtp_chart_k.getLineData().get(1);
            LineEntity MA15 = mtp_chart_k.getLineData().get(2);
            if (MA5.getLineData() == null || MA10.getLineData() == null || MA15.getLineData() == null) {
                setStickChart();
                showSum(mtp_chart_target, 0);
                return;
            }
            double ma5Val = MA5.getLineData().get(MA5.getLineData().size() - 1);
            double ma10Val = MA10.getLineData().get(MA10.getLineData().size() - 1);
            double ma15Val = MA15.getLineData().get(MA15.getLineData().size() - 1);
            OTCChartControl.getInstance(getActivity()).setCandleStickText(titleView, container.mOHLCList.get(container.mSize - 1), ma5Val,
                    ma10Val, ma15Val);
            setStickChart();
            showKChart();
            mtp_chart_k.invalidate();
            mtp_chart_target.invalidate();
            showSum(mtp_chart_target, 0);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void showKTitle(TextView titleView) {
        ChartDataContainer container = OTCChartControl.getInstance(getActivity()).get();
        if (container == null) {
            return;
        }
        LineEntity MA5 = mtp_chart_k.getLineData().get(0);
        LineEntity MA10 = mtp_chart_k.getLineData().get(1);
        LineEntity MA15 = mtp_chart_k.getLineData().get(2);
        if (MA5.getLineData() == null || MA10.getLineData() == null || MA15.getLineData() == null) {
            return;
        }
        double ma5Val = MA5.getLineData().get(MA5.getLineData().size() - 1);
        double ma10Val = MA10.getLineData().get(MA10.getLineData().size() - 1);
        double ma15Val = MA15.getLineData().get(MA15.getLineData().size() - 1);
        OTCChartControl.getInstance(getActivity()).setCandleStickText(titleView, container.mOHLCList.get(container.mSize - 1), ma5Val,
                ma10Val, ma15Val);
    }

    public void showTargetTitle(int index) {
        setStickChart();
        showSum(mtp_chart_target, index);
    }

    // -------------------------------------------- PSY\RSI 14\MDA\BIAS\CCI\KDJ
    // -------------------------------------

    /**
     * 根据mtp_target_btn显示的text来显示
     */
    private void setStickChart() {
        try {
            String textString = mtp_chart_k_tab.getCurrentValue();

            if (getString(R.string.mtp_rsi).equals(textString)) {
                setRSILine(mtp_chart_target, OTCChartControl.getInstance(getActivity()).get());
            } else if (getString(R.string.mtp_macd).equals(textString)) {
                setMinusStickData(mtp_chart_target, OTCChartControl.getInstance(getActivity()).get());
            } else if (getString(R.string.mtp_psy).equals(textString)) {
                setPSYLine(mtp_chart_target, OTCChartControl.getInstance(getActivity()).get());
            } else if (getString(R.string.mtp_cci).equals(textString)) {
                setCCILine(mtp_chart_target, OTCChartControl.getInstance(getActivity()).get());
            } else if (getString(R.string.mtp_dma).equals(textString)) {
                setDMALine(mtp_chart_target, OTCChartControl.getInstance(getActivity()).get());
            } else if (getString(R.string.mtp_kdj).equals(textString)) {
                setKDJLine(mtp_chart_target, OTCChartControl.getInstance(getActivity()).get());
            } else if (getString(R.string.mtp_bias).equals(textString)) {
                setBIASLine(mtp_chart_target, OTCChartControl.getInstance(getActivity()).get());
            } else if (getString(R.string.tas_total).equals(textString)) {
                setBidTotalLine(mtp_chart_target, OTCChartControl.getInstance(getActivity()).get());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setKDJLine(MinusStickChart minusstickchart, ChartDataContainer dataContainer) {
        if (dataContainer.mSize == 0) {
            minusstickchart.postInvalidate();
            return;
        }
        ChartKDJDatas chartKDJDatas = new TargetControl().getKDJs(dataContainer);
        List<LineEntity> lines = new ArrayList<LineEntity>();

        /**
         * k
         */
        LineEntity k = new LineEntity();
        k.setLineData(chartKDJDatas.getK());
        if(getActivity() instanceof OTCChartActivity) {
            k.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getKDJKColor());
        }
        k.setStartIndex(8);
        lines.add(k);

        /**
         * d
         */
        LineEntity d = new LineEntity();
        d.setLineData(chartKDJDatas.getD());
        if(getActivity() instanceof OTCChartActivity) {
            d.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getKDJDColor());
        }
        d.setStartIndex(8);
        lines.add(d);

        /**
         * j
         */
        LineEntity j = new LineEntity();
        j.setLineData(chartKDJDatas.getJ());
        if(getActivity() instanceof OTCChartActivity) {
            j.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getKDJJColor());
        }
        j.setStartIndex(8);
        lines.add(j);

        minusstickchart.setLineData(lines);

        minusstickchart.setShowStick(false);
        List<StickEntity> stickDataList = new ArrayList<StickEntity>();
        StickEntity stickEntity = null;
        /**
         * 计算出当前数据的最大最小值
         */
        for (int i = 0; i < chartKDJDatas.getK().size(); i++) {
            if (i >= stickDataList.size()) {
                stickEntity = new StickEntity();
                stickDataList.add(stickEntity);
            }
            if (stickDataList.get(i).getHigh() < chartKDJDatas.getK().get(i) || stickDataList.get(i).getHigh() == 0) {
                stickDataList.get(i).setHigh(chartKDJDatas.getK().get(i));
            }
            if (stickDataList.get(i).getLow() > chartKDJDatas.getK().get(i)) {
                stickDataList.get(i).setLow(chartKDJDatas.getK().get(i));
            }
        }
        for (int i = 0; i < chartKDJDatas.getD().size(); i++) {
            if (i >= stickDataList.size()) {
                stickEntity = new StickEntity();
                stickDataList.add(stickEntity);
            }
            if (stickDataList.get(i).getHigh() < chartKDJDatas.getD().get(i)) {
                stickDataList.get(i).setHigh(chartKDJDatas.getD().get(i));
            }
            if (stickDataList.get(i).getLow() > chartKDJDatas.getD().get(i)) {
                stickDataList.get(i).setLow(chartKDJDatas.getD().get(i));
            }
        }
        for (int i = 0; i < chartKDJDatas.getJ().size(); i++) {
            if (i >= stickDataList.size()) {
                stickEntity = new StickEntity();
                stickDataList.add(stickEntity);
            }
            if (stickDataList.get(i).getHigh() < chartKDJDatas.getJ().get(i)) {
                stickDataList.get(i).setHigh(chartKDJDatas.getJ().get(i));
            }
            if (stickDataList.get(i).getLow() > chartKDJDatas.getJ().get(i)) {
                stickDataList.get(i).setLow(chartKDJDatas.getJ().get(i));
            }
        }
        minusstickchart.setStickData(stickDataList);

        minusstickchart.setDecimalNum(mDecimalNum);
        minusstickchart.postInvalidate();

    }

    /**
     * 竞价总量
     *
     * @param minusstickchart
     * @param dataContainer
     */
    private void setBidTotalLine(MinusStickChart minusstickchart, ChartDataContainer dataContainer) {
        if (dataContainer.mSize == 0) {
            return;
        }
        BidChartTotalDatas bidChartTotalDatas = new TargetControl().getBidTotalData(dataContainer);
        List<LineEntity> lines = new ArrayList<LineEntity>();

        LineEntity total5 = new LineEntity();
        total5.setStartIndex(5);
        total5.setLineData(bidChartTotalDatas.getTotal5());
        if(getActivity() instanceof OTCChartActivity) {
            total5.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getVOL5Color());
        }
        lines.add(total5);

        LineEntity total10 = new LineEntity();
        total10.setStartIndex(10);
        total10.setLineData(bidChartTotalDatas.getTotal10());
        if(getActivity() instanceof OTCChartActivity) {
            total10.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getVOL10Color());
        }
        lines.add(total10);

        minusstickchart.setLineData(lines);
        minusstickchart.setShowStick(true);
        List<StickEntity> stickDataList = new ArrayList<StickEntity>();
        StickEntity tem = null;
        for (int i = 0; i < dataContainer.mOHLCList.size(); i++) {
            tem = new StickEntity();
            try {
                tem.setHigh(Double.parseDouble(((ChartData) dataContainer.mOHLCList.get(i)).getVolume()));
                if (dataContainer.mOHLCList.get(i).getClose() > dataContainer.mOHLCList.get(i).getOpen()) {
                    tem.setColorFlag(1);
                } else if (dataContainer.mOHLCList.get(i).getClose() == dataContainer.mOHLCList.get(i).getOpen()) {
                    tem.setColorFlag(0);
                } else {
                    tem.setColorFlag(-1);
                }
                stickDataList.add(tem);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        minusstickchart.setStickData(stickDataList);
        minusstickchart.setDecimalNum(mDecimalNum);
        minusstickchart.postInvalidate();
    }

    private void setBIASLine(MinusStickChart minusstickchart, ChartDataContainer dataContainer) {
        if (dataContainer.mSize == 0) {
            minusstickchart.postInvalidate();
            return;
        }

        ChartBIASDatas chartBIASDatas = new TargetControl().getBIASs(dataContainer);
        List<LineEntity> lines = new ArrayList<LineEntity>();

        /**
         * bias6
         */
        LineEntity bias6 = new LineEntity();
        bias6.setLineData(chartBIASDatas.getBias6());
        if(getActivity() instanceof OTCChartActivity) {
            bias6.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getBIAS6Color());
        }
        bias6.setStartIndex(5);
        lines.add(bias6);

        /**
         * bias12
         */
        LineEntity bias12 = new LineEntity();
        bias12.setLineData(chartBIASDatas.getBias12());
        if(getActivity() instanceof OTCChartActivity) {
            bias12.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getBIAS12Color());
        }
        bias12.setStartIndex(11);
        lines.add(bias12);

        /**
         * bias24
         */
        LineEntity bias24 = new LineEntity();
        bias24.setLineData(chartBIASDatas.getBias24());
        if(getActivity() instanceof OTCChartActivity) {
            bias24.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getBIAS24Color());
        }
        bias24.setStartIndex(23);
        lines.add(bias24);

        minusstickchart.setLineData(lines);

        minusstickchart.setShowStick(false);
        List<StickEntity> stickDataList = new ArrayList<StickEntity>();
        StickEntity stickEntity = null;

        for (int i = 0; i < chartBIASDatas.getBias6().size(); i++) {
            if (i >= stickDataList.size()) {
                stickEntity = new StickEntity();
                stickDataList.add(stickEntity);
            }
            if (stickDataList.get(i).getHigh() < chartBIASDatas.getBias6().get(i)) {
                stickDataList.get(i).setHigh(chartBIASDatas.getBias6().get(i));
            }
            if (stickDataList.get(i).getLow() > chartBIASDatas.getBias6().get(i)) {
                stickDataList.get(i).setLow(chartBIASDatas.getBias6().get(i));
            }
        }
        for (int i = 0; i < chartBIASDatas.getBias12().size(); i++) {
            if (i >= stickDataList.size()) {
                stickEntity = new StickEntity();
                stickDataList.add(stickEntity);
            }
            if (stickDataList.get(i).getHigh() < chartBIASDatas.getBias12().get(i)) {
                stickDataList.get(i).setHigh(chartBIASDatas.getBias12().get(i));
            }
            if (stickDataList.get(i).getLow() > chartBIASDatas.getBias12().get(i)) {
                stickDataList.get(i).setLow(chartBIASDatas.getBias12().get(i));
            }
        }
        for (int i = 0; i < chartBIASDatas.getBias24().size(); i++) {
            if (i >= stickDataList.size()) {
                stickEntity = new StickEntity();
                stickDataList.add(stickEntity);
            }
            if (stickDataList.get(i).getHigh() < chartBIASDatas.getBias24().get(i)) {
                stickDataList.get(i).setHigh(chartBIASDatas.getBias24().get(i));
            }
            if (stickDataList.get(i).getLow() > chartBIASDatas.getBias24().get(i)) {
                stickDataList.get(i).setLow(chartBIASDatas.getBias24().get(i));
            }
        }

        minusstickchart.setStickData(stickDataList);

        minusstickchart.setDecimalNum(mDecimalNum);
        minusstickchart.postInvalidate();
    }

    private void setDMALine(MinusStickChart minusstickchart, ChartDataContainer dataContainer) {
        if (dataContainer.mSize == 0) {
            minusstickchart.postInvalidate();
            return;
        }
        ChartDMADatas chartDMADatas = new TargetControl().getDMAs(dataContainer);
        List<LineEntity> lines = new ArrayList<LineEntity>();

        /**
         * DMA
         */
        LineEntity dma = new LineEntity();
        dma.setLineData(chartDMADatas.getDMA());
        if(getActivity() instanceof OTCChartActivity) {
            dma.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getDMAColor());
        }
        dma.setStartIndex(49);
        lines.add(dma);

        /**
         * AMA
         */
        LineEntity ama = new LineEntity();
        ama.setLineData(chartDMADatas.getAMA());
        if(getActivity() instanceof OTCChartActivity) {
            ama.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getDMAAMAColor());
        }
        ama.setStartIndex(5);
        lines.add(ama);

        minusstickchart.setLineData(lines);

        minusstickchart.setShowStick(false);
        minusstickchart.setStickData(dataContainer.mStickList.subList(0, dataContainer.mSize));

        minusstickchart.setDecimalNum(mDecimalNum);
        minusstickchart.postInvalidate();
    }

    /**
     * cci
     *
     * @param minusstickchart
     * @param dataContainer
     */
    private void setCCILine(MinusStickChart minusstickchart, ChartDataContainer dataContainer) {
        if (dataContainer.mSize == 0) {
            minusstickchart.postInvalidate();
            return;
        }
        List<Double> cci = new TargetControl().getCCIs(dataContainer);
        List<LineEntity> lines = new ArrayList<LineEntity>();

        /**
         * cci
         */
        LineEntity cciLine = new LineEntity();
        cciLine.setLineData(cci);
        if(getActivity() instanceof OTCChartActivity) {
            cciLine.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getCCIColor());
        }
        cciLine.setStartIndex(12);
        lines.add(cciLine);

        minusstickchart.setLineData(lines);

        minusstickchart.setShowStick(false);
        minusstickchart.setStickData(dataContainer.mStickList.subList(0, dataContainer.mSize));

        minusstickchart.setDecimalNum(mDecimalNum);
        minusstickchart.postInvalidate();
    }

    /**
     * xia
     *
     * @param minusstickchart
     * @param dataContainer
     */
    private void setMinusStickData(MinusStickChart minusstickchart, ChartDataContainer dataContainer) {
        if (dataContainer.mSize == 0) {
            minusstickchart.postInvalidate();
            return;
        }
        List<LineEntity> lines = new ArrayList<LineEntity>();

        // 计算 DEA 的移动平均
        LineEntity DEA = new LineEntity();
        if(getActivity() instanceof OTCChartActivity) {
            DEA.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getMACDDEAColor());
        }

        // 计算 DIF 线，这个就是 MACD 的核心线
        LineEntity DIF = new LineEntity();
        if(getActivity() instanceof OTCChartActivity) {
            DIF.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getMACDDIFColor());
        }
        List<Double> DEAValues = new ArrayList<Double>();
        List<Double> DIFValues = new ArrayList<Double>();
        ChartData chartData;
        for (int i = 0; i < dataContainer.mSize; i++) {
            if (dataContainer.mSize != i) {
                chartData = (ChartData) dataContainer.mOHLCList.get(i);
                DEAValues.add(chartData.getDEA());
                DIFValues.add(chartData.getDIF());
            }
        }

        DEA.setLineData(DEAValues);
        if(getActivity() instanceof OTCChartActivity) {
            DEA.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getMACDDEAColor());
        }
        lines.add(DEA);
        DIF.setLineData(DIFValues);
        lines.add(DIF);
        minusstickchart.setLineData(lines);
        minusstickchart.setShowStick(true);
        minusstickchart.setStickData(dataContainer.mStickList.subList(0, dataContainer.mSize));
        minusstickchart.setDecimalNum(mDecimalNum);
        minusstickchart.postInvalidate();
    }

    /**
     * RSI 周期为6
     *
     * @param minusstickchart
     * @param dataContainer
     */
    private void setRSILine(MinusStickChart minusstickchart, ChartDataContainer dataContainer) {
        if (dataContainer.mSize == 0) {
            minusstickchart.postInvalidate();
            return;
        }

        /**
         * RSI 5
         */
        List<LineEntity> lines = new ArrayList<LineEntity>();
        ChartRSIDatas rsiData = new TargetControl().getRSI(dataContainer);

        LineEntity rsi = new LineEntity();
        rsi.setLineData(rsiData.getRsi6());
        if(getActivity() instanceof OTCChartActivity) {
            rsi.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getRSI6Color());
        }
        rsi.setStartIndex(5);
        lines.add(rsi);

        LineEntity rsi12 = new LineEntity();
        rsi12.setLineData(rsiData.getRsi12());
        if(getActivity() instanceof OTCChartActivity) {
            rsi12.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getRSI12Color());
        }
        rsi12.setStartIndex(11);
        lines.add(rsi12);


        LineEntity rsi24 = new LineEntity();
        rsi24.setLineData(rsiData.getRsi24());
        if(getActivity() instanceof OTCChartActivity) {
            rsi24.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getRSI24Color());
        }
        rsi24.setStartIndex(23);
        lines.add(rsi24);

        minusstickchart.setLineData(lines);

        List<StickEntity> entities = new ArrayList<StickEntity>();
        StickEntity entity = null;
        for (int i = 0; i < dataContainer.mStickList.subList(0, dataContainer.mSize).size(); i++) {
            entity = new StickEntity();
            entity.setHigh(100.0);
            entity.setLow(0.0);
            entities.add(entity);
        }

        minusstickchart.setShowStick(false);
        minusstickchart.setStickData(entities);
        minusstickchart.setDecimalNum(mDecimalNum);
        minusstickchart.postInvalidate();
    }

    /**
     * 26 6 PSY
     *
     * @param minusstickchart
     * @param dataContainer
     */
    private void setPSYLine(MinusStickChart minusstickchart, ChartDataContainer dataContainer) {
        if (dataContainer.mSize == 0) {
            return;
        }
        ChartPSYDatas chartPSYDatas = new TargetControl().getPSYs(dataContainer);
        List<LineEntity> lines = new ArrayList<LineEntity>();

        /**
         * psy
         */
        LineEntity psy = new LineEntity();
        psy.setLineData(chartPSYDatas.getPsy());
        if(getActivity() instanceof OTCChartActivity) {
            psy.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getPSYColor());
        }
        psy.setStartIndex(25);
        lines.add(psy);

        /**
         * psyMa
         */
        LineEntity psyMa = new LineEntity();
        psyMa.setLineData(chartPSYDatas.getPsyMa());
            if(getActivity() instanceof OTCChartActivity) {
                psyMa.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getPSYMAColor());
            }
        psyMa.setStartIndex(25);
        lines.add(psyMa);

        minusstickchart.setLineData(lines);

        minusstickchart.setShowStick(false);
        minusstickchart.setStickData(dataContainer.mStickList.subList(0, dataContainer.mSize));

        minusstickchart.setDecimalNum(mDecimalNum);
        minusstickchart.postInvalidate();
    }

    private int getNowPriceColor() {
        try {
            if(getActivity() instanceof OTCChartActivity) {
//                if (((OTCChartActivity) getActivity()).getGoodsInfoAndQuotes().getQuoteDayData().getUpDown() == 1) {
//                    // 红涨
//                    return getResources().getColor(R.color.p_up_color);
//                } else if (((OTCChartActivity) getActivity()).getGoodsInfoAndQuotes().getQuoteDayData().getUpDown() == 2) {
//                    // 绿跌
//                    return getResources().getColor(R.color.p_down_color);
//                }
            }
        } catch (Resources.NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return getResources().getColor(R.color.p_up_color);
    }

    private void setMACandleStickData(ChartDataContainer dataContainer) {
        List<LineEntity> lines = new ArrayList<LineEntity>();

        // 计算5日均线
        LineEntity MA5 = new LineEntity();
        MA5.setTitle("MA5");
        if(getActivity() instanceof OTCChartActivity) {
            MA5.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getMA5Color());
        }
        MA5.setLineData(OTCChartControl.getInstance(getActivity()).initMA(5, dataContainer));
        lines.add(MA5);

        // 计算10日均线
        LineEntity MA10 = new LineEntity();
        MA10.setTitle("MA10");
        if(getActivity() instanceof OTCChartActivity) {
            MA10.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getMA10Color());
        }
        MA10.setLineData(OTCChartControl.getInstance(getActivity()).initMA(10, dataContainer));
        lines.add(MA10);

        // 计算15日均线
        LineEntity MA15 = new LineEntity();
        MA15.setTitle("MA15");
        if(getActivity() instanceof OTCChartActivity) {
            MA15.setLineColor(((OTCChartActivity) getActivity()).getMtpChartControl().getMA15Color());
        }
        MA15.setLineData(OTCChartControl.getInstance(getActivity()).initMA(15, dataContainer));
        lines.add(MA15);
        try {
            mtp_chart_k.setTasLastPrice(String.valueOf(OTCChartControl.getInstance(getActivity()).getGoodsInfo().getQuoteDayData().getLast()));
            mtp_chart_k.setNewPriceColor(getNowPriceColor());
        } catch (Exception e) {
            mtp_chart_k.setTasLastPrice("0");
            e.printStackTrace();
        }

        mtp_chart_k.setLineData(lines);
        if (dataContainer.mOHLCList.size() > 0 && dataContainer.mOHLCList.size() >= dataContainer.mSize) {
            // 这里是将k线数据赋值进去
            // if(mtp_chart_k.getStartIndex() > 0){
            // // 如果index > 0，说明有数据
            // // 如果之前的位置是最新的位置，那么刷数据的时候也移到最新的一条
            // if(mtp_chart_k.getOHLCData().size() -
            // mtp_chart_k.getMaxSticksNum() <= 0
            // || ){
            // mtp_chart_k.setNotifyAxisMarginLeft(GridChart.DEFAULT_AXIS_MARGIN_LEFT);
            // mtp_chart_k.setStartIndex(-1);
            // mtp_chart_target.setNotifyAxisMarginLeft(GridChart.DEFAULT_AXIS_MARGIN_LEFT);
            // mtp_chart_target.setStartIndex(-1);
            // }
            // }
            mtp_chart_k.setOHLCData(dataContainer.mOHLCList.subList(0, dataContainer.mSize));
        }
        mtp_chart_k.setDecimalNum(mDecimalNum);
        mtp_chart_k.postInvalidate();
    }

    /**
     * 显示最后一笔的值
     *
     * @param minusStickChart
     * @param index
     */
    private void showSum(MinusStickChart minusStickChart, int index) {
        try {
            String textString = mtp_chart_k_tab.getCurrentValue();
            ChartDataContainer dataContainer = OTCChartControl.getInstance(getActivity()).get();
            if (getString(R.string.mtp_rsi).equals(textString)) {
                showRSISum(minusStickChart, dataContainer, index);
            } else if (getString(R.string.mtp_macd).equals(textString)) {
                showMACDSum(minusStickChart, dataContainer, index);
            } else if (getString(R.string.mtp_psy).equals(textString)) {
                showPSYSum(minusStickChart, dataContainer, index);
            } else if (getString(R.string.mtp_cci).equals(textString)) {
                showCCISum(minusStickChart, dataContainer, index);
            } else if (getString(R.string.mtp_dma).equals(textString)) {
                showDMASum(minusStickChart, dataContainer, index);
            } else if (getString(R.string.mtp_kdj).equals(textString)) {
                showKDJSum(minusStickChart, dataContainer, index);
            } else if (getString(R.string.mtp_bias).equals(textString)) {
                showBIASSum(minusStickChart, dataContainer, index);
            } else if (getString(R.string.tas_total).equals(textString)) {
                showTotalSum(minusStickChart, dataContainer, index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param minusStickChart
     * @param dataContainer
     * @param index
     */
    private void showTotalSum(MinusStickChart minusStickChart, ChartDataContainer dataContainer, int index) {
        if (dataContainer == null || dataContainer.mOHLCList == null || dataContainer.mOHLCList.isEmpty()) {
            mtp_target_show.setText("");
            return;
        }
        String bias = "<font color='%s'> VOL5：%s </font><font color='%s'> VOL10：%s </font><font color='%s'> VOLNUM：%s </font>";
        String htmlString = "";
        String total5 = "";
        String total10 = "";
        String totalNum = "";
        try {
            index = index == 0 ? minusStickChart.getNewLineData().get(0).getLineData().size() - 1 : index;
            total5 = MathUtil.roundNum(minusStickChart.getNewLineData().get(0).getLineData().get(index), 2);
            total10 = MathUtil.roundNum(minusStickChart.getNewLineData().get(1).getLineData().get(index), 2);
            totalNum = "" + (int) minusStickChart.getNewStickData().get(index).getHigh();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(getActivity() instanceof OTCChartActivity) {
            htmlString = String.format(bias, ((OTCChartActivity) getActivity()).getMtpChartControl().getVOL5Color(), total5,
                    ((OTCChartActivity) getActivity()).getMtpChartControl().getVOL10Color(), total10, Color.RED, totalNum);
        }
        mtp_target_show.setText(Html.fromHtml(htmlString));
    }

    /**
     * BIAS6 BIAS12 BIAS24
     *
     * @param minusStickChart
     * @param dataContainer
     * @param index
     */
    private void showBIASSum(MinusStickChart minusStickChart, ChartDataContainer dataContainer, int index) {
        if (dataContainer == null || dataContainer.mOHLCList == null || dataContainer.mOHLCList.isEmpty()) {
            mtp_target_show.setText("");
            return;
        }
        String bias = "<font color='%s'> BIAS6：%s </font><font color='%s'> BIAS12：%s </font><font color='%s'> BIAS24：%s</font>";
        String htmlString = "";
        String bias6String = "0.0";
        String bias12String = "0.0";
        String bias24String = "0.0";
        try {
            index = index == 0 ? minusStickChart.getNewLineData().get(0).getLineData().size() - 1 : index;
            bias6String = MathUtil.roundNum(minusStickChart.getNewLineData().get(0).getLineData().get(index), 2);
            bias12String = MathUtil.roundNum(minusStickChart.getNewLineData().get(1).getLineData().get(index), 2);
            bias24String = MathUtil.roundNum(minusStickChart.getNewLineData().get(2).getLineData().get(index), 2);
        } catch (Exception e) {
        }
        if(getActivity() instanceof OTCChartActivity) {
            htmlString = String.format(bias, ((OTCChartActivity) getActivity()).getMtpChartControl().getBIAS6Color(), bias6String,
                    ((OTCChartActivity) getActivity()).getMtpChartControl().getBIAS12Color(), bias12String, ((OTCChartActivity) getActivity())
                            .getMtpChartControl().getBIAS24Color(), bias24String);
        }
        mtp_target_show.setText(Html.fromHtml(htmlString));
    }

    private void showKDJSum(MinusStickChart minusStickChart, ChartDataContainer dataContainer, int index) {
        if (dataContainer == null || dataContainer.mOHLCList == null || dataContainer.mOHLCList.isEmpty()) {
            mtp_target_show.setText("");
            return;
        }
        String bias = "<font color='%s'> K：%s </font><font color='%s'> D：%s </font><font color='%s'> J：%s </font>";
        String htmlString = "";
        String kString = "0.0";
        String dString = "0.0";
        String jString = "0.0";
        try {
            index = index == 0 ? minusStickChart.getNewLineData().get(0).getLineData().size() - 1 : index;
            kString = MathUtil.roundNum(minusStickChart.getNewLineData().get(0).getLineData().get(index), 2);
            dString = MathUtil.roundNum(minusStickChart.getNewLineData().get(1).getLineData().get(index), 2);
            jString = MathUtil.roundNum(minusStickChart.getNewLineData().get(2).getLineData().get(index), 2);
        } catch (Exception e) {
        }
        if(getActivity() instanceof OTCChartActivity) {
            htmlString = String.format(bias, ((OTCChartActivity) getActivity()).getMtpChartControl().getKDJKColor(), kString,
                    ((OTCChartActivity) getActivity()).getMtpChartControl().getKDJDColor(), dString, ((OTCChartActivity) getActivity())
                            .getMtpChartControl().getKDJJColor(), jString);
        }
        mtp_target_show.setText(Html.fromHtml(htmlString));
    }

    /**
     * DMA AMA
     *
     * @param minusStickChart
     * @param dataContainer
     * @param index
     */
    private void showDMASum(MinusStickChart minusStickChart, ChartDataContainer dataContainer, int index) {
        if (dataContainer == null || dataContainer.mOHLCList == null || dataContainer.mOHLCList.isEmpty()) {
            mtp_target_show.setText("");
            return;
        }
        String dma = "<font color='%s'> DMA：%s </font><font color='%s'> AMA：%s</font>";
        String htmlString = "";
        String dmaString = "0.0";
        String amaString = "0.0";
        try {
            index = index == 0 ? minusStickChart.getNewLineData().get(0).getLineData().size() - 1 : index;
            dmaString = MathUtil.roundNum(minusStickChart.getNewLineData().get(0).getLineData().get(index), 2);
            amaString = MathUtil.roundNum(minusStickChart.getNewLineData().get(1).getLineData().get(index), 2);
        } catch (Exception e) {
        }
        if(getActivity() instanceof OTCChartActivity) {
            htmlString = String.format(dma, ((OTCChartActivity) getActivity()).getMtpChartControl().getDMAColor(), dmaString,
                    ((OTCChartActivity) getActivity()).getMtpChartControl().getDMAAMAColor(), amaString);
        }
        mtp_target_show.setText(Html.fromHtml(htmlString));
    }

    private void showCCISum(MinusStickChart minusStickChart, ChartDataContainer dataContainer, int index) {
        if (dataContainer == null || dataContainer.mOHLCList == null || dataContainer.mOHLCList.isEmpty()) {
            mtp_target_show.setText("");
            return;
        }
        String cci = "<font color='%s'> CCI：%s </font>";
        String htmlString = "";
        String cciValue = "0.0";
        try {
            index = index == 0 ? minusStickChart.getNewLineData().get(0).getLineData().size() - 1 : index;
            cciValue = MathUtil.roundNum(minusStickChart.getNewLineData().get(0).getLineData().get(index), 2);
        } catch (Exception e) {
        }
        if(getActivity() instanceof OTCChartActivity) {
            htmlString = String.format(cci, ((OTCChartActivity) getActivity()).getMtpChartControl().getCCIColor(), cciValue);
        }
        mtp_target_show.setText(Html.fromHtml(htmlString));
    }

    /**
     * PSY PSYMA
     *
     * @param minusStickChart
     * @param dataContainer
     * @param index
     */
    private void showPSYSum(MinusStickChart minusStickChart, ChartDataContainer dataContainer, int index) {
        if (dataContainer == null || dataContainer.mOHLCList == null || dataContainer.mOHLCList.isEmpty()) {
            mtp_target_show.setText("");
            return;
        }
        String psy = "<font color='%s'> PSY：%s </font><font color='%s'> PSYMA：%s </font>";
        String strHtml = "";
        String psyValue = "0.0";
        String psyMAValue = "0.0";
        try {
            index = index == 0 ? minusStickChart.getNewLineData().get(0).getLineData().size() - 1 : index;
            psyValue = MathUtil.roundNum(minusStickChart.getNewLineData().get(0).getLineData().get(index), 2);
            psyMAValue = MathUtil.roundNum(minusStickChart.getNewLineData().get(1).getLineData().get(index), 2);
        } catch (Exception e) {
        }
        if(getActivity() instanceof OTCChartActivity) {
            strHtml = String.format(psy, ((OTCChartActivity) getActivity()).getMtpChartControl().getPSYColor(), psyValue,
                    ((OTCChartActivity) getActivity()).getMtpChartControl().getPSYMAColor(), psyMAValue);
        }
        mtp_target_show.setText(Html.fromHtml(strHtml));
    }

    private void setMinusStickText(TextView minusStickTV, ChartData data) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        nf.setMaximumFractionDigits(mDecimalNum);
        nf.setMinimumFractionDigits(mDecimalNum);
        String macd = nf.format(data.getMACD());
        String strMACDColor = "#FF0000";
        if (data.getMACD() < 0) {
            strMACDColor = "#009900";
        }
        String dif = nf.format(data.getDIF());
        String dea = nf.format(data.getDEA());
        String source = "<font color='%s'>MACD:%s </font>";
        if(getActivity() instanceof OTCChartActivity) {
            source += "<font color='" + ((OTCChartActivity) getActivity()).getMtpChartControl().getMACDDIFColor() + "'>DIF:%s </font>";
            source += "<font color='" + ((OTCChartActivity) getActivity()).getMtpChartControl().getMACDDEAColor() + "'>DEA:%s</font>";
        }
        String strHtml = String.format(source, strMACDColor, macd, dif, dea);
        minusStickTV.setText(Html.fromHtml(strHtml));
    }

    /**
     * 显示MACD对应的sum
     *
     * @param dataContainer
     */
    private void showMACDSum(MinusStickChart minusStickChart, ChartDataContainer dataContainer, int index) {
        if (dataContainer == null || dataContainer.mOHLCList == null || dataContainer.mOHLCList.isEmpty()) {
            mtp_target_show.setText("");
            return;
        }
        try {
            setMinusStickText(mtp_target_show, (ChartData) dataContainer.mOHLCList.get(index == 0 ? dataContainer.mOHLCList.size() - 1 : index));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示RSI对应的sum
     *
     * @param dataContainer
     */
    private void showRSISum(MinusStickChart minusStickChart, ChartDataContainer dataContainer, int index) {
        if (dataContainer == null || dataContainer.mOHLCList == null || dataContainer.mOHLCList.isEmpty()) {
            mtp_target_show.setText("");
            return;
        }
        String rsi = "<font color='%s'> RSI6：%s </font><font color='%s'> RSI12：%s </font><font color='%s'> RSI24：%s </font>";
        String strHtml = "";
        try {
            index = index == 0 ? minusStickChart.getNewLineData().get(0).getLineData().size() - 1 : index;
            String value6 = MathUtil.roundNum(minusStickChart.getNewLineData().get(0).getLineData().get(index), 2);
            String value12 = MathUtil.roundNum(minusStickChart.getNewLineData().get(1).getLineData().get(index), 2);
            String value24 = MathUtil.roundNum(minusStickChart.getNewLineData().get(2).getLineData().get(index), 2);
            if(getActivity() instanceof OTCChartActivity) {
                strHtml = String.format(rsi,
                        ((OTCChartActivity) getActivity()).getMtpChartControl().getRSI6Color(), value6,
                        ((OTCChartActivity) getActivity()).getMtpChartControl().getRSI12Color(), value12,
                        ((OTCChartActivity) getActivity()).getMtpChartControl().getRSI24Color(), value24);
            }
        } catch (Exception e) {
            strHtml = "RSI:0.0";
        }
        mtp_target_show.setText(Html.fromHtml(strHtml));
    }

    // ---------------------- end ------------------------------

    private int firstDataSize = 0;

    /**
     * 行情刷新
     * 这里做了一些刷新的限制
     * 1，之前行情1分钟或者2分钟线时，时间变化时图表不更新到最新的一条线
     * 2，但是在
     */
    public void onRefresh() {
        try {
            int startIndex =  mtp_chart_k.getStartIndex();
            if (firstDataSize == 0){
                firstDataSize = mtp_chart_k.getLineData().get(0).getLineData().size();
                mtp_chart_k.setTasLastPrice(MathUtil.roundNum(OTCChartControl.getInstance(getActivity()).getGoodsInfo().getQuoteDayData().getLast(),
                        OTCChartControl.getInstance(getActivity()).getGoodsInfo().getGoodsInfo().getDecimalplace()));
                mtp_chart_k.setNewPriceColor(getNowPriceColor());
                mtp_chart_k.setNotifyAxisMarginLeft(GridChart.DEFAULT_AXIS_MARGIN_LEFT);
                mtp_chart_k.setStartIndex(-1);
                mtp_chart_k.invalidate();
            }else {
                if (firstDataSize != mtp_chart_k.getLineData().get(0).getLineData().size()){
                    if (!((startIndex + 38) < firstDataSize)){
                        mtp_chart_k.setTasLastPrice(MathUtil.roundNum(OTCChartControl.getInstance(getActivity()).getGoodsInfo().getQuoteDayData().getLast(),
                                OTCChartControl.getInstance(getActivity()).getGoodsInfo().getGoodsInfo().getDecimalplace()));
                        mtp_chart_k.setNewPriceColor(getNowPriceColor());
                        mtp_chart_k.setNotifyAxisMarginLeft(GridChart.DEFAULT_AXIS_MARGIN_LEFT);
                        mtp_chart_k.setStartIndex(-1);
                        mtp_chart_k.invalidate();
                    }
                    firstDataSize = mtp_chart_k.getLineData().get(0).getLineData().size();
                }
            }

            if (firstDataSize <= 38){
                mtp_chart_k.setTasLastPrice(MathUtil.roundNum(OTCChartControl.getInstance(getActivity()).getGoodsInfo().getQuoteDayData().getLast(),
                        OTCChartControl.getInstance(getActivity()).getGoodsInfo().getGoodsInfo().getDecimalplace()));
                mtp_chart_k.setNewPriceColor(getNowPriceColor());
                mtp_chart_k.setNotifyAxisMarginLeft(GridChart.DEFAULT_AXIS_MARGIN_LEFT);
                mtp_chart_k.setStartIndex(-1);
                mtp_chart_k.invalidate();
            }else {
                if (!((startIndex + 38) < firstDataSize)){
                    mtp_chart_k.setTasLastPrice(MathUtil.roundNum(OTCChartControl.getInstance(getActivity()).getGoodsInfo().getQuoteDayData().getLast(),
                            OTCChartControl.getInstance(getActivity()).getGoodsInfo().getGoodsInfo().getDecimalplace()));
                    mtp_chart_k.setNewPriceColor(getNowPriceColor());
                    mtp_chart_k.setNotifyAxisMarginLeft(GridChart.DEFAULT_AXIS_MARGIN_LEFT);
                    mtp_chart_k.setStartIndex(-1);
                    mtp_chart_k.invalidate();
                }
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String getCurrentLine() {
        return currentLine;
    }

    public void setCurrentLine(String currentLine) {
        this.currentLine = currentLine;
    }

    public void dimiss() {
        loading_view.setVisibility(View.GONE);
        stopCountDownTimer();
    }

    /**
     * 是否正在加载数据
     *
     * @return
     */
    public boolean isShowing() {
        if (loading_view == null) {
            return false;
        }

        return loading_view.getVisibility() == View.VISIBLE;
    }

    /**
     * 显示加载信息
     *
     * @param message 待显示的信息
     * @param del     是否延时加载 延时加载用来解决fragment生命周期带来的控件为null的问题
     */
    public void show(String message, boolean del) {
        if (del && !initComplete) {
            HandleMessage ms = new HandleMessage();
            ms.data = message;
            ms.funcode = SHOW;
            handleList.add(ms);
        } else {
            currentMessage = message;
            loading_view_text.setText(message + " (" + delay / 1000 + "s)");
            loading_view.setVisibility(View.VISIBLE);
            stopCountDownTimer();
            startCountDownTimer();
        }
    }

    private void startCountDownTimer() {
        progressDownTimer = new CountDownTimer(delay, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                loading_view_text.setText(currentMessage + " (" + (millisUntilFinished / 1000) + "s)");
            }

            @Override
            public void onFinish() {
                dimiss();
                // MTPApplication.getInstance().showToast(R.string.mtp_timer_out);
            }
        };
        if (progressDownTimer != null) {
            progressDownTimer.start();
        }
    }

    private void stopCountDownTimer() {
        if (progressDownTimer != null) {
            progressDownTimer.cancel();
            progressDownTimer = null;
        }
    }

}
