#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dwg.h"
#include "dwg_api.h"

//read a file from disk and write it back to disk
//input filename, output filename
//each line is an entity such as a circle, line, arc, point, text, polyline
int main(int argc, char *argv[])
{
    Dwg_Data *dwg = NULL; //dwg data
    Dwg_Object *mspace;//model space
    Dwg_Object_BLOCK_HEADER *hdr;//model space header
    Dwg_Version_Type version = R_2000;//dwg version
    int imperial = 1;//imperial units
    dwg = dwg_add_Document(version, imperial, 0);//create a new dwg
    mspace = dwg_model_space_object(dwg);//get the model space
    hdr = mspace->tio.object->tio.BLOCK_HEADER;//get the model space header
    dwg_point_3d start = {0.0, 0.0, 0.0};//start point
    dwg_point_3d end = {90.0, 10.0, 0.0};//end point
    // Add a line
    Dwg_Entity_LINE *line;//line entity
    dwg_add_LINE(hdr, &start, &end);//add a line to the model space
    //add color to parent common entity
    //http://entercad.ru/acadauto.en
    //dwg_dynapi_common_set_value(line->parent, "color", "1", 0);
    //dwg_dynapi_entity_set_value(line, "COLOR", "COLOR", "1", 0);

    //dynamically add the point to the dwg_add_LINE function using cast inline
    //dwg_add_LINE(hdr, (dwg_point_3d *)&start, (dwg_point_3d *)&end);//add a line to the model space
    dwg_point_3d start2 = {5,23,0};
    dwg_point_3d end2 = {90,230,0};
    dwg_add_LINE(hdr , &start2, &end2);//add a line to the model space  

    dwg_add_LINE(hdr, &start, &end);//add a line to the model space

    /**BITCODE_CMC color_red;**/
    //malloc bitcode_cmc
    BITCODE_CMC *color_red = malloc(sizeof(BITCODE_CMC));
    color_red->index = 1;
    color_red->rgb = 0xff0000;
    color_red->name = "red";
    color_red->book_name = "ACI";
    color_red->flag = 0;

    BITCODE_CMC * color_blue = malloc(sizeof(BITCODE_CMC));
    color_blue->index = 4;
    color_blue->rgb = 0x0000ff;
    color_blue->name = "blue";
    color_blue->book_name = "ACI";
    color_blue->flag = 0;

    BITCODE_CMC * color_green = malloc(sizeof(BITCODE_CMC));
    color_green->index = 2;
    color_green->rgb = 0x00ff00;
    color_green->name = "green";
    color_green->book_name = "ACI";
    color_green->flag = 0;

    
    Dwg_Entity_CIRCLE *circle;//circle entity
    dwg_point_3d center = {0.0, 0.0, 0.0};//center point
    double radius = 10.0;//radius
    circle = dwg_add_CIRCLE(hdr, &center, radius);//add a circle to the model space
    printf("%d", circle->parent->color.index ); //prints 256 which is the default color !!
    ///dwg_dynapi_common_set_value(circle->parent, "color.index", "4", 0); //set color to red ,
    //fix above line to prevent segmentation fault
    //dwg_dynapi_common_entity_field(circle->parent, "color", color_blue, 0); //set color to bblue
    circle->parent->color.index = 1; //set color to red works
    //set color to struct
    //circle->parent->layer = "swimming";
    //dwg_dynapi_common_entity_field(circle->parent, "color", color_red, 0);
    printf("\n%d", circle->parent->color.index ); //prints 256 which is the default color !!
    //rgb
    printf("\n%d", circle->parent->color.rgb ); //prints 1
    //layer name
    //printf("%lx", circle->parent->ltype->absolute_ref ); //prints 256 which is the default color !!
    printf("\n%lx", circle->parent->layer->absolute_ref ); 
    //print color of circle
    //tio
    //circle->tio.entity->color = 4; //blue
    //get last object added to dwg
    //Dwg_Object *old_line = dwg_object_tablectrl_get_entry(dwg, dwg->num_objects - 1, 0);
    //set the color of the last object added to dwg
    //dwg_dynapi_common_set_value(old_line->parent, "color", "1", 0);
    Dwg_Object_LAYER *layer = dwg_add_LAYER (dwg, "botswana");
    layer->color.index = 6; //pink
    //circle->parent->layer->handleref = layer->tio;

    // change the color and layer of the line
    //dwg_dynapi_common_set_value(old_line->parent, "color", "1", 0); //int
    //dwg_dynapi_entity_set_value(line, "color", "red", 0); //int
   // dwg_dynapi_common_set_value(line, "color", "red", 0); //int
    //dwg_dynapi_common_set_value(line, "layer", "swimming", 1); //utf string
    //dwg_dynapi_common_set_value(line, "layer", "swimming", 1);
    //dwg_set_ENTITY_common(dwg, dwg->num_objects - 1, "color", "0");
    //Dwg_Object *line = dwg_object_table_get(dwg, dwg->num_objects - 1, 0);
    //line->tio.entity->color = 1;
    //line->tio.entity->layer = "0";
    //https://ezdxf.readthedocs.io/en/stable/concepts/aci.html#aci
    //1 = red, 2 = yellow, 3 = green, 4 = cyan, 5 = blue, 6 = magenta, 7 = black, 8 = gray, 9 = light gray




    int error;
    // Write the DWG file
    /**    The highest level function for encoding a bitstream to a file is
'dwg_write_file', which dumps the dwg to a file.

 -- Function: int dwg_write_file (char *FILENAME, Dwg_Data *DWG)
     Open FILENAME and write the DWG to it.  Return 0 if successful.*/
    //check if libredwg_simple_example_add_line_circle.dwg exists, if exists delete it
    if (remove("libredwg_simple_example_add_line_circle.dwg") != 0)
    {
        printf("Error deleting file");
    }
    error = dwg_write_file("libredwg_simple_example_add_line_circle.dwg", dwg);
    if (error >= DWG_ERR_CRITICAL)
    {
        printf("Write error in libredwg_simple_example_add_line_circle.dwg");
    }
    else
    {
        printf("DWG file successfully written");
    }
    // Free the DWG data
    dwg_free(dwg);
    return 0;
}

// -lm for math.h
// gcc -o libredwg_simple_example_add_line_circle libredwg_simple_example_add_line_circle.c -I/usr/local/include/libredwg -L/usr/local/lib -lredwg -lm
// gcc -o dwgadd dwgadd.c -I/usr/local/include/libredwg -L/usr/local/lib -lredwg -lm