package com.jasamedika.medifirst2000.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.google.gson.annotations.Expose;
import com.jasamedika.medifirst2000.base.BaseMaster;
import com.jasamedika.medifirst2000.entities.constant.KelompokRuangan;
import com.jasamedika.medifirst2000.helper.Caption;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import static javax.persistence.EnumType.STRING;
import static javax.persistence.FetchType.LAZY;

@Getter
@Setter
@Entity
@Table(name = "Ruangan_M")
public class Ruangan extends BaseMaster {
	public Ruangan(String jamBuka, String jamTutup, String kdRuangan, String namaRuangan, String noRuangan,
			Boolean statusViewData, Integer id, Integer departemenId, Ruangan head) {
		super();

		this.jamBuka = jamBuka;
		this.jamTutup = jamTutup;
		this.departemenId = departemenId;
		this.kdRuangan = kdRuangan;
		this.namaRuangan = namaRuangan;
		this.noRuangan = noRuangan;
		this.statusViewData = statusViewData;
		this.headRuangan = head;
		this.id = id;
	}

	public Ruangan(String jamBuka, String jamTutup, String kdRuangan, String namaRuangan, String noRuangan,
			Boolean statusViewData, Integer id, Integer departemenId) {
		super();

		this.jamBuka = jamBuka;
		this.jamTutup = jamTutup;
		this.departemenId = departemenId;
		this.kdRuangan = kdRuangan;
		this.namaRuangan = namaRuangan;
		this.noRuangan = noRuangan;
		this.statusViewData = statusViewData;
		this.id = id;
	}

	public Ruangan(String kdRuangan, String namaRuangan, Integer id) {
		super();
		this.kdRuangan = kdRuangan;
		this.namaRuangan = namaRuangan;
		this.id = id;
	}

	public Ruangan(String namaRuangan, Integer id, String kdDepartemen, String namaDepartemen, Integer departementId) {
		super();
		this.namaRuangan = namaRuangan;
		this.id = id;
		this.departemen = new Departemen(kdDepartemen, namaDepartemen, departementId);
	}

	public Ruangan() {

	}

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "public.ruangan_m_id_seq")
	@SequenceGenerator(name = "public.ruangan_m_id_seq", sequenceName = "public.ruangan_m_id_seq", allocationSize = 1)
	@Column(name = "id")
	protected Integer id;

	@Column(name = "KdInternal", length = 5)
	@Caption(value = "KodeInternal")
	private String kdInternal;

	@Column(name = "AlamatEmail", length = 50)
	@Caption(value = "Alamat Email")
	private String alamatEmail;

	@Column(name = "Faksimile", length = 30)
	@Caption(value = "Faksimile")
	private String faksimile;

	@Column(name = "FixedPhone", length = 30)
	@Caption(value = "Fixed Phone")
	private String fixedPhone;

	@Column(name = "JamBuka", length = 5)
	@Caption(value = "Jam Buka")
	private String jamBuka;

	@Column(name = "JamTutup", length = 5)
	@Caption(value = "Jam Tutup")
	private String jamTutup;

	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "ObjectUnitKerjaFk")
	@Caption(value = "Object Unit Kerja")
	private UnitKerjaPegawai unitKerja;

	@Column(name = "ObjectUnitKerjaFk", insertable = false, updatable = false)
	private Integer unitKerjaId;

	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "ObjectDepartemenFk")
	@Expose(serialize = false)
	@Caption(value = "Object Departemen")
	private Departemen departemen;

	@Column(name = "ObjectDepartemenFk", insertable = false, updatable = false)
	private Integer departemenId;

	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "ObjectKelasHeadFk")
	@Caption(value = "Object Kelas Head")
	private Kelas kelasHead;

	@Column(name = "ObjectKelasHeadFk", insertable = false, updatable = false, nullable = false)
	private Integer kelasHeadId;

	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "ObjectModulAplikasiFk")
	@Caption(value = "Object Modul Aplikasi")
	private ModulAplikasi modulAplikasi;

	@Column(name = "ObjectModulAplikasiFk", insertable = false, updatable = false)
	private Integer modulAplikasiId;

	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "ObjectPegawaiKepalaFk")
	@Caption(value = "Object Pegawai Kepala")
	private Pegawai pegawaiKepala;

	@Column(name = "ObjectPegawaiKepalaFk", insertable = false, updatable = false)
	private Integer pegawaiKepalaId;

	@NotNull(message = "Kd Ruangan tidak boleh kosong")
	@Column(name = "KdRuangan", nullable = false, length = 3)
	@Caption(value = "Kode Ruangan")
	private String kdRuangan;

	@Column(name = "LokasiRuangan", length = 50)
	@Caption(value = "Lokasi Ruangan")
	private String lokasiRuangan;

	@Column(name = "MobilePhone", length = 30)
	@Caption(value = "Mobile Phone")
	private String mobilePhone;

	@NotNull(message = "Nama Ruangan tidak boleh kosong")
	@Column(name = "NamaRuangan", nullable = false, length = 50)
	@Caption(value = "Nama Ruangan")
	private String namaRuangan;

	@Column(name = "NoCounter")
	@Caption(value = "No Counter")
	private Integer noCounter;

	@Column(name = "NoRuangan", length = 4)
	@Caption(value = "No Ruangan")
	private String noRuangan;

	@Column(name = "PrefixNoAntrian", length = 4)
	@Caption(value = "Prefix No Antrian")
	private String prefixNoAntrian;

	@NotNull(message = "QRuangan tidak boleh kosong")
	@Column(name = "QRuangan", nullable = false)
	@Caption(value = "QRuangan")
	private short qRuangan;

	@Column(name = "StatusViewData")
	@Caption(value = "Status View Data")
	private Boolean statusViewData;

	@Column(name = "Website", length = 80)
	@Caption(value = "Website")
	private String website;

	@Column(name = "tanggal")
	@Caption(value = "tanggal")
	private Date tanggal;

	@ManyToOne(fetch = LAZY)
	@JoinColumn(name = "ObjectRuanganFk")
	@Caption(value = "Object Ruangan")
	private Ruangan headRuangan;

	@Column(name = "ObjectRuanganFk", insertable = false, updatable = false)
	private Integer headRuanganId;

	@JsonBackReference
	@OneToMany(cascade = CascadeType.ALL, fetch = LAZY, mappedBy = "ruangan")
	Set<Kamar> kamarSet = new HashSet<>();

	@Column(length = 30, columnDefinition = "varchar(30) default 'REGULER'")
	@Enumerated(STRING)
	@Size(max = 30, message = "Kelompok ruangan maksimal {max} karakter")
	@Caption(value = "Kelompok ruangan")
	private KelompokRuangan kelompokRuangan;

	@Override
	public String toString() {
		return "Ruangan [id=" + id + ", namaRuangan=" + namaRuangan + ", statusEnabled=" + statusEnabled + "]";
	}
}
