//
// Created by xin on 25-3-31.
//

#include "TG_Manager.h"
#include "string"
#include "QThread"
void TG_Manager::SetPin(int pin1_H, int pin1_L, int pin2_H, int pin2_L)
{
    // this->pin1_H = pin1_H;
    // this->pin1_L = pin1_L;
    // this->pin2_H = pin2_H;
    // this->pin2_L = pin2_L;
    InitManger();
}

void TG_Manager::SetPin_V2(int Windows_Pin_H, int Windows_Pin_L, int COOLER_Pin_EN, int HOT_Pin_EN, int Fan_Pin_H,
    int Fan_Pin_L) {
    this->Windows_Pin_H=Windows_Pin_H;
    this->Windows_Pin_L=Windows_Pin_L;
    this->COOLER_Pin_EN=COOLER_Pin_EN;
    this->HOT_Pin_EN=HOT_Pin_EN;
    this->Fan_Pin_H=Fan_Pin_H;
    this->Fan_Pin_L=Fan_Pin_L;
    InitManger();

}

void TG_Manager::Set_Waite_time_for_Windows(int time_in_seconds) {
    m_time_for_wait_windows = time_in_seconds;
}

bool TG_Manager::SetTuiGan(int IS_High)
{

  	switch (IS_High)
    {
    case 2:
        {std::string comand1 = "gpio write " + std::to_string(pin1_H) + " 0";
        std::string comand2 = "gpio write " + std::to_string(pin1_L) + " 0";
        system(comand1.c_str());
        system(comand2.c_str());
        break;}
    case 1:
        {std::string comand3 = "gpio write " + std::to_string(pin1_H) + " 1";
        std::string comand4 = "gpio write " + std::to_string(pin1_L) + " 0";
        system(comand3.c_str());
        system(comand4.c_str());
        break;}
   case 0:
        {std::string comand3 = "gpio write " + std::to_string(pin1_H) + " 0";
        std::string comand4 = "gpio write " + std::to_string(pin1_L) + " 1";
        system(comand3.c_str());
        system(comand4.c_str());
        break;}
    default:
        {std::string comand1 = "gpio write " + std::to_string(pin1_H) + " 0";
        std::string comand2 = "gpio write " + std::to_string(pin1_L) + " 0";
        system(comand1.c_str());
        system(comand2.c_str());
        break;}
    }

    if (IS_High)
    {
        std::string comand1 = "gpio write " + std::to_string(pin1_H) + " 1";
        std::string comand2 = "gpio write " + std::to_string(pin1_L) + " 0";
        system(comand1.c_str());
        system(comand2.c_str());
    }
    else
    {
        std::string comand1 = "gpio write " + std::to_string(pin1_H) + " 0";
        std::string comand2 = "gpio write " + std::to_string(pin1_L) + " 1";
        system(comand1.c_str());
        system(comand2.c_str());
    }
    return IS_High;
}
void TG_Manager::InitManger()
{
    // std::string comand1 = "gpio mode " + std::to_string(pin1_H) + " out";
    // std::string comand2 = "gpio mode " + std::to_string(pin1_L) + " out";
    // std::string comand3 = "gpio mode " + std::to_string(pin2_H) + " out";
    // std::string comand4 = "gpio mode " + std::to_string(pin2_L) + " out";
    // system(comand1.c_str());
    // system(comand2.c_str());
    // system(comand3.c_str());
    // system(comand4.c_str());
    // SetTuiGan(0);
    // SetSwitch1Status(0);
    // SetSwitch2Status(0);
    std::string comand1 = "gpio mode " + std::to_string(Windows_Pin_H) + " out";
    std::string comand2 = "gpio mode " + std::to_string(Windows_Pin_L) + " out";
    std::string comand3 = "gpio mode " + std::to_string(COOLER_Pin_EN) + " out";
    std::string comand4 = "gpio mode " + std::to_string(HOT_Pin_EN) + " out";
    std::string comand5 = "gpio mode " + std::to_string(Fan_Pin_H) + " out";
    std::string comand6 = "gpio mode " + std::to_string(Fan_Pin_L) + " out";
    system(comand1.c_str());
    system(comand2.c_str());
    system(comand3.c_str());
    system(comand4.c_str());
    system(comand5.c_str());
    system(comand6.c_str());
    comand1 = "gpio write " + std::to_string(Windows_Pin_H) + " 0";
    system(comand1.c_str());
    comand1 = "gpio write " + std::to_string(Windows_Pin_L) + " 0";
    system(comand1.c_str());
    comand1 = "gpio write " + std::to_string(COOLER_Pin_EN) + " 0";
    system(comand1.c_str());
    comand1 = "gpio write " + std::to_string(HOT_Pin_EN) + " 0";
    system(comand1.c_str());
    comand1 = "gpio write " + std::to_string(Fan_Pin_H) + " 0";
    system(comand1.c_str());
    comand1 = "gpio write " + std::to_string(Fan_Pin_L) + " 0";
    system(comand1.c_str());


}
bool TG_Manager::SetSwitch1Status(bool status)
{
    if (status)
    {
        std::string comand1 = "gpio write " + std::to_string(pin2_H) + " 1";
        system(comand1.c_str());
    }
    else
    {
        std::string comand1 = "gpio write " + std::to_string(pin2_H) + " 0";
        system(comand1.c_str());
    }

    return status;
}
bool TG_Manager::SetSwitch2Status(bool status)
{
    if (status)
    {
        std::string comand1 = "gpio write " + std::to_string(pin2_L) + " 1";
        system(comand1.c_str());
    }
    else
    {
        std::string comand1 = "gpio write " + std::to_string(pin2_L) + " 0";
        system(comand1.c_str());
    }
    return status;
}

bool TG_Manager::Heating(bool isworking)
{

    WorkingStatus = HEATING;
    return true;
}
bool TG_Manager::Cooling(bool isworking)
{

    WorkingStatus = COOLING;
    return true;
}

bool TG_Manager::Windows_Open() {
    std::string comment="gpio write " + std::to_string(Windows_Pin_H) + " 1";
    std::string comment1="gpio write " + std::to_string(Windows_Pin_L) + " 0";
    system(comment.c_str());
    system(comment1.c_str());
    //休息40秒
    QThread::sleep(m_time_for_wait_windows);

    comment="gpio write " + std::to_string(Windows_Pin_H) + " 0";
    comment1="gpio write " + std::to_string(Windows_Pin_L) + " 0";
    system(comment.c_str());
    system(comment1.c_str());

    return true;

}

bool TG_Manager::Windows_Close() {
    std::string comment="gpio write " + std::to_string(Windows_Pin_H) + " 0";
    std::string comment1="gpio write " + std::to_string(Windows_Pin_L) + " 1";
    system(comment.c_str());
    system(comment1.c_str());
    //休息40秒
    QThread::sleep(m_time_for_wait_windows);

    comment="gpio write " + std::to_string(Windows_Pin_H) + " 0";
    comment1="gpio write " + std::to_string(Windows_Pin_L) + " 0";
    system(comment.c_str());
    system(comment1.c_str());
    return true;
}

bool TG_Manager::Stop(){
    std::string comment="gpio write " + std::to_string(Windows_Pin_H) + " 0";
    std::string comment1="gpio write " + std::to_string(Windows_Pin_L) + " 0";
    system(comment.c_str());
    system(comment1.c_str());

    WorkingStatus = NotWroking;
    return true;

  }

int TG_Manager::GetManagerStatus(){
    return WorkingStatus;
  }

