


let context = canvas.getContext('2d')
function handleImageLoad(context, resolve, image, x1, y1, w, h, align, zoom) {
    // image.onload = null;
    let width = image.width
    let height = image.height
    if (!image.complete) {
        return;
    }
    if (typeof w === 'string') {
        align = w;
        w = null;
        h = null;
    }
    w = w || width
    h = h || height
    if (!!align && align.indexOf('center') > -1) {
        x1 = x1 - (w / 2)
    } else if (!!align && align.indexOf('right') > -1) {
        x1 = x1 - w
    }
    if (!!align && align.indexOf('middle') > -1) {
        y1 = y1 - (h / 2)
    } else if (!!align && align.indexOf('bottom') > -1) {
        y1 = y1 - h
    }
    if (zoom) {

        context.drawImage(image, 0, 0, width, width * h / w, x1, y1, w, h)
    } else {
        context.drawImage(image, x1, y1, w, h)
    }

    let dimension = {
        startX: x1,
        startY: y1,
        endX: x1 + w,
        endY: y1 + h,
        width: w,
        height: h
    }
    if (typeof resolve === "function") {

        resolve(dimension);
    } else {
        return dimension;
    }
}
export default function drawImage(context,img, x1, y1, w, h, align, zoom) {
    return new Promise(function(resolve, reject) {
        if (typeof img === 'string') {
            let image = wx.createImage()
            image.src = img
            image.onload = function (e) {
                let width = image.width
                let height = image.height
                // console.log(`image load ${width}, ${height}`)
                handleImageLoad(context, resolve, image, x1, y1, w, h, align, zoom)
            }
            image.onerror = function (e) {
                reject(e)
            }
        } else {
            let image = img
            handleImageLoad(context, resolve, image, x1, y1, w, h, align, zoom)
        }
        

    })
}
export function drawVScrollImage(context,img, offsetX, offsetY, x, y, w, h, align) {
    return new Promise(function (resolve, reject) {
        if (typeof img === 'string') {
            let image = wx.createImage()
            image.onload = function (e) {
                let width = image.width
                let height = image.height
                if (typeof w === 'string') {
                    align = w;
                    w = null;
                    h = null;
                }
                w = w || width
                h = h || height
                if (!!align && align.indexOf('center') > -1) {
                    x = x - (w / 2)
                } else if (!!align && align.indexOf('right') > -1) {
                    x = x - w
                }
                if (!!align && align.indexOf('middle') > -1) {
                    y = y - (h / 2)
                } else if (!!align && align.indexOf('bottom') > -1) {
                    y = y - h
                }

                context.drawImage(image, offsetX, offsetY, width, width * h / w, x, y, w, h)
                resolve({
                    startX: x,
                    startY: y,
                    endX: x + w,
                    endY: y + h,
                    width: w,
                    height: h,
                    fullHeight: height * w / width
                })
            }
            image.src = img
        } else {
            let image = img
            let width = image.width
            let height = image.height
            if (typeof w === 'string') {
                align = w;
                w = null;
                h = null;
            }
            w = w || width
            h = h || height
            if (!!align && align.indexOf('center') > -1) {
                x = x - (w / 2)
            } else if (!!align && align.indexOf('right') > -1) {
                x = x - w
            }
            if (!!align && align.indexOf('middle') > -1) {
                y = y - (h / 2)
            } else if (!!align && align.indexOf('bottom') > -1) {
                y = y - h
            }

            context.drawImage(image, offsetX, offsetY, width, width * h / w, x, y, w, h)
            resolve({
                startX: x,
                startY: y,
                endX: x + w,
                endY: y + h,
                width: w,
                height: h,
                fullHeight: height * w / width
            })
        }
        
    })
}

export const drawScaleImage = function drawImage(context, img, x1, y1, align, scale) {
    return new Promise(function (resolve, reject) {

        let image = wx.createImage()
        image.onload = function (e) {
            let width = image.width
            let height = image.height
            let w = width * scale
            let h = height * scale
            if (!!align && align.indexOf('center') > -1) {
                x1 = x1 - (w / 2)
            } else if (!!align && align.indexOf('right') > -1) {
                x1 = x1 - w
            }
            if (!!align && align.indexOf('middle') > -1) {
                y1 = y1 - (h / 2)
            } else if (!!align && align.indexOf('bottom') > -1) {
                y1 = y1 - h
            }
            context.drawImage(image, x1, y1, w, h)
            resolve({
                startX: x1,
                startY: y1,
                endX: x1 + w,
                endY: y1 + h,
                width: w,
                height: h
            })
        }
        image.src = img
    })
}

export class Sprite {
    constructor(imgSrc) {
        this.img = wx.createImage()
        this.img.src = imgSrc

    }
    render(context, x1, y1, w, h, align, zoom) {
        return handleImageLoad(context, null, this.img, x1, y1, w, h, align, zoom)
    } 
}

export class Aimage {
    constructor(url) {
        this.url = url
    }
    load() {
        return new Promise((resolve, reject) => {
            let image = wx.createImage()
            image.onload = function (e) {
                let width = image.width
                let height = image.height
                resolve({
                    image: image,
                    width: width,
                    height: height
                })
            }
            image.src = this.url
            this.image = image
        })
    }
}