import { type } from "os";

export default {
 
  /**
   * 获取缓存sessionStorage数据
   * @param {*str} key 
   * @param {* obj||str} val 
   */
  setCache: function (key, val) {
    if (val instanceof Array) {
      let str = JSON.stringify(val);
      window.sessionStorage.setItem(key, str);
    } else {
      window.sessionStorage.setItem(key, val);
    }

  },

  /**
   * 获取缓存sessionStorage数据
   * @param {*str} key 
   */
  getCache: function (key) {
    return window.sessionStorage.getItem(key);
  },
  
  /**
   * 获取缓存localStorage数据
   * @param {*str} key 
   * @param {* obj||str} val 
   */
  setItem: function (key,val) {
    if (val instanceof Array) {
      let str = JSON.stringify(val);
      localStorage.setItem(key, str);
    } else {
      localStorage.setItem(key, val);
    }
    //return localStorage.setItem(key,val);
  },
  
  /**
   * 获取缓存localStorage数据
   * @param {*str} key 
   */
  getItem: function (key) {
    return localStorage.getItem(key);
  },

  /**
   * 比赛时间计时器
   * @param {分钟} f 
   * @param {秒} s 
   * @param {回掉方法} fun 
   */
  timer: function (f, s, fun) {
    let timing = setInterval(function () {
      s++;
      if (s < 10) {
        s = "0" + s;
      }
      if (s == 60) {
        f++;
        s = '00';
      }
      fun && fun(f, s);
    }, 1000)
  },

  /**
   * pc端赛事详情投注方法
   * @param {array} data 当前玩法数据信息
   * @param {string} title 当前玩法
   * @param {int} index 当前玩法数据下标
   * @param {array} betting 玩法投注数据
   * @param {obj} teamName 比赛队伍名称
   * @param {string} playName 玩法名称
   * @param {string} match_id 赛事id
   * @param {string} ballId 球类
   * @param {string} limit 投注数量
   * @param {string} activity 玩法
   * @param {obj} info 当前玩法赛事信息
   * @param {function} callback 回掉方法
   */
  publicBetMethod(data,title,index,betting,teamName,playName,name,match_id,ballId,limit,bettingType,activity,info,callback){
    let isAre = true;
    let isAdd = true,isAlert =false;
    // console.log(teamName.home)
    data[index].home_team =teamName.home;
    data[index].guest_team =teamName.guest;
    data[index].playName =playName;
    data[index].name =name;
    data[index].match_id =match_id;
    data[index].ballId =ballId;
    data[index].oddsType = activity;
    if(activity == 'StRollBall'){
      data[index].roll_time = info.match_process+'-'+info.match_ptime;
      let score = info.match_score.split('-');
      data[index].home_score = score[0];
      data[index].guest_score = score[1];
    }
    let mark = data[index].mark;
    let obj = {
      title:title,
      data:[data[index]]
    }
    if(betting.length >0){
      betting.forEach(e=>{
        if(e.title == title){
          //投注数据删除
          e.data.forEach((res,num) =>{
            if(res.mark == mark){
              e.data.splice(num, 1);
              isAdd =false;
            }
          })
          //投注数据添加
          if(isAdd){
            if(limit >= 10 ) {
              isAlert =true;
            }else{
              if(bettingType == 2){
                betting.forEach(res=>{
                  if(res.data.length > 0 ){
                    for(let i=0;i< res.data.length;i++){
                      if(res.data[i].match_id ==  match_id){
                        res.data.splice(i, 1);
                        break;
                      }
                    }
                  }
                })
              }
              e.data.push(data[index]);
            }
          }
          isAre =false;
        }
      })
      //投注数据添加新玩法
      if(isAre){
        if(limit >= 10 ) {
          isAlert =true;
        }else{
          if(bettingType == 2){
            betting.forEach(res=>{
              if(res.data.length > 0 ){
                for(let i=0;i< res.data.length;i++){
                  if(res.data[i].match_id ==  match_id){
                    res.data.splice(i, 1);
                    break;
                  }
                }
              }
            })
          }
          betting.push(obj);
        }
      }
    }else{
      //初次添加投注玩法
      betting.push(obj);
    }
    callback && callback(betting,isAlert,mark)
  },

  /**
   * pc端赛事页面单式
   * @param {array} data 当前玩法数据信息
   * @param {string} title 当前玩法
   * @param {int} id 当前玩法id
   * @param {int} idx 当前玩法数据下标
   * @param {int} index 当前赛事玩法数据下标
   * @param {int} i 当前赔率玩法数据下标
   * @param {int} activity 玩法
   * @param {array} betting 玩法投注数据
   * @param {string} match_id 赛事id玩法数据
   * @param {string} ballId 球类别名
   * @param {string} limit 投注数量
   * @param {function} callback 回掉方法
   */
  publicBetting(data,title,id,index,idx,i,betting,activity,match_id,teamName,playName,name,ballId,limit,callback){
    let isAre = true;
    let isAdd = true,isAlert =false;
    let dataBetting = data[index].matchData[idx].oddsData[i];
    let data_odds = data[index].matchData[idx];
    dataBetting.home_team = teamName.home;
    dataBetting.guest_team = teamName.guest;
    dataBetting.playName = playName;
    dataBetting.name = name;
    dataBetting.ballId = ballId;
    dataBetting.match_id = match_id;
    dataBetting.oddsType = activity;
    if(activity == 'StRollBall'){
      dataBetting.roll_time = data_odds.match_process+'-'+data_odds.a_time;
      dataBetting.home_score = data_odds.home_score;
      dataBetting.guest_score = data_odds.guest_score;
    }
    let mark = data[index].matchData[idx].oddsData[i].mark;
    let obj = {
      title:title,
      data:[data[index].matchData[idx].oddsData[i]]
    }
    if(betting.length >0){
      betting.forEach(e=>{
        if(e.title == title){
            e.data.forEach((res,num) =>{
              if(res.mark == mark){
                e.data.splice(num, 1);
                isAdd =false;
              }
            })
          if(isAdd){
            if(limit >= 10 ) {
              isAlert =true;
            }else{
              e.data.push(data[index].matchData[idx].oddsData[i]);
            }
          }
          isAre =false;
        }
      })
      if(isAre){
        if(limit >= 10 ) {
          isAlert =true;
        }else{
          betting.push(obj);
        }
      }
    }else{
      betting.push(obj);
    }
    callback && callback(betting,isAlert,mark)
  },

  /**
   * pc端赛事页面串式
   * @param {array} data 当前玩法数据信息
   * @param {string} title 当前玩法
   * @param {int} id 当前玩法id
   * @param {int} idx 当前玩法数据下标
   * @param {int} index 当前赛事玩法数据下标
   * @param {int} i 当前赔率玩法数据下标
   * @param {int} activity 玩法
   * @param {array} betting 玩法投注数据
   * @param {array} match_id 赛事id玩法数据
   * @param {array} ballId 球类别名
   * @param {string} limit 投注数据
   * @param {function} callback 回掉方法
   */
  strandPublicBetting(data,title,id,index,idx,i,betting,activity,match_id,playName,name,ballId,home_team,guest_team,limit,callback){                    
    let isAre = true;
    let isAdd = true,isAlert =false;
    let dataBetting  = data[index].matchData[idx].oddsData[i];
    dataBetting.playName = playName;
    dataBetting.home_team = home_team;
    dataBetting.guest_team = guest_team;
    dataBetting.name = name;
    dataBetting.ballId = ballId;
    dataBetting.match_id = match_id;
    dataBetting.oddsType = activity;
    let mark = data[index].matchData[idx].oddsData[i].mark;
    let obj = {
      title:title,
      data:[data[index].matchData[idx].oddsData[i]]
    }
    if(betting.length >0){
      betting.forEach(e=>{
        if(e.title == title){
            e.data.forEach((res,num) =>{
              if(res.mark == mark){
                e.data.splice(num, 1);
                isAdd =false;
              }
            })
          if(isAdd){
            if(limit >= 10 ) {
              isAlert =true;
            }else{
              betting.forEach(res=>{
                if(res.data.length > 0 ){
                  for(let i=0;i< res.data.length;i++){
                    if(res.data[i].match_id ==  match_id){
                      res.data.splice(i, 1);
                      break;
                    }
                  }
                }
              })
              e.data.push(data[index].matchData[idx].oddsData[i]);
            }
          }
          isAre =false;
        }
      })
      if(isAre){
        if(limit >= 10 ) {
          isAlert =true;
        }else{
          betting.forEach(res=>{
            if(res.data.length > 0 ){
              for(let i=0;i< res.data.length;i++){
                if(res.data[i].match_id ==  match_id){
                  res.data.splice(i, 1);
                  break;
                }
              }
            }
          })
          betting.push(obj);
        }  
      }
    }else{
      betting.push(obj);
    }
    callback && callback(betting,isAlert,mark)
  }, 
  /**
   * 获取vuex相对应玩法index下标和投注数据集合
   * @param {组件类所有玩法数据集合} data 
   * @param {组件里玩法别名} title 
   * @param {从Vuex里面的投注列表的匹配对应的投注数据集合} betting 
   * @param {相对应玩法index下标} dataNum 
   */
  getBettingId(data, title, callback) {
    if (data) {
      data.forEach((res, index) => {
        if (res.title == title) {
          callback && callback(res.data, index);
        }
      });
    }
  },
  /**
   * 接口轮询定时器
   * @param {function} callback 定时器回调方法
   */
  ajaxTimerFun(callback,timers=1000*10){
    //到计时
    let  timing = setInterval(function(){
      callback && callback(timing)
    },timers);
    // },5000);
  },


  /**
   * 复合算法
   * @param {int} data  第几场赛事
   * @param {array} dataList 投注赛事列表
   */
  betPlay(data,dataList){
    let addSum =0;
    let listLenth = dataList.length;
    //调用赔率算法
    addSum = this.oddsAlgorithm(data,dataList);

    //组合算法分母
    function denominator (num){
      let group =1;
      for(let i = listLenth ; i >(listLenth -num);i--){
        let add = group*i
        group  =  add;
      }
      return group;
    }
    //组合算法分子
    function molecule (num){
      let son =1;
      for(let i = num ; i >1;i--){
        let add = son*i;
        son  =  add;
      }
      return son;
    }
    //组合算法
    function algorithm(data) {
      return {
        title:`${data}串1`,//algorithm
        index:denominator(data)/molecule(data),
        odds:addSum,
      };
    }    
    return algorithm(data);
  },

  /**
   * 赔率算法
   * @param {int} index  第几次赛事
   * @param {[]} dataList 赛事列表
   */
  oddsAlgorithm(index,dataList){
    let oddsArray=[];
    let num = index;
    let list = [];
    dataList.forEach((res)=>{
      oddsArray.push(res.odds);
    })
    for (let i=0;i<Math.pow(2, oddsArray.length);i++){
      let a = 0;
      let b = "";
      for (let j=0;j<oddsArray.length;j++){
        if (i>>j&1){
          a++;
          b += oddsArray[j]+',';
        }
      }
      if(a==num){
        let array = b.split(',');
        array.pop();
        list.push(array);
      }
    }
    let odds =0;
    list.forEach(e=>{
      let h =1;
      e.forEach(res=>{
        h = h*(1+res*1)
      })
      odds +=h*1-1;
    })
    return odds;
  },

  /**
   * 赔率数据更新
   * @param {*} current 当时数据源
   * @param {*} previous 上一次数据源
   * @param {*} type 应用场景1=赛事页面,2=赛事详情页面
   * @param {*} callback 回掉方法
   */
  oddsDataUpdate(current,previous,type,callback){
    previous = JSON.parse(previous);
    if(type == '1'){
      current.forEach((c_dat)=>{
        previous.forEach((p_dat,index)=>{
          //匹配获取联赛下的数据
          if(c_dat.lg_id == p_dat.lg_id){
            c_dat.matchData.forEach(c_dat1=>{
              p_dat.matchData.forEach((p_dat1,i)=>{
                //匹配获取赛事下的赔率数据
                if(c_dat1.match_id == p_dat1.match_id){
                  c_dat1.oddsData.forEach(c_dat2 =>{
                    p_dat1.oddsData.forEach(p_dat2=>{
                      //匹配赛事赔率数据
                      if(c_dat2.mark == p_dat2.mark){
                        if(c_dat2.odds > p_dat2.odds){
                          c_dat2.change ='rising';
                        }else if(c_dat2.odds  < p_dat2.odds){
                          c_dat2.change = 'falling';
                        }else{
                          c_dat2.change ='';
                        }
                        p_dat1.oddsData.splice(i,1)
                      }
                    })
                  })
                  p_dat.matchData.splice(i,1)
                }
              })
            })
            previous.splice(index,1)
          }
        })
      })  
      callback&&callback()
    }else if(type == '2'){
      console.log(2)
      current.forEach(e=>{
        previous.forEach((res,index)=>{
          if(e[0].p_code == res[0].p_code){
            e.forEach(dat=>{
              res.forEach((val,i)=>{
                if(dat.mark == val.mark){
                  if(dat.odds > val.odds){
                    dat.change ='rising';
                  }else if(dat.odds  < val.odds){
                    dat.change = 'falling';
                  }else{
                    dat.change ='';
                  }
                  res.splice(i,1)
                }
              })
            })
            previous.splice(index,1)
          }
        })
      })
      callback&&callback()
    }
   
  },

  getGameType(val) {
    let type = [
      {value: 'lq', label: '篮球'},
      {value: 'wq', label: '网球'},
      {value: 'zq', label: '足球'},
      {value: 'bq', label: '棒球'}
    ]
    let name = val
    for(let item of type) {
      if(item.value === val) {
          name = item.label
          break;
      }
    }
    return name;
  }
}


