#include "psrstatemanagerinterface.h"
#include <syslog.h>
#include <unistd.h>
#include <fstream>
namespace {
    const QString psr_path = "/sys/kernel/debug/dri/";
    const std::string signStr = "Currently in psr ";
    const std::string dontSupportFlag = "eDP panel does not support PSR";
}

PSRStateManager::PSRStateManager()
{
    m_supportPSR = false;
    initPSRState();

}

PSRStateManager::~PSRStateManager()
{
}

void PSRStateManager::openPSR()
{
    std::lock_guard<std::mutex> lock(m_psrMutex);
    std::fstream psrFile(m_psrFilePath);
    if(!psrFile) {
        syslog(LOG_INFO, "openPSR: can not open file\n");
        return;
    }
    psrFile << "auto\n";
    syslog(LOG_INFO, "openPSR: set psr to auto\n"); 
    psrFile.close();
}

void PSRStateManager::closePSR()
{
    std::lock_guard<std::mutex> lock(m_psrMutex);
    std::fstream psrFile(m_psrFilePath);
    if(!psrFile) {
        syslog(LOG_INFO, "closePSR: can not open file\n");
        return;
    }
    if(m_rawState != "auto") {
        psrFile << m_rawState << "\n";
        syslog(LOG_INFO, "closePSR: set psr to %s\n", m_rawState.c_str());
    } else {
        psrFile << "enter\n";
        syslog(LOG_INFO, "closePSR: set psr to enter\n");
    }  
    psrFile.close();
}

bool PSRStateManager::isSupportPSR()
{
    return m_supportPSR;
}

void PSRStateManager::initPSRState()
{
    QStringList dirs = {};
    getAllDirs(psr_path, dirs);
    qDebug() << dirs;
    for(auto psr_dir : dirs) {
        std::string whole_psr_path = psr_dir.toStdString() + "/eDP-1/psr_state";
        syslog(LOG_INFO, "initPSRState: whole_psr_path: %s\n", whole_psr_path.c_str());
        if(access(whole_psr_path.c_str(), F_OK) != 0) {
            syslog(LOG_INFO, "initPSRState: file is not exists\n");
            continue;
        }
        m_psrFilePath = whole_psr_path;
        std::fstream psrFile(whole_psr_path);
        if(!psrFile) {
            syslog(LOG_INFO, "initPSRState: can not open file\n");
            continue;
        }       

        std::string line;
        while(getline(psrFile, line)) {
            //qDebug() << "行内容：" << line;
            //syslog(LOG_INFO, "initPSRState line: %s\n", line.c_str());

            if(line.find(dontSupportFlag) != std::string::npos) {
                m_supportPSR = false;
                break;
            }
            
            if(line.find(signStr) != std::string::npos) {
                size_t pos = line.find(signStr);
                std::string subStr = line.erase(pos, signStr.length());
                //syslog(LOG_INFO, "subStr: %s, pos: %ld\n", subStr.c_str(), pos);
                size_t posTail = subStr.find(' ');
                m_rawState = subStr.substr(0, posTail);
                m_supportPSR = true;
                syslog(LOG_INFO, "initPSRState: raw state: %s, posTail: %ld\n", m_rawState.c_str(), posTail);
                break;
            }
        }

        if(m_supportPSR &&  m_rawState == "auto") {
            syslog(LOG_INFO, "initPSRState: init psr state to manual\n");
            psrFile << "enter\n";
        }
        psrFile.close();
        break;
    }
}

void PSRStateManager::getAllDirs(const QString &path, QStringList &dirs) {
    QDirIterator it(path, QDir::Dirs | QDir::NoDotAndDotDot);
    while (it.hasNext()) {
        it.next();
        QFileInfo fileInfo = it.fileInfo();
        if (fileInfo.isDir()) {
            dirs << fileInfo.filePath();
        }
    }
}
