import { Component, ElementRef, EventEmitter, HostListener, Input, OnChanges, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { EditorItem, HotZoneImageItem, Label, MySprite } from './Unit';
import TWEEN from '@tweenjs/tween.js';


@Component({
  selector: 'app-custom-hot-zone',
  templateUrl: './custom-hot-zone.component.html',
  styleUrls: ['./custom-hot-zone.component.scss']
})
export class CustomHotZoneComponent implements OnInit, OnDestroy, OnChanges {




  _bgItem = null;

  @Input()
  set bgItem(v) {
    this._bgItem = v;
    this.init();
  }
  get bgItem() {
    return this._bgItem;
  }

  @Input() imgItemArr = null;
  @Input() hotZoneItemArr = null;
  @Input() hotZoneArr = null;
  @Output() save = new EventEmitter();

  @ViewChild('canvas') canvas: ElementRef;
  @ViewChild('wrap') wrap: ElementRef;
  @HostListener('window:resize', ['$event'])
  onResize(event) {
    this.g_winResizeEventStream.next();
  }

  g_winResizeEventStream = new Subject();
  firstTouch = true;
   canvasWidth = 960;
  canvasHeight = 155;

  canvasBaseW = 960;
  canvasBaseH = 155;
  mapScale = 1;

  ctx;
  mx;
  my; // 点击坐标


  // 资源
  // rawImages = new Map(res);

  // 声音
  bgAudio = new Audio();
  images = new Map();
  animationId: any;
  // winResizeEventStream = new Subject();
  canvasLeft;
  canvasTop;
  renderArr;
  imgArr = [];
  oldPos;
  curItem;
  bg: MySprite;
  changeSizeFlag = false;
  changeTopSizeFlag = false;
  changeRightSizeFlag = false;
  hotZoneChanged = false;
  scale = 1;

  constructor(private el: ElementRef) {
  }


  ngOnInit() {
    if(!this.bgItem.url){
      this.bgItem.url = ""
    }
    this.initListener();
    this.update();
  }

  ngOnDestroy() {
    window.cancelAnimationFrame(this.animationId);
  }

  ngOnChanges() {

  }

  checkDisabel(items, val, index) {
    let valIndex_1 = items.indexOf(-1)
    let valIndex_2 = items.indexOf(-2)
    if(valIndex_1 == index || valIndex_2 == index) { 
      return false
    } else {
      if(valIndex_1 == -1 && valIndex_2 == -1 ) {
        return false
      } else {
        return true
      }
    }
  }

  onBackgroundUploadSuccess(e) {
    this.bgItem.url = e.url;
    this.refreshBackground(() => {
      this.autoSave()
    });
  }

  refreshBackground(callBack = null) {
    if (!this.bg) {
      this.bg = new MySprite(this.ctx);
      this.renderArr.push(this.bg);
    }
    const bg = this.bg;
    if (this.bgItem.url) {
      bg.load(this.bgItem.url).then(() => {
        const rate1 = this.canvasWidth / bg.width;
        const rate2 = this.canvasHeight / bg.height;
        const rate = Math.min(rate1, rate2);
        bg.setScaleXY(rate);
        bg.x = this.canvasWidth / 2;
        bg.y = this.canvasHeight / 2;
        if (callBack) {
          callBack();
        }
      }).catch((error) => {
        console.log(error)
      })
    }
  }

  addBtnClick() {
    const item = this.getHotZoneItem();
    this.hotZoneArr.push(item);
    this.refreshHotZoneId();
    this.refreshIndexNumber()
    this.autoSave()
  }

  handleMoveItemUp(event, index) {
    if (index != 0) {
      this.bgItem.questions[index] = this.bgItem.questions.splice(index - 1, 1, this.bgItem.questions[index])[0];
    } else {
      this.bgItem.questions.push(this.bgItem.questions.shift());
    }
    this.autoSave()
  }

  handleMoveItemDown(event, index) {
    if (index != this.bgItem.questions.length - 1) {
      this.bgItem.questions[index] = this.bgItem.questions.splice(index + 1, 1, this.bgItem.questions[index])[0];
    } else {
      this.bgItem.questions.unshift(this.bgItem.questions.splice(index, 1)[0]);
    }
    this.autoSave()
  }

  onImgUploadSuccessByImg(e, item) {
    item.pic_url = e.url;
    item.image_url = e.url;
    item.initNew((w, h) => {
      item.setScaleXY(Math.min(100 / w, 150 / h))
    })
    this.refreshImage(item);
    this.autoSave()
  }

  onAudioUploadSuccess(e, item, key) {
    if(key) {
      item[key] = e.url;
    } else {
      item.audio_url = e.url;
    }
    
    this.autoSave()
  }

  refreshImage(img) {
    this.hideAllLineDash();
    img.picItem = this.getPicItem(img);
    this.refreshImageId();
  }

  refreshHotZoneId() {
    for (let i = 0; i < this.hotZoneArr.length; i++) {
      this.hotZoneArr[i].index = i;
    }
  }

  refreshHotZoneText(item) {
    item.initNew()
  }

  refreshImageId() {
    for (let i = 0; i < this.imgArr.length; i++) {
      this.imgArr[i].id = i;
      if (this.imgArr[i].picItem) {
        this.imgArr[i].picItem.text = 'Image-' + (i + 1);
      }
    }
  }

  getHotZoneItem(saveData = null, newRect?) {
    const itemW = 200;
    const itemH = 200;
    const item = new HotZoneImageItem(this.ctx);
    if (saveData) {
      item.init(saveData.media.image_url, saveData.media.text, saveData.media.type, (w, h) => {
        const saveRect = saveData.rect;
        item.scaleX = (saveData.rect.width / saveData.scale) / w;
        item.scaleY = (saveData.rect.height / saveData.scale) / h;
        item.x = saveRect.x / saveData.scale + newRect.x
        item.y = saveRect.y / saveData.scale + newRect.y
      });
      item.image_url = saveData.media.image_url
      // item.text = saveData.media.text
      // item.type = saveData.media.type
      item.audio_url = saveData.media.audio_url?saveData.media.audio_url:""
      // item.image_url = "assets/play/bg_70_40.png"
      // item.text = ""
      item.type = "Image"
    } else {
      item.init(`assets/play/hotZooCard/bg_70_40_${this.hotZoneItemArr.length}.png`, null, "Image", () => {
        // item.image_url = "assets/play/bg_70_40.png"
        item.x = this.canvasWidth / 2 - 35;
        item.y = this.canvasHeight / 2 - 20;
      }, () => this.autoSave());
    }
    item.text = item.text?item.text:""
    item.anchorX = 0.5;
    item.anchorY = 0.5;

    return item;
  }

  refreshIndexNumber() {
    this.hotZoneArr.forEach((element: HotZoneImageItem, index) => {
      element.init(`assets/play/hotZooCard/bg_70_40_${index+1}.png`, element.text, "Image", ()=>{
      
      })
    }, () => this.autoSave());
    // this.autoSave()
  }

  getPicItem(img, saveData = null) {
    const item = new EditorItem(this.ctx);
    item.load(img.pic_url).then(img => {
      let maxW, maxH;
      if (this.bg) {
        maxW = this.bg.width * this.bg.scaleX;
        maxH = this.bg.height * this.bg.scaleY;
      } else {
        maxW = this.canvasWidth;
        maxH = this.canvasHeight;
      }
      let scaleX = maxW / 3 / item.width;
      let scaleY = maxH / 3 / item.height;
      if (item.height * scaleX < this.canvasHeight) {
        item.setScaleXY(scaleX);
      } else {
        item.setScaleXY(scaleY);
      }
      item.x = this.canvasWidth / 2;
      item.y = this.canvasHeight / 2;
      if (saveData) {
        const saveRect = saveData.rect;
        item.setScaleXY(saveRect.width / item.width);
        item.x = saveRect.x + saveRect.width / 2;
        item.y = saveRect.y + saveRect.height / 2;
      } else {
        item.showLineDash();
      }
      this.autoSave()
    })
    return item;
  }


  onAudioUploadSuccessByImg(e, item) {
    item.audio_url = e.url;
    this.autoSave()
  }

  deleteItem(e, i) {
    this.hotZoneArr.splice(i, 1);
    this.refreshHotZoneId();
    this.refreshIndexNumber()
    this.autoSave()
  }
  
  deleteQuestionItem(e, i) {
    this.bgItem.questions.splice(i, 1);
    // this.refreshHotZoneId();
    this.autoSave()
  }

  init() {
    this.initData();
    this.initCtx();
    this.initItem();
  }

  initItem() {
    if (!this.bgItem) {
      this.bgItem = { };
    } else {
      if(!this.bgItem.questions) {
        this.bgItem.questions = []
      }
      this.refreshBackground(() => {
        if (!this.hotZoneItemArr) {
          this.hotZoneItemArr = [];
        } else {
          this.initHotZoneArr();
        }
      });
    }
  }

  initHotZoneArr() {
    let curBgRect;
    if (this.bg) {
      curBgRect = this.bg.getBoundingBox();
    } else {
      curBgRect = { x: 0, y: 0, width: this.canvasWidth, height: this.canvasHeight };
    }
    let oldBgRect = this.bgItem.rect;
    if (!oldBgRect) {
      oldBgRect = curBgRect;
    }
    const rate = curBgRect.width / oldBgRect.width;
    this.hotZoneArr = [];
    const arr = this.hotZoneItemArr.concat();
    for (let i = 0; i < arr.length; i++) {
      const data = JSON.parse(JSON.stringify(arr[i]));
      data.rect.x *= rate;
      data.rect.y *= rate;
      data.rect.width *= rate;
      data.rect.height *= rate;
      // data.rect.x += curBgRect.x;
      // data.rect.y += curBgRect.y;
      const item = this.getHotZoneItem(data, { x: curBgRect.x, y: curBgRect.y });
      // c//onsole.log("初始化存储数据", data)
      this.hotZoneArr.push(item);
    }
    this.refreshHotZoneId();
  }

  addQustion() {
    this.bgItem.questions.push({
      number: 2,
      combin: [0, 0],
      _combin: [0, 0]
    })
    this.autoSave()
  }

  handleCombinLength(item) {
    if(Number(item.number)>5){
      return
    }

    if( Number(item.number) > item.combin.length ) {
      let num = Number(item.number) - item.combin.length
      for(var index=0; index<num; index++) {
        item.combin.push(0)
      }
    } else {
      item.combin = item.combin.splice(0, Number(item.number))
    }
    this.autoSave()
    console.log(item.combin)
  }

  initImgArr() {
    let curBgRect;
    if (this.bg) {
      curBgRect = this.bg.getBoundingBox();
    } else {
      curBgRect = { x: 0, y: 0, width: this.canvasWidth, height: this.canvasHeight };
    }

    let oldBgRect = this.bgItem.rect;
    if (!oldBgRect) {
      oldBgRect = curBgRect;
    }

    const rate = curBgRect.width / oldBgRect.width;
    this.imgArr = [];
    const arr = this.imgItemArr.concat();
    for (let i = 0; i < arr.length; i++) {

      const data = JSON.parse(JSON.stringify(arr[i]));
      const img = { pic_url: data.pic_url };

      data.rect.x *= rate;
      data.rect.y *= rate;
      data.rect.width *= rate;
      data.rect.height *= rate;

      data.rect.x += curBgRect.x;
      data.rect.y += curBgRect.y;

      img['picItem'] = this.getPicItem(img, data);
      img['audio_url'] = arr[i].audio_url;
      this.imgArr.push(img);
    }
    this.refreshImageId();
  }

  getQuestionWord(combin) {
    let str = ""
    combin.forEach(element => {
      if(element == -1) {
        str += this.bgItem.wordLeft_text?this.bgItem.wordLeft_text:"";
      } else if(element == -2) {
        str += this.bgItem.wordRight_text?this.bgItem.wordRight_text:"";
      } else {
        str += this.hotZoneArr[element] && this.hotZoneArr[element].text?this.hotZoneArr[element].text:"";
      }
    });
    return str
  }

  initData() {
    this.canvasWidth = this.wrap.nativeElement.clientWidth;
    this.canvasHeight = this.wrap.nativeElement.clientWidth * (155 / 960);
    this.scale = 960 * 0.95 / this.wrap.nativeElement.clientWidth
    this.mapScale = this.canvasWidth / this.canvasBaseW;
    this.renderArr = [];
    this.bg = null;
    this.imgArr = [];
    this.hotZoneArr = [];
  }

  initCtx() {
    this.ctx = this.canvas.nativeElement.getContext('2d');
    this.canvas.nativeElement.width = this.canvasWidth;
    this.canvas.nativeElement.height = this.canvasHeight;
  }
  
  mapDown(event) {
    this.oldPos = { x: this.mx, y: this.my };
    const arr = this.hotZoneArr;
    for (let i = arr.length - 1; i >= 0; i--) {
      const item = arr[i];
      if (item) {
        if (this.checkClickTarget(item)) {
          if (item.lineDashFlag && this.checkClickTarget(item.arrow)) {
            if(item.type == "Text"){
              return
            }
            this.changeItemSize(item);
          } else if (item.lineDashFlag && this.checkClickTarget(item.arrowTop)) {
            if(item.type == "Text"){
              return
            }
            this.changeItemTopSize(item);
          } else if (item.lineDashFlag && this.checkClickTarget(item.arrowRight)) {
            if(item.type == "Text"){
              return
            }
            this.changeItemRightSize(item);
          } else {
            this.changeCurItem(item);
          }
          this.hotZoneChanged = true;
          return;
        }

      }
    }
  }

  mapMove(event) {
    if (!this.curItem) { return; }

    if (this.changeSizeFlag) {
      this.changeSize();

    } else if (this.changeTopSizeFlag) {
      this.changeTopSize();

    } else if (this.changeRightSizeFlag) {
      this.changeRightSize();
    } else {
      let addX = this.mx - this.oldPos.x;
      let addY = this.my - this.oldPos.y;
      this.curItem.x += addX;
      this.curItem.y += addY;
    }

    this.oldPos = { x: this.mx, y: this.my };
  }

  mapUp(event) {
    this.curItem = null;
    this.changeSizeFlag = false;
    this.changeTopSizeFlag = false;
    this.changeRightSizeFlag = false;
  }

  changeSize() {
    const rect = this.curItem.getBoundingBox();
    let distance = this.my - rect.y;
    let lenW = this.mx - rect.x;
    let lenH = rect.height - distance
    let minLen = 20;
    let sx, sy;
    if (lenW < minLen) {
      lenW = minLen;
    }
    sx = lenW / this.curItem.width;
    if (lenH < minLen) {
      lenH = minLen;
    }
    sy = lenH / this.curItem.height;
    this.curItem.y = this.curItem.y + distance
    this.curItem.scaleX = sx;
    this.curItem.scaleY = sy;
  }


  changeTopSize() {
    const rect = this.curItem.getBoundingBox();
    let lenH = rect.y - this.my + rect.height;
    this.curItem.y = this.my
    let minLen = 20;
    let s;
    if (lenH < minLen) {
      lenH = minLen;
    }
    s = lenH / this.curItem.height;
    this.curItem.setScaleXY(s);
  }

  changeRightSize() {
    const rect = this.curItem.getBoundingBox();
    let lenW = this.mx - rect.x;
    let minLen = 20;
    let s;
    if (lenW < minLen) {
      lenW = minLen;
    }
    s = lenW / this.curItem.width;
    this.curItem.setScaleXY(s);
  }

  changeItemSize(item) {
    this.curItem = item;
    this.changeSizeFlag = true;
  }

  changeItemTopSize(item) {
    this.curItem = item;
    this.changeTopSizeFlag = true;
  }

  changeItemRightSize(item) {
    this.curItem = item;
    this.changeRightSizeFlag = true;
  }

  changeCurItem(item) {
    this.hideAllLineDash();
    this.curItem = item;
    this.curItem.showLineDash();
  }

  handlePartChange(e, QI, index) {
    let a = JSON.parse(JSON.stringify(this.bgItem.questions[QI].combin))
    a[index] = Number(e)
    this.bgItem.questions[QI].combin = a
    this.autoSave()
  }

  hideAllLineDash() {
    for (let i = 0; i < this.imgArr.length; i++) {
      if (this.imgArr[i].picItem) {
        this.imgArr[i].picItem.hideLineDash();
      }
    }
  }


  update() {
    this.animationId = window.requestAnimationFrame(this.update.bind(this));
    // 清除画布内容
    this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
    for (let i = 0; i < this.renderArr.length; i++) {
      this.renderArr[i].update(this);
    }
    this.updateArr(this.hotZoneArr);
    TWEEN.update();
  }

  updateArr(arr) {
    if (arr) {
      for (let i = 0; i < arr.length; i++) {
        arr[i].update();
      }
    }
  }

  renderAfterResize() {
    this.canvasWidth = this.wrap.nativeElement.clientWidth;
    this.canvasHeight = this.wrap.nativeElement.clientHeight;
    this.init();
  }

  initListener() {
    const element = this.canvas.nativeElement;
    this.g_winResizeEventStream.pipe(debounceTime(500)).subscribe(data => {
      this.renderAfterResize();
    });
    
    const addTouchListener = () => {
      element.addEventListener('touchstart', touchDownFunc);
      element.addEventListener('touchmove', touchMoveFunc);
      element.addEventListener('touchend', touchUpFunc);
      element.addEventListener('touchcancel', touchUpFunc);
    };
    const removeTouchListener = () => {
      element.removeEventListener('touchstart', touchDownFunc);
      element.removeEventListener('touchmove', touchMoveFunc);
      element.removeEventListener('touchend', touchUpFunc);
      element.removeEventListener('touchcancel', touchUpFunc);
    };

    const addMouseListener = () => {
      element.addEventListener('mousedown', mouseDownFunc);
      element.addEventListener('mousemove', mouseMoveFunc);
      element.addEventListener('mouseup', mouseUpFunc);
    };
    const removeMouseListener = () => {
      element.removeEventListener('mousedown', mouseDownFunc);
      element.removeEventListener('mousemove', mouseMoveFunc);
      element.removeEventListener('mouseup', mouseUpFunc);
    };

    const touchDownFunc = (e) => {
      if (this.firstTouch) {
        this.firstTouch = false;
        removeMouseListener();
      }
      setMxMyByTouch(e);
      this.mapDown(e);
    };
    const touchMoveFunc = (e) => {
      setMxMyByTouch(e);
      this.mapMove(e);
    };
    const touchUpFunc = (e) => {
      setMxMyByTouch(e);
      this.mapUp(e);
    };

    const mouseDownFunc = (e) => {
      if (this.firstTouch) {
        this.firstTouch = false;
        removeTouchListener();
      }
      setMxMyByMouse(e);
      this.mapDown(e);
    };
    const mouseMoveFunc = (e) => {
      setMxMyByMouse(e);
      this.mapMove(e);
    };
    const mouseUpFunc = (e) => {
      setMxMyByMouse(e);
      this.mapUp(e);
    };

    const setMxMyByTouch = event => {
      if (event.touches.length <= 0) {
        return;
      }
      if (this.canvasLeft == null) {
        setParentOffset();
      }
      this.mx = event.touches[0].pageX - this.canvasLeft;
      this.my = event.touches[0].pageY - this.canvasTop;
    };

    const setParentOffset = () => {
      const rect = this.canvas.nativeElement.getBoundingClientRect();
      this.canvasLeft = rect.left;
      this.canvasTop = rect.top;
    };

    const setMxMyByMouse = (event) => {
      this.mx = event.offsetX;
      this.my = event.offsetY;
    };

    addMouseListener();
    addTouchListener();

  }

  IsPC() {
    if (window['ELECTRON']) {
      return false; // 封装客户端标记
    }
    if (document.body.ontouchstart !== undefined) {
      return false;
    } else {
      return true;
    }
  }


  checkClickTarget(target) {
    const rect = target.getBoundingBox();
    if (this.checkPointInRect(this.mx, this.my, rect)) {
      return true;
    }
    return false;
  }

  checkPointInRect(x, y, rect) {
    if (x >= rect.x && x <= rect.x + rect.width) {
      if (y >= rect.y && y <= rect.y + rect.height) {
        return true;
      }
    }
    return false;
  }

  autoSave() {
    // console.log("Auto save")
    const bgItem = this.bgItem;
    if (this.bg) {
      bgItem['rect'] = this.bg.getBoundingBox();
    } else {
      bgItem['rect'] = { x: 0, y: 0, width: Math.round(this.canvasWidth * 100) / 100, height: Math.round(this.canvasHeight * 100) / 100 };
    }

    const hotZoneItemArr = [];
    const hotZoneArr = this.hotZoneArr;
    for (let i = 0; i < hotZoneArr.length; i++) {
      const hotZoneItem = {
        index: hotZoneArr[i].index,
      };
      hotZoneItem['rect'] = hotZoneArr[i].getBoundingBox();
      const currentX = hotZoneItem['rect'].x
      const currentY = hotZoneItem['rect'].y
      hotZoneItem['media'] = {}
      hotZoneItem['scale'] = this.scale
      hotZoneItem['rect'].x = (Math.round((currentX - bgItem['rect'].x) * 1000) / 1000) * this.scale;
      hotZoneItem['rect'].y = (Math.round((currentY - bgItem['rect'].y) * 1000) / 1000) * this.scale;
      hotZoneItem['rect'].width = (Math.round((hotZoneItem['rect'].width) * 1000) / 1000) * this.scale;
      hotZoneItem['rect'].height = (Math.round((hotZoneItem['rect'].height) * 1000) / 1000) * this.scale;
      hotZoneItem['media'].image_url = hotZoneArr[i].image_url
      hotZoneItem['media'].text = hotZoneArr[i].text
      hotZoneItem['media'].type = hotZoneArr[i].type
      hotZoneItem['media'].audio_url = hotZoneArr[i].audio_url ? hotZoneArr[i].audio_url : ""
      // hotZoneItem['media'].card_audio_url = hotZoneArr[i].card_audio_url ? hotZoneArr[i].card_audio_url : ""
      hotZoneItemArr.push(hotZoneItem);
    }
    this.save.emit({ bgItem, hotZoneItemArr });
    this.hotZoneChanged = false;
  }

  saveClick() {
    // console.log("Saved")
    this.autoSave()
  }
}
