package com.oo.eye.activity;

import android.view.View;
import android.widget.TextView;

import com.app.baselibrary.base.common.BaseActivity;
import com.app.baselibrary.commonUtil.CalculateUtils;
import com.app.baselibrary.commonUtil.PreferencesUtils;
import com.app.baselibrary.commonUtil.ToastUtil;
import com.app.baselibrary.widget.LoadMoreListView;
import com.oo.eye.EyeConfig;
import com.oo.eye.R;
import com.oo.eye.R2;
import com.oo.eye.adapter.EyeHistroyListAdapter;
import com.oo.eye.bean.Eye;
import com.oo.eye.bean.EyeHistroyBean;
import com.oo.eye.mvp.PresenterFactory;
import com.oo.eye.mvp.contract.EyeHistroyContract;
import com.oo.seex.netlibrary.net.response.Page;
import com.oo.seex.sys_state.widget.StateView;

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

import butterknife.BindView;
import butterknife.OnClick;

/**
 */

public class EyeTestHistroyActivity extends BaseActivity
		implements EyeHistroyListAdapter.AdapterLoadMoreListener,EyeHistroyContract.View {

	private EyeHistroyContract.Presenter mPresenter;

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

	@BindView(R2.id.tv_histroy_current_vision_left)
	TextView currentVisionLeftText;

	@BindView(R2.id.tv_histroy_current_vision_right)
	TextView currentVisionRightText;

	@BindView(R2.id.tv_histroy_current_vision_soure)
	TextView currentVisionSoureText;

	@BindView(R2.id.tv_histroy_init_vision_left)
	TextView initVisionLeftText;

	@BindView(R2.id.tv_histroy_init_vision_right)
	TextView initVisionRightText;

	@BindView(R2.id.tv_histroy_init_vision_soure)
	TextView initVisionSoureText;

	@BindView(R2.id.tv_histroy_lift_left)
	TextView liftLeftText;

	@BindView(R2.id.tv_histroy_lift_right)
	TextView liftRightText;

	@BindView(R2.id.tv_histroy_lift_soure)
	TextView liftSoureText;

	@BindView(R2.id.lv_eye_histroy)
	LoadMoreListView mListView;

	private EyeHistroyListAdapter mListAdapter;

	private int requestPage = 0;

	private int requestCount = 10;

	private boolean hasMore = true;

	private boolean isRequest = false;

	private int selectId;

	List<EyeHistroyBean> mEyeHistroyBeen = new ArrayList<>();

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

	@Override
	protected void init() {
		selectId = PreferencesUtils.getInt(EyeConfig.EYE_SETTING_KEY,0);
		PresenterFactory.createEyeHistroyPresenter(this);
		updateTopData(EyeConfig.getInstance().getInitBean(),EyeConfig.getInstance().getCuurentBean());
		mListAdapter = new EyeHistroyListAdapter(this,mEyeHistroyBeen);
		mListView.setAdapter(mListAdapter);
		mListAdapter.setAdapterLoadMoreListener(this);
		mListView.setLoadMoreVisible(true);
		getData(1,requestCount);
	}
	protected void onResume() {
		super.onResume();
		mStateView.registStateListener();
	}


	@Override
	protected void onPause() {
		super.onPause();
		mStateView.unRegistStateListener();
	}

	private void updateTopData(EyeHistroyBean initBean,EyeHistroyBean currentBean){
		if(selectId == 1){
			initVisionLeftText.setText(String.valueOf(Eye.transformSmall(initBean.getLeft_eye())));
			initVisionRightText.setText(String.valueOf(Eye.transformSmall(initBean.getRight_eye())));
		}else {
			initVisionLeftText.setText(String.valueOf(initBean.getLeft_eye()));
			initVisionRightText.setText(String.valueOf(initBean.getRight_eye()));
		}
		initVisionSoureText.setText(initBean.getType() == 1?
				R.string.histroy_soure_input:R.string.histroy_soure_calculate);
		if(selectId == 1){
			currentVisionLeftText.setText(String.valueOf(Eye.transformSmall(currentBean.getLeft_eye())));
			currentVisionRightText.setText(String.valueOf(Eye.transformSmall(currentBean.getRight_eye())));
		}else {
			currentVisionLeftText.setText(String.valueOf(currentBean.getLeft_eye()));
			currentVisionRightText.setText(String.valueOf(currentBean.getRight_eye()));
		}
		currentVisionSoureText.setText(currentBean.getType() == 1?
				R.string.histroy_soure_input:R.string.histroy_soure_calculate);
		double left = CalculateUtils.sub(currentBean.getLeft_eye(),initBean.getLeft_eye(),1);
		double right = CalculateUtils.sub(currentBean.getRight_eye(),initBean.getRight_eye(),1);
		if(left < 0.1){
			liftLeftText.setText("少于1行");
		}else{
			liftLeftText.setText((int)(CalculateUtils.mul(left,10,0))+"行");
		}
		if(right < 0.1){
			liftRightText.setText("少于1行");
		}else{
			liftRightText.setText((int)(CalculateUtils.mul(right,10,0))+"行");
		}
		liftSoureText.setText(R.string.histroy_soure_calculate);
	}


	@OnClick(R2.id.ll_eye_histroy_refresh)
	void onClickRefresh(View view){
		getData(1,requestCount);
	}

	@Override
	public void adapterLoadMore(){
		if(hasMore) {
			mListView.setLoadMoreVisible(true);
			getData(requestPage + 1,requestCount);
		}
	}

	private void getData(int page,int limit){
		if(!isRequest) {
			isRequest = true;
			mPresenter.getEyeHistroy(this,EyeConfig.getInstance().isNetData(), page, limit);
		}
	}

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

	public void getEyeHistroyNetSuccee(ArrayList<EyeHistroyBean> beans, Page page){
		isRequest = false;
		if(beans != null && beans.size() > 0){
			if(page.getCurrentPage() == 1){
				mEyeHistroyBeen.clear();
			}
			mEyeHistroyBeen.addAll(beans);
			requestPage = page.getCurrentPage();
			hasMore = page.getNextPage()>page.getCurrentPage();
			mListAdapter.updateList(mEyeHistroyBeen);
			mListAdapter.notifyDataSetChanged();
		}else{
			hasMore = false;
		}
		if(hasMore){
			mListView.setLoadMoreVisible(false);
		}else{
			mListView.setLoadComplete();
		}
	}
	public void getEyeHistroyFail(String msg){
		isRequest = false;
		ToastUtil.showLongMessage(msg);
	}

	public void getEyeHistroyDBSuccee(List<EyeHistroyBean> beans, int page, int count){
		isRequest = false;
		if(beans != null && beans.size() > 0){
			mEyeHistroyBeen.clear();
			mEyeHistroyBeen.addAll(beans);
			requestPage = page;
			hasMore = (count > page*requestCount);
			mListAdapter.updateList(mEyeHistroyBeen);
			mListAdapter.notifyDataSetChanged();
		}else{
			hasMore = false;
		}
		if(hasMore){
			mListView.setLoadMoreVisible(false);
		}else{

			mListView.setLoadComplete();
		}
	}

}

