package com.oo.eye.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.baselibrary.commonUtil.PreferencesUtils;
import com.oo.eye.EyeConfig;
import com.oo.eye.R;
import com.oo.eye.bean.Eye;
import com.oo.eye.bean.EyeHistroyBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by gyp on 17/5/19.
 */

public class EyeHistroyListAdapter extends BaseAdapter {

    private AdapterLoadMoreListener mAdapterLoadMoreListener;
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
    private Context context;
    private List<EyeHistroyBean> list = new ArrayList<>();
    private int selectId;

    public void updateList(List<EyeHistroyBean> list) {
        this.list.clear();
        if (list != null && !list.isEmpty()) {
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void setAdapterLoadMoreListener(AdapterLoadMoreListener adapterLoadMoreListener) {
        mAdapterLoadMoreListener = adapterLoadMoreListener;
    }

    public EyeHistroyListAdapter(Context context, List<EyeHistroyBean> list) {
        this.context = context;
        selectId = PreferencesUtils.getInt(EyeConfig.EYE_SETTING_KEY,0);
        if (list != null && !list.isEmpty())
            this.list.addAll(list);
    }


    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.item_histroy, null);
            holder.timeText = (TextView) view.findViewById(R.id.tv_item_histroy_time);
            holder.leftVisionText = (TextView) view.findViewById(R.id.tv_item_histroy_left);
            holder.rightVisionText = (TextView) view.findViewById(R.id.tv_item_histroy_right);
            holder.soureText = (TextView) view.findViewById(R.id.tv_item_histroy_source);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        view.setFocusable(true);
        view.setFocusableInTouchMode(true);

        EyeHistroyBean model = list.get(i);
        holder.timeText.setText(mDateFormat.format(new Date(model.getCreated_time())));
        if(selectId == 1){
            holder.leftVisionText.setText(String.valueOf(Eye.transformSmall(model.getLeft_eye())));
            holder.rightVisionText.setText(String.valueOf(Eye.transformSmall(model.getRight_eye())));
        }else {
            holder.leftVisionText.setText(String.valueOf(model.getLeft_eye()));
            holder.rightVisionText.setText(String.valueOf(model.getRight_eye()));
        }
        holder.soureText.setText(model.getType()==1?"用户录入":"自动计算");

        if (i > 0 && i == list.size() - 1) {
            view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        if (mAdapterLoadMoreListener != null)
                            mAdapterLoadMoreListener.adapterLoadMore();

                    }
                }
            });
        }
        return view;
    }

    private class ViewHolder {
        public TextView timeText;
        public TextView leftVisionText;
        public TextView rightVisionText;
        public TextView soureText;
    }

    public interface AdapterLoadMoreListener {
        void adapterLoadMore();
    }
}
