import Konva from "konva";

let selectIndex=null

const liliao=function (width,height,dw,callback) {

  const _width=width*0.4,_height=height*0.715
  const container=function (){
    const x=width*0.2+30,y=262

    const group=new Konva.Group({
      x:0,
      y:0,
      width:_width,
      height:_height
    })

    const bgGroup=new Konva.Group({
      x:x,
      y:y,
      width:_width,
      height:_height,
    })
    bgGroup.clipFunc(function(ctx) {
      ctx.rect(0, 6, _width, _height-11);
    });
    bgGroup.add(group)

    const imageObj = new Image();
    imageObj.src = require('@/assets/sliao/personbg.png');
    imageObj.onload = function () {
      const shape = new Konva.Image({
        x: 0,
        y: 0,
        width: _width,
        height:_height,
        image: this,
      });
      bgGroup.add(shape)
      shape.zIndex(0)
    }

    for (let i = 0; i < dw.length; i++) {
      const item=dw[i]
      const _group=duowei(i,item)
      group.add(_group)
    }

    return bgGroup
  }

  let currentDw=null
  const getCurrentDw=function (){
    return currentDw
  }

  let selectedShape=null;
  const getSelectShape=function (){
    return selectedShape
  }

  const nonSelectShape=function (){
    selectedShape.hide()
    selectedShape=null
  }

  const duowei=function (index,item){
    const alignSize=3
    const width=((_width-60)/alignSize),height=208
    const x=index%alignSize*width+index%alignSize*10+20,y=height*(Math.floor(index/alignSize))+(Math.floor(index/alignSize)+1)*10
    const group=new Konva.Group({
      x:x,
      y:y,
      width:width,
      height:height
    })
    group.on('click touchstart',function (){
      if (selectedShape){
        selectedShape.hide()
        if (selectedShape===selectShape){
          selectedShape=null
          callback(null,null)
          return
        }
      }
      selectedShape=selectShape
      selectedShape.show()
      currentDw=item
      selectIndex=index
      callback(index,dw[index])
    })

    const bgObj = new Image();
    bgObj.src = require('@/assets/sliao/personbg1.png');
    bgObj.onload = function () {
      const shape = new Konva.Image({
        x: 0,
        y: 0,
        offsetX:4,
        width: width,
        height:height,
        image: this,
      });
      group.add(shape)
      shape.zIndex(0)
    }

    const selectObj = new Image();
    selectObj.src = require('@/assets/sliao/selected.png');
    let selectShape=null
    const setSelectShape=function (shape){
      selectShape=shape
    }
    selectObj.onload = function () {
      const shape = new Konva.Image({
        x: width-width*0.30,
        y: 4,
        width: width*0.25,
        height:width*0.25,
        image: this,
        visible:false
      });
      group.add(shape)
      shape.zIndex(0)
      setSelectShape(shape)
      if (selectIndex===index){
        selectedShape=shape
        shape.show()
      }
    }

    const text=new Konva.Text({
      x:0,
      y:0,
      width: width,
      height: height,
      fill:'#fff',
      text:`${item.name}\n${item.list.length}张钢板`,
      align:'center',
      verticalAlign:'middle',
      fontSize:20
    })
    group.add(text)

    return group
  }

  return {
    container,duowei,getCurrentDw,getSelectShape,nonSelectShape
  }
}

export default liliao
