#include <iostream>
#include <thread>
#include <atomic>
#include <string>

using namespace std;

class Alarm {
    public:
    Alarm(int hour, int minute, std::string speech);
    Alarm(const Alarm& other);
    Alarm& operator=(const Alarm& other);
    void start();
    void stop();
    bool isStopped() const;
//    std::string getStats() const;

    private:
    int m_hour;
    int m_minute;
    std::string m_speech;
    std::atomic<bool> m_stopped;
};

Alarm::Alarm(int hour, int minute, string speech) 
: m_hour(hour),m_minute(minute),m_speech(speech),m_stopped(false) {
}

Alarm::Alarm(const Alarm& other) 
: m_hour(other.m_hour),m_minute(other.m_minute),m_speech(other.m_speech),m_stopped(other.isStopped()) {
}
Alarm& Alarm::operator=(const Alarm& other) {
	m_hour = other.m_hour;
	m_minute = other.m_minute;
	m_speech = other.m_speech;
	m_stopped.store(other.isStopped());
	return *this;
}

void Alarm::start() {
    int currentHour, currentMinute;

    while (!Alarm::isStopped()) {
        time_t now = time(NULL);
        struct tm *current = localtime(&now);

        currentHour = current->tm_hour;
        currentMinute = current->tm_min;

        if (currentHour == m_hour && currentMinute == m_minute) {
            cout << m_speech << endl;
            m_stopped.store(true);
        }
        else {
           this_thread::sleep_for(chrono::milliseconds(1000));
        }
     }
}    

void Alarm::stop() {
    m_stopped.store(true);
}

bool Alarm::isStopped() const {
    return m_stopped.load();
}

int main(int argc, const char * argv[]) {        
    Alarm test1(12, 12, "foo");
    Alarm test2(12, 12, "foo");

    std::thread t1(&Alarm::start,test1);
    std::thread t2(&Alarm::start,test2);

    bool stop2ndThread = false;
    while(!test1.isStopped() && !test2.isStopped()) {
         this_thread::sleep_for(chrono::milliseconds(1000));
         if(!stop2ndThread) {
         	 test1.stop();
         }
         else {
         	 test2.stop();
         }
    }

    t2.join();
    t1.join();
}