#include <pulse/simple.h>
#include <pulse/error.h>
#include <cmath>
#include <thread>
#include <atomic>
#include <iostream>

std::atomic<bool> running{true};
float gfreq=440;
#include <stdint.h>

#define REAL 0

typedef unsigned int divisor_t;
static_assert( sizeof(divisor_t) == 4 );

constexpr unsigned int div100( divisor_t x )
{
   // Valid till 102400
   return( ( ( x + 1 ) * 0xA3D7 ) >> 22 );
};


constexpr int div5( divisor_t x )
{
   //return( ( ( x + 1 ) * 0x19999 ) >> 19 );
   //return( ( ( x + 1 ) * 0xCCCC ) >> 18 );
   return( ( ( x + 1 ) * 0x6666 ) >> 17 );
   //return( ( ( x + 1 ) * 0x3333 ) >> 16 );
}


constexpr int div4( divisor_t x )
{
   return( x >> 2 );
}


constexpr int div400( divisor_t x )
{
   return( div100( div4( x ) ) );
}

constexpr int div7( divisor_t x )
{
   return( ( ( x + 1 ) * 0x9249 ) >> 18 );
}

static_assert( 43690 / 7 == div7( 43690 ) );

constexpr int mod4( int x )
{
   // return( x - ( div4(x) * 4 ) );
   return( x % 4 );
}

static_assert( 43690 % 4 == mod4( 43690 ) );

constexpr int mod7( int x )
{
   // return( x - ( div7(x) * 7 ) );
   return( x % 7 );
}

static_assert( 43690 % 7 == mod7( 43690 ) );

constexpr int mod100( unsigned long long int x )
{
   return( x - ( div100(x) * 100 ) );
   //return( x % 100 );
}

static_assert( 43690 % 100 == mod100( 43690 ) );

constexpr unsigned int mod400( unsigned long long int x )
{
   //return( x - ( div400(x) * 400 ) );
   return( x % 400 );
}

static_assert( 43690 % 400 == mod400( 43690 ) );

// 0 = Sonntag, 1 = Montag, ... 6 = Samstag
constexpr int weekday(int y, int m, int d)
{
    if (m < 3) {
        m += 12;
        y -= 1;
    }
#if REAL || 0
    int K = y % 100;
    int J = y / 100;
    int h = (d + (13 * (m + 1)) / 5 + K + K/4 + J/4 + 5*J) % 7;

    // Zeller: 0=Samstag,1=Sonntag,... → 0=Sonntag
    return (h + 6) % 7;
#else
    int J = div100( y );
    int K = y - ( J * 100 );
    int h = mod7(d + div5(13 * (m + 1) ) + K + div4(K) + div4(J) + 5*J );

    // Zeller: 0=Samstag,1=Sonntag,... → 0=Sonntag
    return mod7( h + 6 );
#endif
}

constexpr static int last_sunday_of_month(int year, int month)
{
    constexpr const uint8_t mdays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int days = mdays[month-1];

    // Schaltjahr
#if REAL
    if (month == 2 && ((year%4==0 && year%100!=0) || (year%400==0)))
        days = 29;
#else
    if (month == 2 && (( mod4(year)==0 && mod100(year)!=0) || ( mod400( year ) == 0 ) ) )
        days = 29;
#endif

    for (int d = days; d >= days-6; --d) {
        if (weekday(year, month, d) == 0)
            return d;
    }
    return days; // Fallback
}

// 1 = Sommerzeit, 0 = Winterzeit (Europa)
constexpr int is_dst_europe(int year, int month, int day, int hour)
{
    int start = last_sunday_of_month(year, 3);   // letzter Sonntag im März
    int end   = last_sunday_of_month(year, 10);  // letzter Sonntag im Oktober

    if (month < 3) return 0;
    if (month > 10) return 0;
    if (month > 3 && month < 10) return 1;

    if (month == 3) {
        if (day < start) return 0;
        if (day > start) return 1;
        // Wechseltag: ab 02:00 Sommerzeit
        return (hour >= 2);
    }

    if (month == 10) {
        if (day < end) return 1;
        if (day > end) return 0;
        // Wechseltag: bis 03:00 Sommerzeit
        return (hour < 3);
    }

    return 0;
}

// https://www.rechner.club/zeitumstellung/ende-sommerzeit-berechnen
// Geaendert 1996
static_assert( is_dst_europe( 1996, 3, 31, 0 ) == 0 );
static_assert( is_dst_europe( 1996, 3, 31, 1 ) == 0 );
static_assert( is_dst_europe( 1996, 3, 31, 2 ) == 1 );
static_assert( is_dst_europe( 1996, 3, 31, 3 ) == 1 );
static_assert( is_dst_europe( 1996, 10, 27, 2 ) == 1 );
static_assert( is_dst_europe( 1996, 10, 27, 3 ) == 0 );
static_assert( is_dst_europe( 1996, 10, 27, 4 ) == 0 );

