$(document).ready(function (){
    checkStateCookie()
})

// window.onfocus = function () {
//     getSocketState()
// }

window.onload = function (){
    changeTopStrMobile()
    middleWareJS()
    openOrCloseCurtainSupportChat()
    openOverlayOrClose()
}
//

function middleWareJS(){
    let footer = document.querySelector("footer")
    let body = document.querySelector("body")
    let type = getInfoAboutUser()
    // if (type !== 'mobile'){
    if (document.querySelector(".menu_buttons.curtain.left") || window.location.href.includes('support') || window.location.href.includes('chat')){
        footer.style.display = "none"
        body.style.overflow = "hidden"

    } else {
        footer.style.display = ""
        body.style.overflow = ""
    }
    // }/
}


function changeTopStrMobile (){
    let body = document.querySelector("body")
    // if (!window.location.href.includes("mobile")){
    //     if (!window.location.href.includes("mobile") && !window.location.href.includes("route_search_results")){
    //         window.location.href = window.location.href + `?mobile=${getInfoAboutUser() === 'mobile'}`
    //     }
        body.style.opacity = ''
        body.style.transition = '500ms'
    // } else {
        body.style.display = ''
        body.style.transition = '500ms'
    // }
}

function openOrCloseCurtainSupportChat (){
    if (window.location.href.includes("support")){
        let type_screen = getInfoAboutUser()
        let chat_block = document.querySelector(".block-chat")
        let name_ticket = document.querySelector(".name_ticket")
        let str = 'menu_buttons curtain left'
        if (type_screen === 'mobile'){
            if (!chat_block){
                str = `${str} open`
            } else {
                str = `${str} close`
            }
        } else {
            str = `${str} open`
        }
        if (name_ticket){
            str = `${str} margin`
        }
        document.querySelector(".menu_buttons").class = str
    }
}

function openOverlayOrClose () {
    if (window.location.href.includes("profile") && !window.location.href.includes("login") && !window.location.href.includes("registration")){
        let overlay = document.querySelector('.block_overlay')
        if (screen.width < 700){
            if (!overlay.classList.contains("show")){
                overlay.classList.add("show")
                overlay.classList.remove("hidden")
            }
        }
    }
}

function getTypeOfData (data) {
    let type = typeof data
    return type
}


function getInfoAboutUser (){
    let user_type = ''
    if (screen.width <= 700){
        user_type = 'mobile'
    } else if (screen.width > 1180) {
        user_type = 'desctop'
    } else if (screen.width > 700 && screen.width <= 1180) {
        user_type = 'laptop'
    }
    return user_type
}

document.addEventListener('DOMContentLoaded', function() {
     let img = document.getElementById('dropbtn_lang')
     if (window.location.href.indexOf("ru") > -1){
        img.style.background = "url('/static/img/svg/ru.svg')"
        img.style.backgroundRepeat = "no-repeat"
        img.style.backgroundPosition = "center"
         // img.style.backgroundSize = '50%'

     }else if(window.location.href.indexOf("en") > -1){
        img.style.background = "url('/static/img/svg/gb.svg')"
        img.style.backgroundRepeat = "no-repeat"
        img.style.backgroundPosition = "center"
        // img.style.backgroundSize = '50%'
     }
});

window.addEventListener('scroll', () => {
    // let headerBG = document.getElementById('header_bg')
    //
    // headerBG.style.backgroundColor = 'rgb(248 248 248 / 90%)'
    // headerBG.style.padding = '20px 40px'
  // headerBG.style.paddingBottom = 'padding: 10px 40px 10px 40px'
  // headerBG.style.paddingRight = 'padding: 10px 40px 10px 40px'
  // headerBG.style.paddingLeft = 'padding: 10px 40px 10px 40px'
})

  // Действия при изменении URL






function scroll_ev (event,el){
    let curtain = document.querySelector(".menu_buttons.filters")
    let user_type = getInfoAboutUser()
    let ch = el.scrollY
    let top = 293 - ch + 20
    if (user_type !== 'mobile' && user_type !== 'laptop'){
        if (el.scrollY === 300){
            curtain.style.top = `${top}px`
        } else if (el.scrollY < 300) {
            curtain.style.top = `${top}px`
        } else if (el.scrollY > 300){
            top = 15
            curtain.style.top = `${top}px`
        }
        if (el.scrollY === 0){
            top = 293 - ch
            curtain.style.top = `${top}px`
        }
    }
}

function checkStateCookie () {
    if (!window.document.cookie.includes("allow_cookie=true")){
        document.querySelector(".cookie_block").classList.add("show")
    }
}

function getCsrfCookie () {
    let str = window.document.cookie
    str = str.split('; ');
    let obj_cookie = {}
    for (let i = 0;i < str.length;i++){
        let cur = str[i].split('=');
        obj_cookie[cur[0]] = cur[1]
    }
    let csrf = obj_cookie['csrftoken']
    return csrf
}

function setCokie () {
    let date = new Date();
    let days = 182;
    date.setTime(+ date + (days * 86400000));
    window.document.cookie = "allow_cookie=true" + "; expires=" + date.toGMTString() + "; path=/";
    document.querySelector(".cookie_block").classList.remove("show")
    return value;
}