/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * main.c
 * Copyright (C) 2019 TikalonLLC <drdev@TikalonLLC>
 * 
 * TV_commercials is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * TV_commercials is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

 /*Note that many improvements are possible, especially the use of proper
    memory allocation for the arrays.  At least this program serves as a good
    example of how two-dimensional arrays are passed by reference to functions.
  */

#include <stdio.h>
#include <stdlib.h>
#include<time.h>

#define stations 5
#define comm_num 10
#define comm_length 180
#define trials 10000

 /* Prototypes */
void srand (unsigned seed);
void populate_breaks (int station, int length, int num,
		      int start[stations][60],
		      unsigned char commercial[stations][3600]);
void exit (int status);

int i;
int j;
int k;
int num;			//number of commercial breaks in an hour
int length;			//length of commercial break in seconds
int station;			//number of station monitored
int flag;			//flags existence of overlapping commercials
int start[stations][60];	//starting time in seconds for each commercial break
int comm;			//commercial seconds overlap in an hour for designated number of stations
unsigned char commercial[stations][3600];	//arrays of flags for commercials
FILE *outdata;

void
populate_breaks (int station, int length, int num, int start[stations][60],
		 unsigned char commercial[stations][3600])
{
  int t_sec;			//length of chunk
  int i;
  int j;
//randomly populates commercial breaks, avoiding overlaps
//first check to be sure that num*length isn't greater than allowed
  if ((num * length) > 3600)
    {
      printf ("Too many commercials in an hour!\nExiting...\n");
      exit (1);
    }
//Strategy: break hour into num chunks, then position commercial break
//randomly within that chunk, accounting for possible spillage at the end
  t_sec = 3600 / num;

  for (i = 0; i < num; i++)
    {
      start[station][i] = (i * t_sec) + (rand () % (t_sec)) - length;
//printf("start%d = %d\n",station,start[station][i]);
    }

  j = 0;

  for (i = 0; i < 3600; i++)
    {
      if ((i >= start[station][j]) && (i < (start[station][j] + length)))
	{
	  commercial[station][i] = 1;
	}
      else
	{
	  commercial[station][i] = 0;
	}
//printf("%d",commercial[station][i]);
      if (i == (start[station][j] + length))
	{
	  j++;
	}

    }

}


int
main ()
{

//Open output datafile
  if ((outdata = fopen ("data.txt", "w")) == NULL)
    {
      printf ("\nOutput file cannot be opened.\n");
      exit (1);
    }
  else
    {
      printf ("Output file = data.txt\n");
    }

//seed RNG
  srand (time (0));

  num = comm_num;
  length = comm_length;

  fprintf (outdata, "number = %d\nlength = %d\ntrials = %d\n", num, length,
	   trials);

  for (j = 0; j < trials; j++)
    {

      for (station = 0; station < stations; station++)
	{
	  populate_breaks (station, length, num, start, commercial);
	}

      comm = 0;

      for (i = 0; i < 3600; i++)
	{
	  flag = 1;
	  for (k = 0; k < stations; k++)
	    {
	      flag = flag * commercial[k][i];
	    }
	  if (flag == 1)
	    {
	      comm++;
	    }
	}

      printf ("%f\n", 100 * ((float) comm / 3600));
      fprintf (outdata, "%f\n", 100 * ((float) comm / 3600));

    }
  printf ("\nDone.\n");

//Close output file
  fclose (outdata);

  return 0;
}
