window.jssdk = {

}
window.jssdk.savePricToAlbum = function savePricToAlbum (src) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrModel.saveImagToAlbum(src, function (success) {
        resolve(success)
      }, function (error) {
        reject(error)
      })
    } else {
      reject(new Error('no cordova'))
    }
  })
}
window.jssdk.jumpbackNative = function jumpbackNative () {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrModel.backToNative(function (success) {
        resolve(success)
      }, function (error) {
        reject(error)
      })
    } else {
      reject(new Error('no cordova'))
    }
  })
}
window.jssdk.deviceInfo = function deviceInfo () {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      device.getInfo(function (obj) {
        resolve(obj)
      }, function (error) {
        reject(error)
      })
    } else {
      resolve('')
    }
  })
}
window.jssdk.camera = function camera (options) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      let options = options
      navigator.camera.getPicture(function (imageData) {
        resolve(imageData)
      }, function (message) {}, options)
    } else {
      reject(new Error('no cordova'))
    }
  })
}
window.jssdk.ImagePicker = function ImagePicker (options) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.imagePicker.getPictures(function (results) {
        resolve(results)
      }, function (error) {
        reject(error)
      }, options)
    } else {
      reject(new Error('no cordova'))
    }
  })
}
window.jssdk.getCode = function getCode (appId) {
  let paramse = {
    appId: appId
  }
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrJsBridge.call((returnValue) => {
        resolve(returnValue)
      }, (error) => {
        reject(error)
      }, 'getCode', paramse)
    }
  })
}

window.jssdk.currentTownInfo = function currentTownInfo () {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrJsBridge.call((returnValue) => {
        resolve(returnValue)
      }, (error) => {
        reject(error)
      }, 'currentTownInfo', {})
    }
  })
}

window.jssdk.networkState =  function networkState () {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      var networkState = ''
      networkState = navigator.connection.type
      var states = {}
      states[Connection.UNKNOWN] = 'Unknown connection'
      states[Connection.ETHERNET] = 'Ethernet connection'
      states[Connection.WIFI] = 'WiFi connection'
      states[Connection.CELL_2G] = 'Cell 2G connection'
      states[Connection.CELL_3G] = 'Cell 3G connection'
      states[Connection.CELL_4G] = 'Cell 4G connection'
      states[Connection.CELL] = 'Cell generic connection'
      states[Connection.NONE] = 'No network connection'
      resolve(states[networkState])
    }
  })
}

window.jssdk.scanbarClick = function scanbarClick () {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrModel.scanbarClick(function (success) {
        resolve(success)
      })
    }
  })
}

// 鍦板浘瀵艰埅
window.jssdk.mapNavigation = function mapNavigation (params) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      let obj = {
        originLat: '',
        originLong: '',
        destinLat: '',
        destinLong: ''
      }
      if (!params) {
        reject('鍙傛暟閿欒')
        return
      }
      if (params.hasOwnProperty('originLat') && params.hasOwnProperty('originLong')) {
        if (params.originLat.length > 0 && params.originLat.length > 0) {
          obj.originLat = params.originLat
          obj.originLong = params.originLong
        } else {
          obj.originLat = ''
          obj.originLong = ''
        }
      }
      if (params.hasOwnProperty('destinLat') && params.hasOwnProperty('destinLong')) {
        if (params.destinLat.length > 0 && params.destinLong.length > 0) {
          obj.destinLat = params.destinLat
          obj.destinLong = params.destinLong
        } else {
          reject('缁堢偣浣嶇疆涓嶈兘涓虹┖')
          return
        }
      }
      window.MrModel.mapNavigation(obj, function () {
        resolve()
      }, function (error) {
        reject(error)
      })
    }
  })
}

window.jssdk.toOutterLink = function toOutterLink (obj) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrModel.toOutterLink(obj, function (success) {
        resolve()
      })
    }
  })
}

window.jssdk.showShareView =  function showShareView (obj) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrModel.showShareView(obj, function (success) {
        resolve(success)
      })
    }
  })
}

window.jssdk.setLocationString = function setLocationString (key, value) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.NativeStorage.setString(key, value, (success) => {
        if (success === 'null') resolve(undefined)
        resolve(success)
      }, (error) => {
        reject(error)
      })
    } else {
      window.localStorage.setItem(key, value)
      resolve()
    }
  })
}
window.jssdk.getLocationString = function getLocationString (key) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.NativeStorage.getString(key, (success) => {
        if (success === 'null') resolve(undefined)
        resolve(success)
      }, (error) => {
        reject(error)
      })
    } else {
      let success = window.localStorage.getItem(key)
      resolve(success)
    }
  })
}
window.jssdk.removeLocationString = function removeLocationString (key) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.NativeStorage.remove(key, (success) => {
        if (success === 'null') resolve(undefined)
        resolve(success)
      }, (error) => {
        reject(error)
      })
    } else {
      window.localStorage.removeItem(key)
      resolve()
    }
  })
}
window.jssdk.shareToWx = function shareToWx (argument) {
  // body...
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrJsBridge.call((returnValue) => {
      }, (error) => {
        reject(error)
      }, 'shareToWx', argument)
    }
  })
}
window.jssdk.getShareView = function getShareView () {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrJsBridge.call((returnValue) => {
        resolve(returnValue)
      }, (error) => {
        reject(error)
      }, 'getShareView', '')
    }
  })
}
window.jssdk.payOrder = function payOrder (params) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrJsBridge.call(
        (success) => {
          resolve(success)
        },
        (error) => {
          reject(error)
        },
        'payOrder', params
      )
    }
  })
}
window.jssdk.lockListMansion = function lockListMansion () {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      window.MrJsBridge.call((list) => {
        resolve(JSON.parse(list))
      }, (error) => {
        reject(error)
      }, 'lockListMansion', {})
    }
  })
}
window.jssdk.lockOpenMansion = function lockOpenMansion (id, pwd) {
  return new Promise((resolve, reject) => {
    if (window.cordova) {
      let params = {
        id: id,
        pwd: pwd
      }
      window.MrJsBridge.call((success) => {
        resolve(success)
      }, (error) => {
        reject(error)
      }, 'lockOpenMansion', params)
    }
  })
}
