var dever =
{
    //初始化
    init : function()
    {
        if (!this.config) {
          this.config = require('config.js').config;
          this.setting = {};
          this.log('init config', this.config);

          wx.getSystemInfo({
            success: e => {
              this.config.bar = {};
              this.config.bar.status = e.statusBarHeight;
              this.config.bar.custom = e.platform == 'android' ? e.statusBarHeight + 50 : e.statusBarHeight + 45;
            }
          })
        }
        return this;
    }

    //设置title
    ,title : function(title)
    {
      wx.setNavigationBarTitle({
        title: title
      })
    }

    //获取模板里传过来的data
    ,getData : function(event)
    {
      return event.currentTarget.dataset;
    }

    //保存数据到存储器
    ,save : function(key, value)
    {
      wx.setStorageSync(key, value);
      this.log('save ' + key, value);
    }

    //读取保存数据到存储器
    ,getSave: function (key)
    {
      wx.getStorageSync(key)
    }

    //设置服务器传过来的公共值
    ,setSetting : function(value)
    {
        this.setting = value;
        this.log('init server setting', this.setting);
        if (value.title) {
          this.title(value.title);
        } else if(value.name) {
          this.title(value.name);
        }
    }

    //获取包
    ,package : function(name)
    {
        return require('package/' + name + '.js');
    }

    //html解析
    ,html : function()
    {
        var wxParse = this.package('wxParse');
    }

    //提示框
    ,alert : function(msg, title)
    {
        wx.showToast({
          title: msg,
          icon: 'success',
          duration: 3000
        });
    }

    //跳转
    ,location : function(url)
    {
        this.log('location', url);
        wx.navigateTo({
          url: '../../template/' + url,
        })
    }

    //回退 delta 返回的页面数,如果 delta 大于现有页面数,则返回到首页。
    ,goBack: function(delta)
    {
      if(typeof delta == 'undefined') delta = 1;
      wx.navigateBack({
        delta: delta
      })
    }

    //log
    ,log : function(title, msg)
    {
      if (this.config.debug) {
        console.log('dever debug -- ' + title, msg);
      }
    }

    //加载购物车
    ,carts: function(self, save, path)
    {
      this.package('carts').load(self, save, path);
    }
}

var page =
{
  dever : dever.init()
  ,data: {}

  /**
   * 生命周期函数--监听页面加载
   */
  ,onLoad: function (options) {
    this.setConfig();
    this.stopPullDown();
    if (this.hasOwnProperty('load')) {
      this.load();
    }
  }

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  ,onReady: function () {
    if (this.hasOwnProperty('ready')) {
      this.ready();
    }
  }

  /**
   * 生命周期函数--监听页面显示
   */
  ,onShow: function () {
    if (this.hasOwnProperty('show')) {
      this.show();
    }
  }

  /**
   * 生命周期函数--监听页面隐藏
   */
  ,onHide: function () {
    if (this.hasOwnProperty('hide')) {
      this.hide();
    }
  }

  /**
   * 生命周期函数--监听页面卸载
   */
  ,onUnload: function () {
    if (this.hasOwnProperty('unload')) {
      this.unload();
    }
  }

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  ,onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.onLoad();
  }

  /**
   * 停止下拉刷新
   */
  ,stopPullDown: function () {
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  }

  /**
   * 用户点击右上角分享
   */
  ,onShareAppMessage: function () {
    if (this.hasOwnProperty('share')) {
      this.share();
    }
  }

  //设置模板变量
  ,set : function(key, value)
  {
    var data = {};
    data[key] = value;
    this.sets(data);
  }

  //设置列表页模板变量
  ,setList: function (value)
  {
    this.set('list', value);
  }

  //设置详情页模板变量
  ,setView: function (value)
  {
    this.set('view', value);
  }

  //设置模板变量
  ,sets: function (data)
  {
    this.dever.log('data', data);
    this.setData(data);
  }

  //设置基本配置模板变量
  ,setConfig: function ()
  {
    this.set('config', this.dever.config);
  }

  //获取请求
  ,request : function(url, param, callback, method)
  {
      if (!method) {
          method = 'get';
      }
      return this.dever.package('network').request(this, url, param, callback, method);
  }
}

var app =
{
  onLaunch: function () {
    if (this.hasOwnProperty('launch')) {
      this.launch();
    }
  },
  onShow: function () {
    if (this.hasOwnProperty('show')) {
      this.show();
    }
  },
  onHide: function () {
    if (this.hasOwnProperty('hide')) {
      this.hide();
    }
  },
  dever: dever,
  page: page
}

module.exports = {
    app: app
}