package cgmblelib.database.entity;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;

import java.io.Serializable;
import java.util.Date;

import cgmblelib.Enum.enumUnBind;
import cgmblelib.database.ByteConverter;
import cgmblelib.database.DateConverter;
import cgmblelib.database.EnumConverter;
import ist.com.sdk.ProtocolTools;

/**
 * Created by minyuchun on 2019/1/11.
 * 发射器类
 *
 * @author minyuchun
 */
@Entity(tableName = "device")
@TypeConverters(value = {DateConverter.class, ByteConverter.class, EnumConverter.class})
public class PocDevice implements Parcelable , Serializable {
  /**
   * 连接的设备id，每次新绑定会递增赋值
   */
  @PrimaryKey(autoGenerate = true)
  private int id;
  /**
   * 设备名称 读取自数据解析
   */
  private String name;
  /**
   * mac address
   */
  private String address;
  /**
   * bind Date
   */
  private Date bindDate;
  /**
   * unBind Date
   */
  private Date unBindDate;
  //初始化结束时间
  private Date initEndDate;
  /**
   * connect Date
   */
  private Date connectDate;
  /**
   * disConnect Date
   */
  private Date disConnectDate;
  /**
   * unBind Type
   */
  private enumUnBind unBind = enumUnBind.NONE;
  /**
   * 算法版本
   */
  private int algorithmVersion;
  /**
   * 设备关闭是否用户已经确认
   */
  private int sureClose;
  /**
   * 传感器扫描信息 2020-12-18
   */
  private String qrMessage;

  private int initCount;

  private int endCount;

  public PocDevice() {
  }

  public PocDevice(ProtocolTools.Verify verify, String address) {
    this.name = verify.getName();
    this.address = address;

  }


  protected PocDevice(Parcel in) {
    id = in.readInt();
    name = in.readString();
    address = in.readString();
    long bindDateLong = in.readLong();
    long unBindDateLong = in.readLong();
    long connectDateLong = in.readLong();
    long disConnectDateLong = in.readLong();
    long initEndDateLong = in.readLong();
    bindDate = bindDateLong > 0 ? new Date(bindDateLong) : null;
    unBindDate = unBindDateLong > 0 ? new Date(unBindDateLong) : null;
    connectDate = connectDateLong > 0 ? new Date(connectDateLong) : null;
    disConnectDate = disConnectDateLong > 0 ? new Date(disConnectDateLong) : null;
    initEndDate = initEndDateLong > 0 ? new Date(initEndDateLong) : null;
    unBind = enumUnBind.getEnumUnBind(in.readInt());
    algorithmVersion = in.readInt();
    sureClose = in.readInt();
    qrMessage = in.readString();
    initCount = in.readInt();
    endCount = in.readInt();
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(name);
    dest.writeString(address);
    dest.writeLong(bindDate != null ? bindDate.getTime() : 0);
    dest.writeLong(unBindDate != null ? unBindDate.getTime() : 0);
    dest.writeLong(connectDate != null ? connectDate.getTime() : 0);
    dest.writeLong(disConnectDate != null ? disConnectDate.getTime() : 0);
    dest.writeLong(initEndDate != null ? initEndDate.getTime() : 0);
    dest.writeInt(unBind.getType());
    dest.writeInt(algorithmVersion);
    dest.writeInt(sureClose);
    dest.writeString(qrMessage);
    dest.writeInt(initCount);
    dest.writeInt(endCount);
//        dest.writeBoolean(sensorUpload);
  }

  @Override
  public int describeContents() {
    return 0;
  }

  public static final Creator<PocDevice> CREATOR = new Creator<PocDevice>() {
    @Override
    public PocDevice createFromParcel(Parcel in) {
      return new PocDevice(in);
    }

    @Override
    public PocDevice[] newArray(int size) {
      return new PocDevice[size];
    }
  };

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public Date getBindDate() {
    return bindDate;
  }

  public void setBindDate(Date bindDate) {
    this.bindDate = bindDate;
  }

  public Date getUnBindDate() {
    return unBindDate;
  }

  public void setUnBindDate(Date unBindDate) {
    this.unBindDate = unBindDate;
  }

  public Date getConnectDate() {
    return connectDate;
  }

  public void setConnectDate(Date connectDate) {
    this.connectDate = connectDate;
  }

  public Date getDisConnectDate() {
    return disConnectDate;
  }

  public void setDisConnectDate(Date disConnectDate) {
    this.disConnectDate = disConnectDate;
  }

  public Date getInitEndDate() {
    return initEndDate;
  }

  public void setInitEndDate(Date initEndDate) {
    this.initEndDate = initEndDate;
  }

  public enumUnBind getUnBind() {
    return unBind;
  }

  public void setUnBind(enumUnBind unBind) {
    this.unBind = unBind;
  }

  public int getAlgorithmVersion() {
    return algorithmVersion;
  }

  public void setAlgorithmVersion(int algorithmVersion) {
    this.algorithmVersion = algorithmVersion;
  }

  public int getSureClose() {
    return sureClose;
  }

  public void setSureClose(int sureClose) {
    this.sureClose = sureClose;
  }

  public String getQrMessage() {
    return qrMessage;
  }

  public void setQrMessage(String qrMessage) {
    this.qrMessage = qrMessage;
  }

  public int getInitCount() {
    return initCount;
  }

  public void setInitCount(int initCount) {
    this.initCount = initCount;
  }

  public int getEndCount() {
    return endCount;
  }

  public void setEndCount(int endCount) {
    this.endCount = endCount;
  }

  public boolean isConnect() {
    return connectDate != null && disConnectDate == null;
  }

  public boolean isDisConnect() {
    return connectDate != null && disConnectDate != null;
  }

  public boolean isBound() {
    return bindDate != null && unBindDate == null;
  }

  public boolean isUnBind() {
    return bindDate != null && unBindDate != null;
  }

  public boolean isInit() {
    return initEndDate == null;
  }

  public boolean isInitComplete() {
    if (initEndDate != null) {
      return getInitEndDate().getTime() < System.currentTimeMillis();
    } else {
      return false;
    }
  }

  public boolean isBoundAndConnect() {
    return isBound() && isConnect();
  }

  public boolean isBoundButDisConnect() {
    return isBound() && isDisConnect();
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof PocDevice) {
      PocDevice device = (PocDevice) obj;
      this.address = device.getAddress();
    }
    return super.equals(obj);
  }

  @NonNull
  @Override
  public String toString() {
    return "PocDevice{" +
      "id=" + id +
      ", name='" + name + '\'' +
      ", address='" + address + '\'' +
      ", bindDate=" + bindDate +
      ", unBindDate=" + unBindDate +
      ", initEndDate=" + initEndDate +
      ", connectDate=" + connectDate +
      ", disConnectDate=" + disConnectDate +
      ", unBind=" + (unBind != null ? unBind.name() : "null") +
      ", algorithmVersion=" + algorithmVersion +
      ", sureClose=" + sureClose +
      ", qrMessage='" + qrMessage + '\'' +
      ", initCount=" + initCount +
      ", endCount=" + endCount +
      ", isConnect=" + isConnect() +
      ", isDisConnect=" + isDisConnect() +
      ", isBound=" + isBound() +
      ", isUnBind=" + isUnBind() +
      ", isInit=" + isInit() +
      ", isInitComplete=" + isInitComplete() +
      ", isBoundAndConnect=" + isBoundAndConnect() +
      ", isBoundButDisConnect=" + isBoundButDisConnect() +
      '}';
  }

}
