<template>
  <div class="grey-card p-2">
    <ul :class="listClasses" v-if="displayPicked">
      <li
        v-for="p in picked"
        @click="removeEntity(p)"
        :key="p === 'me' ? 'me' : p.type + p.id"
      >
        <span
          v-if="'me' === p"
          class="chill_denomination current-user updatedBy"
          >{{ trans(USER_CURRENT_USER) }}</span
        >
        <span
          v-else-if="!isEntityHousehold(p)"
          :class="getBadgeClass(p)"
          class="chill_denomination"
          :style="getBadgeStyle(p)"
        >
          {{ p.text }}
        </span>
        <span
          v-else
          :class="getBadgeClass(p)"
          class="chill_denomination"
          :style="getBadgeStyle(p)"
        >
          Ménage n°{{ p.id }}
        </span>
      </li>
    </ul>
    <ul class="record_actions mb-0">
      <li v-if="isCurrentUserPicker" class="btn btn-sm btn-misc">
        <label class="flex items-center gap-1">
          <input
            :checked="isMePicked"
            ref="itsMeCheckbox"
            :type="multiple ? 'checkbox' : 'radio'"
            @change="selectItsMe($event as InputEvent)"
            style="margin: 0"
          />
          {{ trans(USER_CURRENT_USER) }}
        </label>
      </li>
      <li class="add-persons">
        <add-persons
          :options="addPersonsOptions"
          :key="uniqid"
          :buttonTitle="translatedListOfTypes"
          :modalTitle="translatedListOfTypes"
          :allowCreate="true"
          @addNewPersons="addNewEntity"
        >
        </add-persons>
      </li>
    </ul>

    <ul class="badge-suggest add-items inline text-end">
      <li
        v-for="s in suggested"
        :key="s.type + s.id"
        @click="addNewSuggested(s)"
        style="margin: 0"
      >
        <span
          v-if="!isEntityHousehold(s)"
          :class="getBadgeClass(s)"
          :style="getBadgeStyle(s)"
        >
          {{ s.text }}
        </span>
        <span v-else> Ménage n°{{ s.id }} </span>
      </li>
    </ul>
  </div>
</template>

<script lang="ts" setup>
import {
  ref,
  computed,
  defineProps,
  defineEmits,
  defineComponent,
  withDefaults,
} from "vue";
import AddPersons from "ChillPersonAssets/vuejs/_components/AddPersons.vue";
import {
  Entities,
  EntitiesOrMe,
  EntityType,
  isEntityHousehold,
  SearchOptions,
  Suggestion,
} from "ChillPersonAssets/types";
import {
  PICK_ENTITY_MODAL_TITLE,
  PICK_ENTITY_USER,
  PICK_ENTITY_USER_GROUP,
  PICK_ENTITY_PERSON,
  PICK_ENTITY_THIRDPARTY,
  USER_CURRENT_USER,
  trans,
} from "translator";
import { addNewEntities } from "ChillMainAssets/types";

defineComponent({
  components: {
    AddPersons,
  },
});
const props = withDefaults(
  defineProps<{
    multiple: boolean;
    types: EntityType[];
    picked: EntitiesOrMe[];
    uniqid: string;
    removableIfSet?: boolean;
    displayPicked?: boolean;
    suggested?: Entities[];
    label?: string;
    isCurrentUserPicker?: boolean;
  }>(),
  { isCurrentUserPicker: false, displayPicked: true, removableIfSet: true },
);

const emits = defineEmits<{
  (e: "addNewEntity", payload: { entity: EntitiesOrMe }): void;
  (e: "removeEntity", payload: { entity: EntitiesOrMe }): void;
  (e: "addNewEntityProcessEnded"): void;
}>();

const itsMeCheckbox = ref<null | HTMLInputElement>(null);
const addPersons = ref();

const addPersonsOptions = computed(
  () =>
    ({
      uniq: !props.multiple,
      type: props.types,
      priority: null,
      button: {
        size: "btn-sm",
        class: "btn-submit",
      },
    }) as SearchOptions,
);

const isMePicked = computed<boolean>(() => props.picked.indexOf("me") >= 0);

