import React, { Fragment } from 'react'
import { zipItemAction, sectionItemAction, judgeItemAction, transferItemAction } from './ListState'
import Item from '../components/item'

const JUDGEING = 1, JUGESUCCESS = 2, JUGEERR = 3
const SECTIONING = 4, SECTIONSUCCESS = 5, SECTIONEERR = 6
const TRANING = 7, TRANSUCCESS = 8, TRANEERR = 9
const ZIPING = 10, ZIPSUCCESS = 11, ZIPEERR = 12
const TRANSFERING = 13, TRANSFERSUCCESS = 14, TRANSFEREERR = 15



export default function Grent({ setItemStaus, modelDispatch, referData, delHandle, api, region }: any) {
  let [text, setText] = ['', (dom: HTMLInputElement) => { text = dom.value }]
  let [min, setMin] = ['9', (dom: HTMLInputElement) => {
    let i = Number(dom.value)
    let imx = Number(max)
    if (isNaN(i) || (imx && i > imx) || i < 9 || i > 20) {
      dom.value = min
    } else {
      min = dom.value
    }
  }]
  let [max, setMax] = ['20', (dom: HTMLInputElement) => {
    let i = Number(dom.value)
    let imi = Number(min)
    if (isNaN(i) || (imi && i < imi) || i < 9 || i > 20) {
      dom.value = max
    } else {
      max = dom.value
    }
  }]

  const judge = async (model: Model) => {
    model = setItemStaus(model, JUDGEING)
    let data = await judgeItemAction(modelDispatch, api.judge + model.id + '/', model)

    if (data.status !== 200) {
      alert(data.message)
      setItemStaus(model, JUGEERR)
    } else {
      setItemStaus(model, JUGESUCCESS)
      referData()
    }
  }
  const zipHandle = async (model: Model) => {
    model = setItemStaus(model, ZIPING)
    let data = await zipItemAction(modelDispatch, api.zip + model.id + '/', model)
    if (data.status !== 200) {
      alert(data.message)
      setItemStaus(model, ZIPEERR)
    } else {
      setItemStaus(model, ZIPSUCCESS)
      referData()
    }

  }
  const section = async (model: Model) => {
    let param = region ? (min + '/' + max + '/') : ''
    model = setItemStaus(model, SECTIONING)
    sectionItemAction(modelDispatch, api.section + model.id + '/' + param, model)
      .then(data => {
        if (data.status !== 200) {
          model = setItemStaus(model, SECTIONEERR)
          alert(data.message)
        } else {
          model = setItemStaus(model, SECTIONSUCCESS)
          referData()
        }
      });
  }

  const transform = async (model: Model) => {
    model = setItemStaus(model, TRANING)
    sectionItemAction(modelDispatch, api.transform + model.id + '/', model)
      .then(data => {
        if (data.status !== 200) {
          model = setItemStaus(model, TRANEERR)
          alert(data.message)
        } else {
          model = setItemStaus(model, TRANSUCCESS)
          referData()
        }
      });
  }

  const transfer = async (model: Model) => {
    model = setItemStaus(model, TRANSFERING)
    transferItemAction(modelDispatch, api.transfer + model.id + '/', model, { text: text })
      .then(data => {
        if (data.status !== 200) {
          model = setItemStaus(model, TRANSFEREERR)
          alert(data.message)
        } else {
          model = setItemStaus(model, TRANSFERSUCCESS)
          referData()
        }
      });
  }

  const ItemFn = (model: Model, privItem: Function | void) => {
    let Zip: any = <b onClick={() => zipHandle(model)} style={{ cursor: 'pointer' }}>解压</b>
    let Sect: any = <b onClick={() => section(model)} style={{ cursor: 'pointer' }}>切片</b>
    let Jude: any = <b onClick={() => judge(model)} style={{ cursor: 'pointer' }}>判断</b>
    let Tran: any = <b onClick={() => transform(model)} style={{ cursor: 'pointer' }}>转geojson</b>
    let Tf: any = (
      <Fragment>
        <input type="text" defaultValue={text} onChange={ev => setText(ev.target)} placeholder="发布参数" />
        <b onClick={() => transfer(model)} style={{ cursor: 'pointer' }}>发布</b>
      </Fragment>
    )

    if (region) {
      Sect = (
        <Fragment>
          <input type="text" defaultValue={min} onBlur={ev => setMin(ev.target)} placeholder="最小层级" />
          <input type="text" defaultValue={max} onBlur={ev => setMax(ev.target)} placeholder="最大层级" />
          {Sect}
        </Fragment>
      )
    }

    Zip = model.ajaxStatue === ZIPING ? '解压中…' : model.ajaxStatue === ZIPSUCCESS ? '成功解压' : Zip
    Sect = model.ajaxStatue === SECTIONING ? '切片中…' : model.ajaxStatue === SECTIONSUCCESS ? '成功切片' : Sect
    Jude = model.ajaxStatue === JUDGEING ? '判断中…' : model.ajaxStatue === JUGESUCCESS ? '成功判断' : Jude
    Tran = model.ajaxStatue === TRANING ? '转换中…' : model.ajaxStatue === TRANSUCCESS ? '成功转换' : Tran
    Tf = model.ajaxStatue === TRANSFERING ? '移动中…' : model.ajaxStatue === TRANSFEREERR ? '成功移动' : Tf

    if (model.status === 9) {
      Jude = '判断中…'
    }
    return (
      <Item key={model.id} {...model} >
        {() => (
          <Fragment>
            {api.zip && (model.status === 1 && Zip)}
            {api.judge && ((model.status === 2 || model.status === 9) && Jude)}
            {api.transform && ((model.status === 3 || model.status === 7) && Tran)}
            {api.section && (model.status === 4 && Sect)}
            {api.transfer && (model.status === 5 && Tf)}
            <b onClick={() => delHandle(model)} style={{ cursor: 'pointer' }}>删除</b>
            {privItem && privItem(model)}
          </Fragment>
        )}
      </Item>
    )
  }

  return {
    ItemFn,
    text
  }
}