import Konva from "konva";

const workers=function (width,height,persons) {

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

    const group=new Konva.Group({
      x:0,
      y:0,
      width:_width,
      height:_height,
      draggable:true,
      dragBoundFunc:function (pos){
        return {
          x:x,
          y:pos.y
        }
      },
    })

    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 < persons.length; i++) {
      const item=persons[i]
      const _group=person(i,item)
      group.add(_group)
    }

    return {bgGroup,group}
  }

  let currentWork=null;
  let selectedShape=null;

  const getCurrentWorker=function (){
    return currentWork
  }
  const person=function (index,item){
    const alignSize=2
    const width=((_width*0.9)/alignSize),height=_height/5.2
    const x=index%alignSize*width+(index%alignSize+1)*10,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
    })

    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: 0,
        y: 0,
        offsetX:-width*0.69,
        offsetY:-3,
        width: 39,
        height:39,
        image: this,
        visible:false
      });
      group.add(shape)
      shape.zIndex(0)
      setSelectShape(shape)
    }

    group.on('click touchstart',function (){
      currentWork=item
      if (selectedShape){
        selectedShape.hide()
        if (selectedShape===selectShape){
          selectedShape=null
          return
        }
      }
      selectedShape=selectShape
      selectedShape.show()
    })

    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 imageObj = new Image();
    imageObj.src = require('@/assets/sliao/headImage.png');
    imageObj.onload = function () {
      const shape = new Konva.Image({
        x: 0,
        y: 0,
        offsetX:-4,
        offsetY:-10,
        width: width*0.9,
        height:height*0.89,
        image: this,
      });
      group.add(shape)
      shape.zIndex(0)
    }

    const text=new Konva.Text({
      x:0,
      y:70,
      width: width,
      height: height,
      fill:'white',
      text:item.userName,
      align:'center',
      verticalAlign:'middle',
      fontSize:20
    })
    group.add(text)

    return group
  }

  return {
    container,getCurrentWorker,person
  }
}

export default workers
