import Konva from "konva";

export default function (width,height){
  const _width=width*0.134,_height=height*0.691

  const container=function (){
    const x=20,y=300

    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, 0, _width, _height*0.969);
    });
    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*0.975,
        image: this,
      });
      bgGroup.add(shape)
      shape.zIndex(0)
    }

    return {bgGroup,group}
  }

  let currentWork=null;
  let selectedShape=null;

  const getCurrentWorker=function (){
    return currentWork
  }

  const person=function (index,persons={}){
    const alignSize=2
    const width=(_width-5)/alignSize,height=110
    const x=index%alignSize*width+index%alignSize+5,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: width-width*0.3,
        y: 2,
        width: width*0.25,
        height:height*0.25,
        image: this,
        visible:false
      });
      group.add(shape)
      shape.zIndex(0)
      setSelectShape(shape)
    }

    group.on('click touchstart',function (){
      currentWork=persons
      window.localStorage.setItem('worker',JSON.stringify(currentWork))
      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.89,
        height:height*0.85,
        image: this,
      });
      group.add(shape)
      shape.zIndex(0)
    }

    const text=new Konva.Text({
      x:0,
      y:height,
      width:width,
      height:height*0.1,
      text: persons.userName,
      align: 'center',
      verticalAlign:'middle',
      fill:'#000',
      fontSize:16
    })
    group.add(text)

    return group
  }

  return{
    container,person,getCurrentWorker
  }
}
