<template>
  <add-address
    :key="key"
    :context="context"
    :options="options"
    :address-changed-callback="submitAddress"
    ref="addAddress"
  />
</template>

<script>
import AddAddress from "./components/AddAddress.vue";
import {
  postAddressToHousehold,
  postAddressToPerson,
} from "ChillPersonAssets/vuejs/_api/AddAddress";

export default {
  name: "App",
  components: {
    AddAddress,
  },
  props: ["addAddress", "callback"],
  emits: ["addressEdited", "addressCreated"],
  computed: {
    context() {
      return this.addAddress.context;
    },
    options() {
      return this.addAddress.options;
    },
    key() {
      return this.context.edit
        ? "address_" + this.context.addressId
        : this.context.target.name + "_" + this.context.target.id;
    },
  },
  mounted() {
    //console.log('AddAddress: data context', this.context);
    //console.log('AddAddress: data options', this.options);
  },
  methods: {
    displayErrors() {
      return this.$refs.addAddress.errorMsg;
    },
    submitAddress(payload) {
      console.log("@@@ click on Submit Address Button", payload);

      // Existing address
      if (this.context.edit) {
        // address is already linked, just finish !
        this.$refs.addAddress.afterLastPaneAction({});
        this.$emit("addressEdited", payload);

        // New created address
      } else {
        this.postAddressTo(payload);
      }
    },

    /*
     *  Post new created address to targetEntity
     */
    postAddressTo(payload) {
      this.$emit("addressCreated", payload);

      console.log(
        "postAddress",
        payload.addressId,
        "To",
        payload.target,
        payload.targetId,
      );
      switch (payload.target) {
        case "household":
          postAddressToHousehold(payload.targetId, payload.addressId)
            .then(
              (address) =>
                new Promise((resolve, reject) => {
                  console.log("..household address", address);
                  this.$refs.addAddress.flag.loading = false;
                  this.$refs.addAddress.flag.success = true;

                  // finish
                  this.$refs.addAddress.afterLastPaneAction({
                    addressId: address.address_id,
                  });

                  resolve();
                }),
            )
            .catch((error) => {
              this.$refs.addAddress.errorMsg.push(error);
              this.$refs.addAddress.flag.loading = false;
            });
          break;
        case "person":
          postAddressToPerson(payload.targetId, payload.addressId)
            .then(
              (address) =>
                new Promise((resolve, reject) => {
                  console.log("..person address", address);
                  this.$refs.addAddress.flag.loading = false;
                  this.$refs.addAddress.flag.success = true;

                  // finish
                  this.$refs.addAddress.afterLastPaneAction({
                    addressId: address.address_id,
                  });

                  resolve();
                }),
            )
            .catch((error) => {
              this.$refs.addAddress.errorMsg.push(error);
              this.$refs.addAddress.flag.loading = false;
            });
          break;
        case "thirdparty":
          console.log("TODO write postAddressToThirdparty");
          break;
        default:
          this.$refs.addAddress.errorMsg.push(
            "That entity is not managed by address !",
          );
      }
    },
  },
};
</script>
