import $ from 'jquery';
import lib from 'lib';
import BDY from 'BDY';

let debug = false;

var M = {
    getWxSDK : function(callback) {
        if(!lib.checkWXAgent()) {
            return;
        }

        var url = BDY.mHiydUrl + "weixin/getJsSign";
        var data = {
            url : document.location.href,
            appid : 6
        };

        lib.post(url, data, function(objResult) {
            if (objResult.result) {
                var config = objResult.data;
                callback && callback(config);
            } else {
                lib.showTip(objResult.msg);
            }
        }, {
            loading : true
        });
    },
    getWxLocation: function (callback) {
        M.getWxSDK(function(config) {
            config.debug = debug;
            config.jsApiList = ['getLocation'];

            wx.config(config);
            wx.ready(function () {
                M.getWxLocation2(callback);
            });
        });
    },
    getWxLocation2 : function(callback) {
        wx.getLocation({
            success: function (res) {
                callback && callback(res.latitude, res.longitude);
            },
            cancel: function () {
                lib.hideLoading();
            },
            fail: function (res) {
                if (res.errMsg) {
                    lib.showTip(res.errMsg);
                    lib.hideLoading();
                } else {
                    if (callback) {
                        callback(res.latitude, res.longitude);
                    } else {
                        lib.hideLoading();
                    }
                }
            }
        });
    },
    previewImage : function(imgList, curIndex) {
        lib.showLoading();
        M.getWxSDK(function(config) {
            config.debug = debug;
            config.jsApiList = ['previewImage'];

            wx.config(config);
            wx.ready(function() {
                lib.hideLoading();
                wx.previewImage({
                    current: imgList[curIndex],
                    urls: imgList
                });
            });
        });
    },
    scanQRCode : function(data) {
        lib.showLoading();
        M.getWxSDK(function(config) {
            config.debug = debug;
            config.jsApiList = ['scanQRCode'];

            wx.config(config);
            wx.ready(function() {
                lib.hideLoading();
                wx.scanQRCode(data);
            });
        });
    },
    chooseImage : function(data) {
        lib.showLoading();
        M.getWxSDK(function(config) {
            config.debug = debug;
            config.jsApiList = ['chooseImage'];

            wx.config(config);
            wx.ready(function() {
                lib.hideLoading();
                wx.chooseImage(data);
            });
        });
    },
    uploadImage : function(data) {
        lib.showLoading();
        M.getWxSDK(function(config) {
            config.debug = debug;
            config.jsApiList = ['uploadImage'];

            wx.config(config);
            wx.ready(function() {
                lib.hideLoading();
                wx.uploadImage(data);
            });
        });
    },
    chooseWXPay:function(data, redirect = null) {
        let order_sn = data.order_sn;
        let order_id = data.order_id;
        let finance_order_sn = data.finance_order_sn;
        let obj = data['platform_data'];

        lib.showLoading();
        M.getWxSDK(function(config) {
            config.debug = false;
            config.jsApiList = ['chooseWXPay'];

            wx.config(config);
            wx.ready(function() {
                lib.hideLoading();

                obj.success = function(res) {
                    // 跳转到结果页面
                    redirect && redirect('page=order-result&order_sn=' + order_sn + "&finance_order_sn=" + finance_order_sn);
                };

                obj.cancel = function(res) {
                    lib.showErrorTip('取消微信支付');
                };

                obj.fail = function(res) {
                    lib.showErrorTip('微信支付失败');
                };

                wx.chooseWXPay(obj);
            });
        });
    }
};

function getLocation(callback, hideLoading) {
    !hideLoading && lib.showLoading();
    if (lib.checkWXAgent()) {
        M.getWxLocation(callback);
    } else if (navigator.geolocation) {
        var options = {
            enableHighAccuracy: true,
            maximumAge: 1000
        };
        //浏览器支持geolocation
        navigator.geolocation.getCurrentPosition(function(position) {
            C.onLocationSucc(position, callback);
        }, C.onLocationError, options);
    } else {
        lib.showTip("浏览器不支持获取地址");
    }
}

function showGallery($imgs, index) {
    var _picList = [];
    if(lib.checkWXAgent()) {
        $imgs.each(function(index, el) {
            _picList.push($(el).attr('src'));
        });

        //调用微信图片预览接口
        M.previewImage(_picList, index);
    } else {
        $imgs.each(function(index, el) {
            _picList.push({
                src : $(el).attr("src"),
                w: parseInt($(el).width()) * 2,
                h: parseInt($(el).height()) * 2
            })
        });

        //保存当前查看图库数据
        lib.setLocalData("viewPicList", _picList);
        //跳转到图库页
        lib.redirect("page=common-gallery&pic_index=" + index);
    }
}

var C = {
    onLocationSucc: function (position, callback) {
        //经度
        var longitude = position.coords.longitude;
        //纬度
        var latitude = position.coords.latitude;

        callback(latitude, longitude);
    },
    onLocationError: function (error) {
        lib.hideLoading();
        switch (error.code) {
            case 1:
                lib.showTip("位置服务被拒绝");
                break;
            case 2:
                lib.showTip("暂时获取不到位置信息");
                break;
            case 3:
                lib.showTip("获取信息超时");
                break;
            case 4:
                lib.showTip("未知错误");
                break;
        }
    }
};

var Com = {};
Com.getLocation = getLocation;
Com.getWxLocation = M.getWxLocation;
Com.showGallery = showGallery;
Com.scanQRCode = M.scanQRCode;
Com.chooseImage = M.chooseImage;
Com.uploadImage = M.uploadImage;
Com.previewImage = M.previewImage;
Com.chooseWXPay = M.chooseWXPay;

export default Com;