static_assert( is_dst_europe( 2000, 3, 26, 0 ) == 0 );
static_assert( is_dst_europe( 2000, 3, 26, 1 ) == 0 );
static_assert( is_dst_europe( 2000, 3, 26, 2 ) == 1 );
static_assert( is_dst_europe( 2000, 3, 26, 3 ) == 1 );
static_assert( is_dst_europe( 2000, 3, 29, 4 ) == 1 );
static_assert( is_dst_europe( 2000, 10, 29, 2 ) == 1 );
static_assert( is_dst_europe( 2000, 10, 29, 3 ) == 0 );
static_assert( is_dst_europe( 2000, 10, 29, 4 ) == 0 );

static_assert( is_dst_europe( 2026, 3, 29, 0 ) == 0 );
static_assert( is_dst_europe( 2026, 3, 29, 1 ) == 0 );
static_assert( is_dst_europe( 2026, 3, 29, 2 ) == 1 );
static_assert( is_dst_europe( 2026, 3, 29, 3 ) == 1 );
static_assert( is_dst_europe( 2026, 3, 29, 4 ) == 1 );
static_assert( is_dst_europe( 2026, 3, 30, 4 ) == 1 );
static_assert( is_dst_europe( 2026, 10, 25, 2 ) == 1 );
static_assert( is_dst_europe( 2026, 10, 25, 3 ) == 0 );
static_assert( is_dst_europe( 2026, 10, 25, 4 ) == 0 );
static_assert( is_dst_europe( 2026, 10, 25, 5 ) == 0 );

static_assert( is_dst_europe( 2226, 3, 26, 0 ) == 0 );
static_assert( is_dst_europe( 2226, 3, 26, 1 ) == 0 );
static_assert( is_dst_europe( 2226, 3, 26, 2 ) == 1 );
static_assert( is_dst_europe( 2226, 3, 26, 3 ) == 1 );
static_assert( is_dst_europe( 2226, 3, 30, 4 ) == 1 );
static_assert( is_dst_europe( 2226, 10, 29, 2 ) == 1 );
static_assert( is_dst_europe( 2226, 10, 29, 3 ) == 0 );
static_assert( is_dst_europe( 2226, 10, 29, 4 ) == 0 );

static_assert( is_dst_europe( 3226, 3, 29, 0 ) == 0 );
static_assert( is_dst_europe( 3226, 3, 29, 1 ) == 0 );
static_assert( is_dst_europe( 3226, 3, 29, 2 ) == 1 );
static_assert( is_dst_europe( 3226, 3, 29, 3 ) == 1 );
static_assert( is_dst_europe( 3226, 3, 30, 4 ) == 1 );
static_assert( is_dst_europe( 3226, 10, 25, 2 ) == 1 );
static_assert( is_dst_europe( 3226, 10, 25, 3 ) == 0 );
static_assert( is_dst_europe( 3226, 10, 25, 4 ) == 0 );

typedef unsigned long long int  ui;

int main() {
    std::cout << "Starting tone in background...\n";

    //printf("12345/5=%d\n", div5(-12345));
    //return(0);

    // 102400-1
    // -(1<<16)
    for(int i1=0; i1< (1<<16) + 4; i1++)
    {
       if( (i1/100) != div100(i1) )
       {
          printf( "Error 100: %d: %d; delta %d\n", i1, div100(i1), (i1/100) - div100(i1) );
       }
       if( (i1/5) != div5(i1) )
       {
          printf( "Error 5: %d: %d vs. %d; delta %d\n", i1, i1/5, div5(i1), (i1/5) - div5(i1) );
       }
       if( (i1/7) != div7(i1) )
       {
          printf( "Error 7: %d: %d vs. %d; delta %d\n", i1, i1/7, div7(i1), (i1/7) - div7(i1) );
       }

       if( (i1/400) != div400(i1) )
       {
          printf( "Error 400: %d: %d vs. %d; delta %d\n", i1, i1/400, div400(i1), (i1/400) - div400(i1) );
       }
    }


    printf( "At least < 100pb\n" );
    // find nearest
    //for(ui i1=0; i1< 0xffffffff; i1++)
    //ui i1=0xA3D7;
    //ui divider=100ull;
    ui divider=5ull;

    for(ui i1=0; i1< 32; i1++)
    {
       ui m=(1ull << i1)/divider;
       //m++;
       ui r = ( 10240 * m ) >> i1;
       ui d = (1ull << i1)-( m * divider);
       ui pb = (d*1000ull*1000ull);

       if(m)
          pb/=m;
       else
          pb=-1;

       //if( r == (10240/divider) )
       {
         printf("m=0x%llX; s=%d; r=%d; max=%d; fehler=%dpb\n", m, i1, r
                , 0xFFFFFFFF / ( m ? m : 1 )
                , pb);
              // "102400/100=%d\n", r);
       }
    }

    return(0);
}
