export class PictureController {

  private imagesStatic: any;

  constructor(
    private $element
  ) {
  }

  public get images(): any {
    if (this.imagesStatic) {
      return this.imagesStatic;
    } else {
      let imagesAttribute = this.$element[0].parentNode.getAttribute('images');
      if (imagesAttribute) {
        return JSON.parse(imagesAttribute);
      } else {
        return {};
      }
    }
  }

  public set images(images: any) {
    if (images) {
      if (images.constructor.name === 'String') {
        try {
          this.imagesStatic = JSON.parse(images);
        } catch (error) {
          console.error(error);
        }
      } else {
        this.imagesStatic = images;
      }
    } else {
      let parentAttribute = this.$element[0].parentNode.getAttribute('images');
      if (parentAttribute) {
        this.imagesStatic = JSON.parse(parentAttribute);
      } else {
        this.imagesStatic = {};
      }
    }
  }
}
