import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { JsonPipe } from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd/message';


@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit, OnChanges, OnDestroy {


  
  baseData = {"templateType":"select","quesArr":[{"itemType":"jump","bgItem":{"url":"http://staging-teach.cdn.ireadabc.com/9dbccb9eb987794dae2f431e8fa9d69e.jpg","rect":{"x":0,"y":164.59,"width":1218,"height":562.82}},"hotZoneItemArr":[{"id":"1709803952827","index":0,"pic_url":"http://staging-teach.cdn.ireadabc.com/cf983e7bf75a1f42a3d0c7ddb05f60a5.png","itemType":"pic","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":0.7215639810426541,"imgSizeW":844,"imgSizeH":112,"mapScale":0.9515625,"dragDot":{"x":609,"y":326.5},"gIdx":"3","posX":609,"posY":100.5,"rect":{"x":304.5,"y":15.01,"width":609,"height":80.82}},{"id":"1710318952570","index":1,"pic_url":"http://staging-teach.cdn.ireadabc.com/36f7e814ac582315b534bbea668da7f5.png","itemType":"pic","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":0.7215639810426541,"imgSizeW":96,"imgSizeH":96,"mapScale":0.9515625,"dragDot":{"x":609,"y":446},"gIdx":"3","posX":347,"posY":220,"rect":{"x":312.36,"y":20.77,"width":69.27,"height":69.27}},{"id":"1709804038970","index":2,"pic_url":"http://staging-teach.cdn.ireadabc.com/cf5aa9cdeae3aa02a0fa3571200af395.png","itemType":"rect","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":0.7215639810426541,"imgSizeW":96,"imgSizeH":96,"mapScale":0.9515625,"skeJsonData":{"url":"http://staging-teach.cdn.ireadabc.com/95cd9e52dea28af68e217272b437068c.json","name":"laba_ske.json"},"texJsonData":{"url":"http://staging-teach.cdn.ireadabc.com/843bd6e81c0e3469a1d80d0d9f0e0e9e.json","name":"laba_tex.json"},"texPngData":{"url":"http://staging-teach.cdn.ireadabc.com/aef922429e79d9a3ecf34fe62e9f9bac.png","name":"laba_tex.png"},"dragDot":{"x":609,"y":326.5},"gIdx":"5","posX":345.995,"posY":219.9950473933649,"rect":{"x":311.36,"y":20.77,"width":69.27,"height":69.27}},{"id":"1709804126892","index":3,"itemType":"rect","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":1,"imgSizeW":0,"imgSizeH":0,"mapScale":0.9515625,"dragDot":{"x":609,"y":326.5},"gIdx":"0","posX":646,"posY":100.5000473933649,"labelArr":[{"type":"inputText","key":"title","title":"标题","value":"标题"},{"key":"key","title":"key","value":"title_text"},{"key":"fontName","title":"字体"},{"key":"fontSize","title":"大小","value":"50"},{"key":"fontColor","title":"颜色","value":"#ffffff"}],"rect":{"x":391,"y":21.91,"width":510,"height":67}},{"id":"1709876563767","index":4,"itemType":"rect","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":1,"imgSizeW":0,"imgSizeH":0,"mapScale":0.9515625,"dragDot":{"x":609,"y":326.5},"gIdx":"6","posX":345,"posY":221.00004739336487,"labelArr":[{"type":"inputAudio","key":"title","title":"标题","value":"标题音频"},{"key":"key","title":"key","value":"title_audio_url"}],"rect":{"x":297,"y":8.41,"width":96,"height":96}},{"id":"1709804436675","index":5,"pic_url":"http://staging-teach.cdn.ireadabc.com/8297964c4aa69bf37a3c54ddcc409e40.png","itemType":"pic","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":0.7215639810426541,"imgSizeW":512,"imgSizeH":512,"mapScale":0.9515625,"dragDot":{"x":609,"y":326.5},"gIdx":"3","posX":609,"posY":372.5,"rect":{"x":424.28,"y":142.69,"width":369.44,"height":369.44}},{"id":"1709804508935","index":6,"itemType":"rect","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":1,"imgSizeW":0,"imgSizeH":0,"mapScale":0.9515625,"dragDot":{"x":609,"y":326.5},"gIdx":"2","posX":608.0052896725442,"posY":493.9988635142717,"labelArr":[{"type":"inputPic","key":"title","title":"标题","value":"描述图片"},{"key":"key","title":"key","value":"pic_url"}],"rect":{"x":464,"y":185.41,"width":288,"height":288}},{"id":"1709804804013","index":7,"itemType":"rect","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":1,"imgSizeW":0,"imgSizeH":0,"mapScale":0.9515625,"dragDot":{"x":609,"y":326.5},"gIdx":"11","posX":989,"posY":619.0000473933649,"rect":{"x":889,"y":392.91,"width":200,"height":123}},{"id":"1709867627088","index":8,"pic_url":"http://staging-teach.cdn.ireadabc.com/a5287e233dd1c796ff01e51e095bfeea.png","itemType":"pic","fontScaleX":0.9515625,"fontScaleY":0.9515625,"imgScale":0.7215639810426541,"imgSizeW":392,"imgSizeH":140,"mapScale":0.9515625,"dragDot":{"x":609,"y":326.5},"gIdx":"3","posX":138,"posY":560.5,"anchorGroup":6,"rect":{"x":-3.43,"y":464.91,"width":282.85,"height":101.02}}]}],"readingItemArr":[],"isTurnPage":true,"customTypeGroupArr":[{"name":"输入型文本","rect":true,"isShowPos":true,"labelArr":[{"type":"inputText","key":"title","title":"标题"},{"key":"key","title":"key"},{"key":"fontName","title":"字体"},{"key":"fontSize","title":"大小"},{"key":"fontColor","title":"颜色"}]},{"name":"固定型文本","text":true,"audio":true,"isShowPos":true,"labelArr":[{"key":"fontName","title":"字体"},{"key":"fontSize","title":"大小"},{"key":"fontColor","title":"颜色"}]},{"name":"输入型图片","rect":true,"isShowPos":true,"labelArr":[{"type":"inputPic","key":"title","title":"标题"},{"key":"key","title":"key","value":""}]},{"name":"固定型图片","pic":true,"audio":true,"anchorGroup":true,"isShowPos":true},{"name":"输入型动画","rect":true,"isShowPos":true,"labelArr":[{"key":"title","title":"标题"}]},{"name":"固定型动画","rect":true,"anima":true,"audio":true,"isShowPos":true},{"name":"输入型音频","rect":true,"isShowPos":true,"labelArr":[{"type":"inputAudio","key":"title","title":"标题"},{"key":"key","title":"key","value":""}]},{"name":"固定型音频","rect":true,"audio":true,"pic":true,"anim":true,"isShowPos":true},{"name":"数据型文本","data":true,"labelArr":[{"type":"inputText","key":"title","title":"标题"},{"key":"key","title":"key"}]},{"name":"数据型图片","labelArr":[{"type":"inputPic","key":"title","title":"标题","value":""},{"key":"key","title":"key","value":""}]},{"name":"翻页按钮组","rect":true,"audio":true,"isShowPos":true},{"name":"录音按钮组","rect":true,"audio":true,"isShowPos":true}]}
  
  // 储存数据用
  saveKey = "Zhe_Creator";
  // 储存对象
  item;
  //临时储存对象
  tempItem;


  templateTypeArr = [
    // {name:"选择题", value: "select"},
    // {name:"连线题", value: "line"},
    // {name:"翻翻卡", value: "flip"},
    // {name:"语音评测", value: "voice"},
  ]

  isPreviewTemplate = false;




  customTypeGroupArr = [
    {
      name: '输入型文本',
      rect: true,
      isShowPos: true,
      labelArr: [
        {type:"inputText", key: "title",  title:"标题"},
        {key: "key", title:"key"},
        {key: "fontName", title:"字体"},
        {key: "fontSize", title:"大小"},
        {key: "fontColor", title:"颜色"},
      ]
    },
    {
      name: '固定型文本',
      text: true,
      audio: true,
      isShowPos: true,
      labelArr: [
        {key: "fontName", title:"字体"},
        {key: "fontSize", title:"大小"},
        {key: "fontColor", title:"颜色"},
      ]
    },
    {
      name: '输入型图片',
      rect: true,
      isShowPos: true,
      labelArr: [
        {type:"inputPic", key: "title", title:"标题"},
        {key: "key", title:"key", value:""},
      ]
    },
    {
      name: '固定型图片',
      pic: true,
      audio: true,
      anchorGroup: true,
      isShowPos: true,
    },
    {
      name: '输入型动画',
      rect: true,
      isShowPos: true,
      labelArr: [
        {key: "title", title:"标题"},
      ]
    },
   
    {
      name: '固定型动画',
      rect: true,
      anima: true,
      audio: true,
      isShowPos: true,
    }, 

    {
      name: '输入型音频',
      rect: true,
      isShowPos: true,
      labelArr: [
        {type:"inputAudio", key: "title", title:"标题"},
        {key: "key", title:"key", value:""},
      ]
    },

    {
      name: '固定型音频',
      rect: true,
      audio: true,
      pic: true,
      anim: true,
      isShowPos: true,
      
    },

  
     
    {
      name: '数据型文本',
      data: true,
      labelArr: [
        {type:"inputText", key: "title",  title:"标题"},
        {key: "key", title:"key"},
      ]
    }, 

    {
      name: '数据型图片',
      // data: true,
      labelArr: [
        {type:"inputPic", key: "title",  title:"标题", value:""},
        {key: "key", title:"key", value:""},
      ]
    }, 

   
    {
      name: '翻页按钮组',
      rect: true,
      audio: true,
    
      // radio: {
      //   title: "按钮",
      //   list: [
      //     {label: '上一页', key: 'prev'},
      //     {label: '下一页', key: 'next'},
      //     {label: '翻页文本', key: 'text'},
      //   ]
      // },

      // labelArr: [
      //   {key: "fontName", title:"字体"},
      //   {key: "fontSize", title:"大小"},
      //   {key: "fontColor", title:"颜色"},
      // ],

      // picArr: [

      // ],
      isShowPos: true,
    },


    {
      name: '录音按钮组',
      rect: true,
      audio: true,
      isShowPos: true,
    },


    

    // {
    //   name: '选择区域',
    //   rect: true,
    //   anima: true,
    //   audio: true,
    // },
    // {
    //   name: '动画',
    //   rect: true,
    //   anima: true,
    // },
    // {
    //   name: '图片',
    //   pic: true,
    // },

    // {

    //   name: '选项区域',
    //   rect: true,
    //   isCopy: true,
    //   select: [
    //     {label: '正确答案', value: 'right'},
    //     {label: '错误答案', value: 'wrong'},
    //   ],
    //   labelArr: [
    //     {title:"开始时间"},
    //     {title:"结束时间"},
    //   ]
    // },
    
    // {

    //   name: '选项图（默认，选中）',
    //   // rect: true,
    //   isCopy: true,
    //   pic: true,
    //   pic2: true,
    //   audio: true,
    //   select: [
    //     {label: '正确答案', value: 'right'},
    //     {label: '错误答案', value: 'wrong'},
    //   ],
    //   // isShowPos: true,
    //   // label: '答案',
    // },

    // {
    //   name: '提交按钮',
    //   rect: true,
    // },

    // {

    //   name: '音频区域',
    //   rect: true,
    //   isCopy: true,
    //   audio: true,
    // },
    // {
    //   name: '拖拽物',
    //   drag: true,
    //   pic: true,
    //   isCopy: true,
    // },
    // {
    //   name: '拖拽结束区',
    //   rect: true,
    //   isCopy: true,
    // },
  ];
 



  curSelectItem;
  isShowKK=false;
  curSelectKK;
  KKArray = [
    "i",
    "ɪ",
    "u",
    "ʊ",
    "ɑ",
    "ʌ",
    "ɝ",
    "ə",
    "ɚ",
    "ɔ",
    "æ",
    "ɛ",
    "e",
    "aɪ",
    "ɔɪ",
    "o",


    "aʊ",
    "p",
    "b",
    "t",
    "d",
    "k",
    "g",
    "tʃ",
    "dʒ",
    "f",
    "v",
    "θ",
    "ð",
    "s",
    "z",
    "ʃ",
    "ʒ",
    "h",
    "m",
    "n",
    "ŋ",
    "l",
    "r",
    "w",
    "j",
  ]
  customTypeGroupArr2 = [
    {
      name: '星星区域',
      rect: true,
    },
    {
      name: '选择区域',
      rect: true,
    },
  ]

  stepArr = [
    {label: '视频时间', value: 'ques'},
  ];

  videoSizeArr = [
    {key: '1920_1080', value: '1920 x 1080'},
    {key: '1280_720', value: '1280 x 720'},
    {key: '940_600', value: '940 x 600'},
  ]

  audioTypeArr = [
    {key: 'begin', info: '第一遍开始音频'},
    {key: 'begin_2', info: '第二遍开始音频'},
    {key: 'finish', info: '第二遍结束音频'},
  ]


  constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef, 
    private message: NzMessageService) {

  }


  ngOnInit() {

    this.item = {};

    // 获取存储的数据
    (<any>window).courseware.getData((data) => {


      if (data) {
        this.item = data;
        this.tempItem = data.tempItem;
      }

      this.initItem();


      console.log("this.item: ", JSON.stringify(this.item));
      
      this.init();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
      this.refresh();

    }, this.saveKey);
  }

  initItem() {
    if (this.baseData) {
      this.item = this.baseData;
    }

    if (this.tempItem) {
      this.item.tempItem = this.tempItem;
    }
  }

  init() {

    if (!this.item.templateType) {
      this.item.templateType = 'select';
    }

    if (!this.item.quesArr) {
      this.item.quesArr = [];
    }

    // if (!this.item.readingItemArr) {
    //   this.item.readingItemArr = [];
    // }

    if (this.baseData) {
      this.showPreviewTemplate();
    }
  }



  templateTypeChange(e) {
    console.log("in templateTypeChange e: ", e);
  }





  createShell() {
    this.item.wordList.push({
      word: '',
      audio: '',
      backWord: '',
      backWordAudio: '',
    });
    this.save();
  }

  removeShell(idx) {
    this.item.wordList.splice(idx, 1);
    this.save();
  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }


  itemSplitTypeChange(e, it) {
    it.splitType = e;
    this.save();
  }
  videoTypeChange(e) {
    console.log('e: ', e);
    this.item.videoType = e;
    this.save();
  }

  addBtnClick() {
    this.item.quesArr.push({
      itemType: 'jump'
    });
    this.save();
  }



  deleteBtnClick(i) {
    this.item.quesArr.splice(i, 1);
    this.save();
  }



  radioChange(e, item) {
    console.log('e: ', e);
    item.itemType = e;
    this.save();
  }

  onVideoUploadSuccess(e) {
    console.log('e: ', e);
    this.item.video_url = e.url;
    this.save();
  }

  videoSeeked(e) {

  }

  videoError(e) {

  }

  videoLoaded(e, it) {

  }


  saveHotZone(group, e) {
    console.log('e: ', e);
    const {bgItem, hotZoneItemArr} = e;

    group.bgItem = bgItem;
    group.hotZoneItemArr = hotZoneItemArr;

    this.save();
  }

  saveHotZone2(group, e) {
    console.log('e2: ', e);
    const {bgItem, hotZoneItemArr} = e;

    group.bgItem2 = bgItem;
    group.hotZoneItemArr2 = hotZoneItemArr;

    this.save();
  }


  getLRC(evt) {
    this.item.lrcData = evt;
    this.save();

  }

  onVideoUploaded(res, key, item) {
    if (!item) {
      item = this.item;
    }
    console.log('onVideoUploaded: ', res);

    item[key] = res.url;
    this.save();
  }

  isTurnPageChange(e) {
    console.log("e: ", e);
  }
  

  onItemImgUploadSuccess(e, item, key='pic_url') {
    item[key] = e.url;
    // console.log('item: ', item);
    this.save();
  }

  onItemAudioUploadSuccess(e, item, key='audio_url') {
    item[key] = e.url;
    this.save();
    // console.log('item: ', item);
  }

  /**
   * 储存图片数据
   * @param e
   */
  onImageUploadSuccess(e, key, item) {

    if (!item) {
      item = this.item;
    }
    item[key] = e.url;
    this.save();
  }

  /**
   * 储存音频数据
   * @param e
   */
  onAudioUploadSuccess(e, key, it) {
    it[key] = e.url;
    this.save();
  }

  KKBtnClick(it) {
    this.curSelectItem = it;
    this.isShowKK = true
  }

  KKHandleCancel() {
    this.isShowKK = false;
    this.curSelectKK = null;
    this.curSelectItem = null;
  }

  KKHandleOk() {

    // if (!this.curSelectItem.KKText) {
    //   this.curSelectItem.KKText = '';
    // }
    this.curSelectItem.KKText = this.curSelectKK;
   
    this.KKHandleCancel();

    this.save();
  }
 
  saveItemText(it, key) {
    console.log('it: ', it);
    this.save();
  }

  getItemHasInputPic(it) {
    return it.labelArr && it.labelArr[0]?.type == 'inputPic';
  }

  getItemHasInputAudio(it) {
    return it.labelArr && it.labelArr[0]?.type == 'inputAudio';
  }

  getItemHasInputText(it) {
    return it.labelArr && it.labelArr[0]?.type == 'inputText';
  }

  showPreviewTemplate() {
    this.isPreviewTemplate = true;

      //正常模板显示 赋值数据
      if (this.baseData && this.item.tempItem) {

        this.tempItem = this.item.tempItem;
      } else {
        
        this.tempItem = {}
        this.tempItem.quesArr = [];
        // this.addOnePageBtnClick();
      }

  }

  hidePreviewTemplate() {
    this.isPreviewTemplate = false;
  }

  addOnePageBtnClick() {
    const hotZoneItemArr = this.item?.quesArr[0]?.hotZoneItemArr;
    if (!this.tempItem || !this.tempItem.quesArr) {
      this.tempItem = {quesArr:[]};
    }
    this.tempItem.quesArr.push( 
      JSON.parse(JSON.stringify(hotZoneItemArr))
    );
    
    this.save();
  }

  deletePageBtnClick(i) {
    this.tempItem.quesArr.splice(i, 1);
    this.save();
  }


  /**
   * 储存数据
   */
  save() {

    if (this.tempItem) {
      this.item.tempItem = this.tempItem;
    }

    if (this.customTypeGroupArr) {
      this.item.customTypeGroupArr = this.customTypeGroupArr;
    }

    (<any>window).courseware.setData(this.item, null, this.saveKey);

    this.refresh();
    console.log('this.item = ' + JSON.stringify(this.item));

    this.message.create('success', "保存成功");

  }

  /**
   * 刷新 渲染页面
   */
  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }

}