/*
 * This file is part of ORY Editor.
 *
 * ORY Editor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ORY Editor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with ORY Editor.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @license LGPL-3.0
 * @copyright 2016-2018 Aeneas Rekkas
 * @author Aeneas Rekkas <aeneas+oss@aeneas.io>
 *
 */

import * as React from 'react';
import { ImageLoaded, ImageUploaded } from 'ory-editor-ui/lib/ImageUpload';
import { ImageProps } from '../types/component';
import { ImageState } from './../types/state';

type StateType = {
  imagePreview?: ImageLoaded;
};

class Form extends React.Component<ImageProps, StateType> {
  constructor(props: ImageProps) {
    super(props);
    this.state = {};
    this.imageMap = new Map();
    this.saveServerDataToState(props.state);
    this.editImage = this.editImage.bind(this);
  }

  saveServerDataToState = (state) => {
    if(state.carouselData) {
      state.carouselData.map((item) => {
        let tempObj = {
          src : item.src,
          id : item.id,
          href : item.href
        }
        this.imageMap.set(tempObj.id,tempObj);
      })
      // this.props.onChange({'carouselData':this.getMapToArray(this.imageMap)});
    }
  } 

  handleChange = (
    e: React.ChangeEvent<HTMLInputElement>, prevState = null
  ) => {
    const target = e.target; 
    if (target instanceof HTMLInputElement) {
      let change: Partial<ImageState> = {};   
      let key = target.id;

      change = {...this.imageMap.get(key)}
      change[target.name] = target.value;
      change.id = target.id;
      this.imageMap.set(key,change);
      console.log("[Carousel :: href Component handlechange]", this.imageMap);
      this.props.onChange({'carouselData':this.getMapToArray(this.imageMap)});
      return;
    } 
  }

  deleteImage = key => {
    this.imageMap.delete(String(key));
    console.log("[Carousel :: Component image deleted]", this.imageMap);
    let arrdata = this.getMapToArray(this.imageMap)
    this.props.onChange({'carouselData':arrdata});
  }

  editImage = key => {
    console.log("[Carousel :: Edit image in component key and imagemapdata",key , this.imageMap);
    let change: Partial<ImageState> = {};   
      change = {...this.imageMap.get(String(key))} 
      change.src = '';
      change.id = key;
      this.imageMap.set(String(key),change);
      let arrdata = this.getMapToArray(this.imageMap);
      // this.props.onChange({'carouselData':arrdata.filter(item=>item.src !== '')});
      this.props.onChange({'carouselData':arrdata});
  }

  getMapToArray = mapData => {
    let data =  Array.from(mapData.values());
    return data;
  }

  handleImageLoaded = (image: ImageLoaded) =>{
    this.setState({ imagePreview: image })
  }

  handleImageUploaded = (resp: ImageUploaded,id) => {
    this.setState({ imagePreview: undefined });
    // this.props.onChange({ src: resp.url });
    let change: Partial<ImageState> = {};    
    let key = id;
    change = {...this.imageMap.get(String(key))} 
    change.src = resp.url;
    change.id = key;
    this.imageMap.set(String(key),change);
    console.log("[Carousel :: Component handlechange]", this.imageMap);
    this.props.onChange({'carouselData':this.getMapToArray(this.imageMap)});
  }

  render() {
    const { Controls } = this.props;
    return (
      <Controls
        {...this.props}
        imagePreview={this.state.imagePreview}
        handleImageLoaded={this.handleImageLoaded}
        handleImageUploaded={this.handleImageUploaded}
        handleChange={this.handleChange}
        deleteImage={this.deleteImage}
        editImage = {this.editImage}
      />
    );
  }
}

export default Form;
