package com.mytatvarnsdk.myView.widget;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import cgmblelib.Enum.enumGlucoseStatus;
import cgmblelib.Enum.enumTrend;
import cgmblelib.database.entity.PocDevice;
import cgmblelib.database.entity.PocGlucose;
import cgmblelib.utils.Tool;
import com.mytatvarnsdk.R;

/**
 * 用户显示当前的血糖值区域的内容
 * Created by minyuchun on 2019/1/31.
 *
 * @author minyuchun
 */
@SuppressLint("AppCompatCustomView")
public class SensorGlucoseShow extends RelativeLayout implements View.OnClickListener {
    /**
     * 显示趋势
     */
    private ImageView trendIcon;
    /**
     * 显示血糖值
     */
    private TextView glucoseValue;


    private PocGlucose mGlucose;

    private PocDevice mDevice;

    private TextView unitView;


    private OnClickListener mOnClickListener;


    private TextView showTime;

    public SensorGlucoseShow(Context context) {
        super(context);
        initView(context);
    }

    public SensorGlucoseShow(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public SensorGlucoseShow(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SensorGlucoseShow(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }


    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_sensor_glucose_show, this);
        trendIcon = view.findViewById(R.id.sensor_glucose_show_trend);
        glucoseValue = view.findViewById(R.id.sensor_glucose_show_value);
        showTime = view.findViewById(R.id.sensor_glucose_show_time);
        unitView = view.findViewById(R.id.sensor_glucose_show_unit);

    }

    public synchronized void refreshClear() {
        this.mDevice = null;
        this.mGlucose = null;
        refresh();
    }

    /**
     * 由于设备信息的变动设备
     *
     * @param device 发射器
     * @return 返回错误
     */
    public synchronized void refresh(PocDevice device) {
        this.mDevice = device;
        refresh();
    }

    /**
     * 血糖值变动
     *
     * @param glucose 血糖值信息
     * @return 错误
     */
    public synchronized void refresh(PocGlucose glucose) {
        this.mGlucose = glucose;
        refresh();
    }

    /**
     * 相关项整体变动
     */
    private synchronized void refresh() {

        boolean isBind = false;
        boolean isConnect = false;
        boolean isInit = true;
        boolean isNewBG = false;
        int hour = 0;
        int minute = 0;
        float glu = 0;
        enumGlucoseStatus status = enumGlucoseStatus.NONE;
        enumTrend trend = enumTrend.NONE;
        if (mDevice != null) {
            isBind = mDevice.isBound();
            isConnect = mDevice.isConnect();
            isInit = mDevice.isInit();
            if (isBind) {
                if (mGlucose != null) {
                    trend = mGlucose.getTrend();
                    hour = mGlucose.getHour();
                    minute = mGlucose.getMinute();
                    if (!isNewBG) {
                        glu = mGlucose.getShowGlucose();
                        status = mGlucose.getGlucoseStatus();
                    }
                }
            }
        }
        unitView.setText("mmol/L");
        showCountDownTime(isInit, hour, minute);
        showTrend(isNewBG, isBind, isConnect, trend);
        showGlucoseValue(isNewBG, isConnect, glu, status);
    }



    /**
     * 显示血糖值
     *
     * @param isNewBg 是否校准
     * @param glu     血糖值
     * @param status  血糖状态
     */
    private void showGlucoseValue(boolean isNewBg, boolean isConnect, float glu, enumGlucoseStatus status) {
        if (!isConnect) {
            glucoseValue.setText("-");
            glucoseValue.setTextColor(Color.BLACK);
        } else {
            if (isNewBg || status != enumGlucoseStatus.NONE) {
                glucoseValue.setText(Tool.getStringGluValue(glu));
            } else {
                glucoseValue.setText("-");
            }
            glucoseValue.setTextColor(getResources().getColor(status.getTextColorId(), null));
        }
    }

    /**
     * 显示倒计时
     *
     * @param isInit 是否初始化
     * @param hour   倒计时的小时
     * @param minute 倒计时的分钟
     */
    private void showCountDownTime(boolean isInit, int hour, int minute) {
        if (showTime == null) {
            return;
        }
//        if (!isInit) {
        String hourString = hour >= 10 ? String.valueOf(hour) : "0" + hour;
        String minuteString = minute >= 10 ? String.valueOf(minute) : "0" + minute;
        String message = String.format(
                getResources().getString(R.string.sensor_time_default), hourString, minuteString);
        showTime.setText(message);
    }

    /**
     * 显示趋势
     *
     * @param isBind 是否绑定
     * @param trend  趋势
     */
    private void showTrend(boolean isBewBg, boolean isBind, boolean isConnect, @NonNull enumTrend trend) {
        if (trendIcon == null) {
            return;
        }
        if (!isBewBg && isBind && isConnect) {
            trendIcon.setImageResource(trend.getDrawableId());
        } else {
            trendIcon.setImageResource(enumTrend.NONE.getDrawableId());
        }
    }

    @Override
    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    @Override
    public void onClick(View v) {
        mOnClickListener.onClick(v);
    }
}