const translatedListOfTypes = computed(() => {
  if (props.label !== undefined && props.label !== "") {
    return props.label;
  }

  const translatedTypes = props.types.map((type: EntityType) => {
    switch (type) {
      case "user":
        return trans(PICK_ENTITY_USER, {
          count: props.multiple ? 2 : 1,
        });
      case "person":
        return trans(PICK_ENTITY_PERSON, {
          count: props.multiple ? 2 : 1,
        });
      case "thirdparty":
        return trans(PICK_ENTITY_THIRDPARTY, {
          count: props.multiple ? 2 : 1,
        });
      case "user_group":
        return trans(PICK_ENTITY_USER_GROUP, {
          count: props.multiple ? 2 : 1,
        });
    }
  });

  return `${trans(PICK_ENTITY_MODAL_TITLE, {
    count: props.multiple ? 2 : 1,
  })} ${translatedTypes.join(", ")}`;
});

const listClasses = computed(() => ({
  "badge-suggest": true,
  "remove-items": props.removableIfSet,
  inline: true,
}));

const selectItsMe = (event: InputEvent): void => {
  const target = event.target as HTMLInputElement;
  if (target.checked) {
    addNewSuggested("me");
  } else {
    removeEntity("me");
  }
};

function addNewSuggested(entity: EntitiesOrMe) {
  emits("addNewEntity", { entity });
}

function addNewEntity({ selected }: { selected: Suggestion[] }) {
  Object.values(selected).forEach((item) => {
    emits("addNewEntity", { entity: item.result });
  });
  addPersons.value?.resetSearch();

  emits("addNewEntityProcessEnded");
}

const removeEntity = (entity: EntitiesOrMe): void => {
  if (!props.removableIfSet) return;
  if (entity === "me" && itsMeCheckbox.value) {
    itsMeCheckbox.value.checked = false;
  }
  emits("removeEntity", { entity });
};

function getBadgeClass(entities: Entities) {
  if (entities.type !== "user_group") {
    return entities.type;
  }
  return "";
}

function getBadgeStyle(entities: Entities) {
  if (entities.type === "user_group") {
    return [
      `ul.badge-suggest li > span {
                color: ${entities.foregroundColor}!important;
                border-bottom-color: ${entities.backgroundColor};
        }`,
    ];
  }
  return [];
}
</script>

<style lang="scss" scoped>
.grey-card {
  background: #f8f9fa;
  border-radius: 8px;
}

.btn-check:checked + .btn,
:not(.btn-check) + .btn:active,
.btn:first-child:active,
.btn.active,
.btn.show {
  color: white;
  box-shadow: 0 0 0 0.2rem var(--bs-chill-green);
  outline: 0;
}

.as-user-group {
  display: inline-block;
}
ul.badge-suggest {
  list-style-type: none;
  padding-left: 0;
  margin-bottom: 0px;
  min-height: 30px;
}
ul.badge-suggest li > span {
  white-space: normal;
  text-align: start;
  margin-bottom: 3px;
}

ul.badge-suggest.inline li {
  display: inline-block;
  margin-right: 0.2em;
}
ul.badge-suggest.add-items li {
  position: relative;
}
ul.badge-suggest.add-items li span {
  cursor: pointer;
  padding-left: 2rem;
}
ul.badge-suggest.add-items li span:hover {
  color: #ced4da;
}
ul.badge-suggest.add-items li > span:before {
  font: normal normal normal 13px ForkAwesome;
  margin-right: 1.8em;
  content: "\f067";
  color: var(--bs-success);
  position: absolute;
  display: block;
  top: 50%;
  left: 0.75rem;
  -webkit-transform: translateY(-50%);
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}
ul.badge-suggest.remove-items li {
  position: relative;
}
ul.badge-suggest.remove-items li span {
  cursor: pointer;
  padding-left: 2rem;
}
ul.badge-suggest.remove-items li span:hover {
  color: #ced4da;
}
ul.badge-suggest.remove-items li > span:before {
  font: normal normal normal 13px ForkAwesome;
  margin-right: 1.8em;
  content: "\f1f8";
  color: var(--bs-danger);
  position: absolute;
  display: block;
  top: 50%;
  left: 0.75rem;
  -webkit-transform: translateY(-50%);
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}
ul.badge-suggest li > span {
  margin: 0.2rem 0.1rem;
  display: inline-block;
  padding: 0 1em 0 2.2em !important;
  background-color: #fff;
  border: 1px solid #dee2e6;
  border-bottom-width: 3px;
  border-bottom-style: solid;
  border-radius: 6px;
  font-size: 0.75em;
  font-weight: 700;
}

ul.badge-suggest li > span.person {
  border-bottom-color: #43b29d;
}
ul.badge-suggest li > span.thirdparty {
  border-bottom-color: rgb(198.9, 72, 98.1);
}
.current-user {
  color: var(--bs-body-color);
  background-color: var(--bs-chill-l-gray) !important;
}
</style>
