/**
 * @author: Clay
 * @date: 2022/1/16 11:13
 * @description：sist
 * @email: clay@hchyun.com
 */
let vm = null

import {info} from "@/api";

export function setThis(that) {
  vm = that
}

export function getParentNode(id) {
  try {
    let menuList = JSON.parse(sessionStorage.getItem("menu"));
    for (let menu of menuList) {
      for (let child of menu.children) {
        if (id == child.encodeId) {
          menu.children = []
          child.children = []
          return {
            stair: menu, second: child,
          }
        }
        for (let item of child.children) {
          if (id == item.encodeId) {
            menu.children = []
            child.children = []
            item.children = []
            return {
              stair: menu, second: child, three: item
            }
          }
        }
      }
    }
    return null
  } catch (e) {
    return null
  }
}


export function getNodeMenu(id) {
  try {
    let menuList = JSON.parse(sessionStorage.getItem("menu"));
    for (let menu of menuList) {
      for (let child of menu.children) {
        for (let item of child.children) {
          if (id == item.encodeId) {
            return item
          }
        }
      }
    }
    return null
  } catch (e) {
    return null
  }
}

export function setAppLanguage() {
  let lang = sessionStorage.getItem("lang");
  if (lang != null) {
    vm.$i18n.locale = lang
    return
  }
  var language = window.navigator.userLanguage || window.navigator.language;
  if (language == 'zh-CN') {
    lang = 'cn'
  } else {
    lang = 'en'
  }
  vm.$i18n.locale = lang
  sessionStorage.setItem("lang", lang)
}

export function setLanguage() {
  let lang = vm.$route.query && vm.$route.query.lang
  if (lang != undefined) {
    vm.$i18n.locale = lang
    sessionStorage.setItem("lang", lang)
  }
}


export function toArticle(news, width) {
  console.log(width, "width")
  info(news.id).then(res => {
    let info = null
    try {
      info = getParentNode(res.data.encodeId)
      console.log(info)
    } catch (e) {
    }
    if (info == null) {
      return
    }
    if (info.three == null) {
      if (width) {
        vm.$router.push("/pc/" + info.second.encodeId + "?id=" + news.id + "&type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
      } else {
        vm.$router.push("/mobile/" + info.second.encodeId + "?id=" + news.id + "&type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
      }
    } else {
      if (width) {
        vm.$router.push("/pc/" + info.second.encodeId + "/" + info.three.encodeId + "&id=" + news.id + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
      } else {
        vm.$router.push("/mobile/" + info.second.encodeId + "/" + info.three.encodeId + "&id=" + news.id + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
      }
    }
  })
}

export function lookMoreComment(item, width) {
  info(item.id).then(res => {
    let info = null
    try {
      info = getParentNode(res.data.encodeId)
    } catch (e) {
    }
    if (info == null) {
      return
    }
    if (info.three == null) {
      if (width) {
        vm.$router.push("/pc/" + info.second.encodeId + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
      } else {
        vm.$router.push("/mobile/" + info.second.encodeId + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
      }
    } else {
      if (width) {
        vm.$router.push("/pc/" + info.second.encodeId + "/" + info.three.encodeId + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
      } else {
        vm.$router.push("/mobile/" + info.second.encodeId + "/" + info.three.encodeId + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
      }
    }
  })
}


export function lookMoreFixedComment(id, width) {
  let info = null
  try {
    info = getParentNode(id)
    console.log(info)
  } catch (e) {
  }
  if (info == null) {
    return
  }
  if (info.three == null) {
    if (width) {
      vm.$router.push("/pc/" + info.second.encodeId + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
    } else {
      vm.$router.push("/mobile/" + info.second.encodeId + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
    }
  } else {
    if (width) {
      vm.$router.push("/pc/" + info.second.encodeId + "/" + info.three.encodeId + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
    } else {
      vm.$router.push("/mobile/" + info.second.encodeId + "/" + info.three.encodeId + "?type=" + info.second.type + "&lang=" + sessionStorage.getItem("lang"));
    }
  }
}






