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

    <ul class="badge-suggest add-items inline" style="float: right">
      <li v-for="s in suggested" :key="s.id" @click="addNewSuggested(s)">
        <span :class="getBadgeClass(s)" :style="getBadgeStyle(s)">
          {{ s.text }}
        </span>
      </li>
    </ul>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, defineProps, defineEmits, defineComponent } from "vue";
import AddPersons from "ChillPersonAssets/vuejs/_components/AddPersons.vue";
import { Entities, EntityType, SearchOptions } 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 = defineProps<{
  multiple: boolean;
  types: EntityType[];
  picked: Entities[];
  uniqid: string;
  removableIfSet?: boolean;
  displayPicked?: boolean;
  suggested?: Entities[];
  label?: string;
  isCurrentUserPicker: boolean // must default to false
}>();

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

const itsMeCheckbox = ref(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 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) =>
    event.target.checked ? addNewSuggested("me") : removeEntity("me");

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

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

  emits("addNewEntityProcessEnded");
}


const removeEntity = (entity) => {
    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;
  padding: 1.5rem;
  min-height: 160px;
}

.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;
}
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>
