package com.oo.eye.activity;

import android.util.DisplayMetrics;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.app.baselibrary.base.common.BaseActivity;
import com.app.baselibrary.commonUtil.CalculateUtils;
import com.app.baselibrary.commonUtil.DensityUtil;
import com.app.baselibrary.commonUtil.PreferencesUtils;
import com.app.baselibrary.commonUtil.ToastUtil;
import com.oo.eye.EyeConfig;
import com.oo.eye.R;
import com.oo.eye.R2;
import com.oo.eye.bean.Eye;
import com.oo.eye.bean.EyeChartsBean;
import com.oo.eye.bean.EyeHistroyBean;
import com.oo.eye.mvp.PresenterFactory;
import com.oo.eye.mvp.contract.EyeChartsContract;
import com.oo.eye.widget.ChartsView;
import com.oo.seex.sys_state.widget.StateView;

import java.util.List;

import butterknife.BindView;

/**
 */

public class EyeTestStatisticsActivity extends BaseActivity
		implements EyeChartsContract.View{

	private EyeChartsContract.Presenter mPresenter;

	@BindView(R2.id.state_view)
	StateView mStateView;

	@BindView(R2.id.tv_eye_statistics_init_vision)
	TextView initVisionText;

	@BindView(R2.id.tv_eye_statistics_current_vision)
	TextView currentVisionText;

	@BindView(R2.id.tv_eye_statistics_lift_vision)
	TextView liftText;

	@BindView(R2.id.line_chart_left)
	FrameLayout leftLineChart;
	@BindView(R2.id.line_chart_right)
	FrameLayout rightLineChart;

	DisplayMetrics mDisplayMetrics = new DisplayMetrics();

	private int selectId;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_eye_statistics;
	}

	protected void onResume() {
		super.onResume();
		mStateView.registStateListener();
	}


	@Override
	protected void onPause() {
		super.onPause();
		mStateView.unRegistStateListener();
	}
	@Override
	protected void init() {
		getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		selectId = PreferencesUtils.getInt( EyeConfig.EYE_SETTING_KEY,0);
		PresenterFactory.createEyeChartsPresenter(this);

		EyeHistroyBean initBean = EyeConfig.getInstance().getInitBean();
		EyeHistroyBean currentBean = EyeConfig.getInstance().getCuurentBean();
		String initText = String.valueOf(initBean.getLeft_eye()) + " | "
				+ String.valueOf(initBean.getRight_eye());
		if(selectId == 1) {
			initText = String.valueOf(Eye.transformSmall(initBean.getLeft_eye())) + " | "
					+ String.valueOf(Eye.transformSmall(initBean.getRight_eye()));
		}
		initVisionText.setText(initText);

		String currentText = String.valueOf(currentBean.getLeft_eye()) + " | "
				+ String.valueOf(currentBean.getRight_eye());
		if(selectId == 1) {
			currentText = String.valueOf(Eye.transformSmall(currentBean.getLeft_eye())) + " | "
					+ String.valueOf(Eye.transformSmall(currentBean.getRight_eye()));
		}
		currentVisionText.setText(currentText);

		double left = CalculateUtils.sub(currentBean.getLeft_eye(),initBean.getLeft_eye(),1);
		double right = CalculateUtils.sub(currentBean.getRight_eye(),initBean.getRight_eye(),1);
		if(left+right > 0) {
			double num = CalculateUtils.add(left,right,1);
			liftText.setText("" + (int) (CalculateUtils.mul(num,10,1)) + "行");
		}else{
			liftText.setText("少于1行");
		}
		mPresenter.getEyeChartsData(this,6);

//		List<EyeChartsBean> beans = new ArrayList<>();
//		EyeChartsBean bean = new EyeChartsBean();
//		bean.setLeft_eye(4.6);
//		bean.setRight_eye(4.5);
//		bean.setCreated_month("2017-07");
//		beans.add(bean);
//		EyeChartsBean bean2 = new EyeChartsBean();
//		bean2.setLeft_eye(4.4);
//		bean2.setRight_eye(4.4);
//		bean2.setCreated_month("2017-08");
//		beans.add(bean2);
//		EyeChartsBean bean3 = new EyeChartsBean();
//		bean3.setLeft_eye(4.8);
//		bean3.setRight_eye(4.8);
//		bean3.setCreated_month("2017-09");
//		beans.add(bean3);
//		bean = new EyeChartsBean();
//		bean.setLeft_eye(5.0);
//		bean.setRight_eye(5.3);
//		bean.setCreated_month("2017-10");
//		beans.add(bean);
//		bean2 = new EyeChartsBean();
//		bean2.setLeft_eye(4.4);
//		bean2.setRight_eye(5.2);
//		bean2.setCreated_month("2017-11");
//		beans.add(bean2);
//		bean3 = new EyeChartsBean();
//		bean3.setLeft_eye(4.8);
//		bean3.setRight_eye(4.8);
//		bean3.setCreated_month("2017-12");
//		beans.add(bean3);
//		getEyeChartsSuccee(beans);
	}


	public void setPresenter(EyeChartsContract.Presenter presenter){
		mPresenter = presenter;
	}

	public void getEyeChartsSuccee(final List<EyeChartsBean> beans){
		if(beans != null && beans.size() > 0) {

			int w = mDisplayMetrics.widthPixels - DensityUtil.dip2px(290);
			int h = DensityUtil.dip2px(220);

			if(selectId == 1){
				for(EyeChartsBean bean: beans){
					bean.setLeft_eye(Eye.transformSmall(bean.getLeft_eye()));
					bean.setRight_eye(Eye.transformSmall(bean.getRight_eye()));
				}
			}
			ChartsView chartsView1 = new ChartsView(this,w/2,h);
			leftLineChart.addView(chartsView1);
			chartsView1.setData(beans,true);


			ChartsView chartsView2 = new ChartsView(this,w/2,h);
			rightLineChart.addView(chartsView2);
			chartsView2.setData(beans,false);

		}
	}
	public void getEyeChartsFail(String msg){
		ToastUtil.showLongMessage(msg);
	}
}
