#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <curses.h>
#include <keycodes.h>
#include <ncinput.h>
#include "mfjterm.h"
#include "misc.h"
#include "inout.h"
#include "qso.h"
#include "timefuncs.h"


char* mk_sc(char *dc)
{
       static char sc[5];
       int numlet, i, first, pos = 0;

       sc[0] = 0;
       numlet = 0;

       first = 0;
       if(dc[0] < 'A')
               first = 1;

       for (i = first; i < strlen(dc); i++)
               if (dc[i] >= 'A')
                       numlet++;
               else
                       pos = i;

       switch (numlet){
               case 3 :
                       sc[0] = dc[first];
                       sc[1] = dc[first];
                       if ((strlen(dc)-1)-pos > 1) {
                               sc[2] = dc[++pos];
                               sc[3] = dc[++pos];
                       }
                       else {
                               sc[2] = dc[++first];
                               sc[3] = dc[++pos];
                       }
                       break;
               case 4 :
                       if ((strlen(dc)-1)-pos > 2)
                               goto case5;

                       sc[0] = dc[first++];
                       sc[1] = dc[first];
                       sc[2] = dc[++pos];
                       sc[3] = dc[++pos];
                       break;
case5:
               case 5 :
                       sc[0] = dc[first];
                       sc[1] = dc[++pos];
                       sc[2] = dc[++pos];
                       sc[3] = dc[++pos];
       }

       sc[4] = 0;
       return sc;
}


void set_my_callsign(void)
{
       int i;

       attrset(A_REVERSE);
       move(19, 1);
       addstr("My Call: ");
       edgets(stdscr, callsign, 10, TRUE);
       move(19, 1);
       addstr("                              ");
       refresh();

       for (i = 0; i < strlen(callsign); i++)
               callsign[i] = toupper(callsign[i]);

       if (strlen(callsign))
               strcpy(my_selcall, mk_sc(callsign));
       else
               strcpy(my_selcall,"");

       attrset(A_REVERSE);
       move(19, 1);
       addstr("My SelCall: ");
       edgets(stdscr, my_selcall, 4, TRUE);
       move(19, 1);
       addstr("                 ");

       move(tnc_y, tnc_x);
       refresh();
}


void get_call(void)
{
       int i;

       attrset(A_REVERSE);
       move(19, 1);
       addstr("Call: ");
       edgets(stdscr, destcall, 10, TRUE);
       move(19, 1);
       addstr("                              ");
       refresh();

       for (i = 0; i < strlen(destcall); i++)
               destcall[i] = toupper(destcall[i]);

       if (opmode == AMTOR) {
               move(19, 1);
               addstr("SelCall: ");
               if (strlen(destcall))
                       strcpy(selcall, mk_sc(destcall));
               else
                       strcpy(selcall,"");
               edgets(stdscr, selcall, 4, TRUE);
               move(19, 1);
               addstr("                              ");
               refresh();
       }
       move(tnc_y, tnc_x);
       refresh();
}

/*
void lookup_call(void)
{

       return;
}
*/


void grab_callsign(void)
{
       char *p, *p2;
       int cs_len, i;

       cs_grab ^= 1;

       curs_set(0);
       attrset(A_REVERSE);
       move(19, 1);

       if (cs_grab)
               addstr("GRABBING CALLSIGN...");
       else {
               for (i = 0; i < strlen(scanstr); i++)
                       scanstr[i] = toupper(scanstr[i]);

               if ((p = strstr(scanstr, "DE "))) {
                       p += 3;

                       while (*p == ' ')
                               p++;

                       if ((p2 = strpbrk(p, " \r\n"))) {
                               if ((cs_len = p2 - p) <= 10) {
                                       strncpy(destcall, p, cs_len);
                                       destcall[cs_len] = 0;
                               }
                       }
               }
               strcpy(scanstr,"                              ");
               addstr("                    ");
       }
       move(tnc_y, tnc_x);
       curs_set(1);
       refresh();
}




void conn(void)
{
       int i;
       char s[80];

       strcpy(s,"");

       switch (opmode) {
               case AMTOR :
                       strcat(s,"ARQ ");
                       strcat(s,selcall);
                       send_string(s,1);
                       break;
               case ASCII  :
               case RTTY :
                       write_tnc(K_CTL_T);
                       write_tnc(0x0A);
                       send_ry();
                       for (i = 0; i < 3; i++) {
                               strcat(s,destcall);
                               strcat(s, " ");
                       }
                       strcat(s,"DE ");
                       for (i = 0; i < 3; i++) {
                               strcat(s, callsign);
                               strcat(s, " ");
                       }
                       strcat(s, "PSE KN KN");
                       send_string(s,1);
                       break;
               default   :
                       strcat(s,"C ");
                       strcat(s,destcall);
                       send_string(s,1);
                       break;
       }
}


void send_ry(void)
{
       if ((opmode == RTTY) || (opmode == ASCII))
               send_string("RYRYRYRYRYRYRYRYRYRYRYRYRYRYRYRY", 1);
}



void insert_call_sequence(int spc)
{
       char s[80];

       strcpy(s, "");
       strcat(s, destcall);
       strcat(s, " DE ");
       strcat(s, callsign);
       if (spc)
               strcat(s," ");
       send_string(s,0);
}


void turn_it_over(void)
{
       int type;

       insert_call_sequence(0);

       switch (opmode) {
               case AMTOR:
                       type = 0;
                       break;
               case PACTOR:
                       type = 1;
                       break;
               default:
                       type = 2;
       }

       send_string(over_str[type],1);

       if ((opmode != AMTOR) && (opmode != PACTOR))
               write_tnc(K_CTL_R);
}


void end_qso(void)
{
       insert_call_sequence(0);

       switch (opmode){
               case AMTOR:
                       send_string(" I AM NOW BREAKING THE LINK...", 1);
                       waitfor_string("...\012");
                       transmit = FALSE;
                       write_tnc(0x03);
                       zleep(20);
                       send_string("R", 1);
                       break;
               case ASCII:
               case RTTY:
                       send_string(" AR SK SK",1);
                       break;
               case PACTOR:
                       send_string(" AR SK", 1);
                       waitfor_string(" SK\012");
                       transmit = FALSE;
                       write_tnc(0x03);
                       zleep(20);
                       send_string("DISC", 1);
                       break;
               default:
                       send_string(" AR SK", 1);
                       write_tnc(0x03);
                       zleep(20);
                       send_string("DISC", 1);
       }
}