//
//  BundleModelViewController.m
//  RedAnt ERP Mobile
//
//  Created by Ray on 10/31/15.
//  Copyright © 2015 United Software Applications, Inc. All rights reserved.
//

#import "BundleModelViewController.h"
#import "const.h"
#import "BundleModelCell.h"
#import "DefaultTableHeaderView.h"
#import "DefaultAppearance.h"

#define  DEF_CELL_HEIGHT 44
//#define  DEF_TABLE_HEIGHT 44
//#define  LINE_WIDTH 0
//#define  CELL_MARGIN 0
#define  LABEL_MARGIN 11

@interface BundleModelViewController ()

@end

@implementation BundleModelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title=@"Bundle Items";
    
    
    
    
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:self
                                                                   action:@selector( onCloseClick:)];
    //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
    //    closeButton.title = @"Close";
    
    self.navigationItem.rightBarButtonItem = closeButton;

    
    // Do any additional setup after loading the view.
}

- (void)onCloseClick:(UIButton *)sender {
    
    
    [self dismissViewControllerAnimated:true completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{

    NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];

    
    NSString* value = [item_json valueForKey:@"description"];
    
    
    float width = 254;
    width-=40;
    CGSize constraintkey = CGSizeMake(width-2*LABEL_MARGIN, 10.0f);//key label width is 40% cell width;

    
    
    CGRect frame;
    frame.size = constraintkey;
    frame.origin.x=0;
    frame.origin.y=0;
    
    RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];

    [rtlabel setText: value];
    CGSize optimumSize = [rtlabel optimumSize];
     float height = optimumSize.height;
    height = MAX(height+2*LABEL_MARGIN, DEF_CELL_HEIGHT);
    
    
    return height;

}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 0;
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 0;
//}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//        UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
//    return myView;
//
//}

//- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
////    myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
////    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
////    titleLabel.textColor=[UIColor whiteColor];
////    titleLabel.backgroundColor = [UIColor clearColor];
////    if(section==0)
////        titleLabel.text=NSLocalizedString(@"display_items", nil);
////    else
////        titleLabel.text=NSLocalizedString(@"hide_items", nil);
////    [titleLabel sizeToFit];
////    [myView addSubview:titleLabel];
////
//    return myView;
//}
//
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    if(section==0)
//        return nil;
//    else
//        return @"detail section";
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 33;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
    
    if(value==nil)
        value=@"";
    unsigned long color = strtoul([value UTF8String],0,16);
    
    DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
 //   myView.backgroundColor = UIColorFromRGB(0x996633);
    
    myView.layer.shadowColor = [UIColor blackColor].CGColor;
    myView.layer.shadowOffset = CGSizeMake(0, 0);
    myView.layer.shadowOpacity = 0.5;
    myView.layer.shadowRadius = 2.0;
    
    UILabel *modellabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 5.5, 90, 22)];
    modellabel.textColor=UIColorFromRGB(color);
    modellabel.backgroundColor = [UIColor clearColor];
    modellabel.text=NSLocalizedString(@"Model", nil);
    [modellabel sizeToFit];
    [myView addSubview:modellabel];
    
    UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(200, 5.5, 90, 22)];
    description.textColor=UIColorFromRGB(color);
    description.backgroundColor = [UIColor clearColor];
    description.text=NSLocalizedString(@"Description", nil);
    [description sizeToFit];
    [myView addSubview:description];
    
    
    UILabel *qtylabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 5.5, 90, 22)];
    qtylabel.textColor=UIColorFromRGB(color);
    qtylabel.backgroundColor = [UIColor clearColor];
    qtylabel.text=NSLocalizedString(@"QTY", nil);
    [qtylabel sizeToFit];
    [myView addSubview:qtylabel];
    
    UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(450, 5.5, 90, 22)];
    pricelabel.textColor=UIColorFromRGB(color);
    pricelabel.backgroundColor = [UIColor clearColor];
    pricelabel.text=NSLocalizedString(@"Unit Price", nil);
    [pricelabel sizeToFit];
    [myView addSubview:pricelabel];
    
    
    
    
    
    
    
    //
    return myView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
    int count =[[self.content_data valueForKey:@"count"] intValue];
    return count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    NSString *CellIdentifier = @"BundleModelCell";
    BundleModelCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];

    
    cell.labelModel.text = [item_json valueForKey:@"model"];
    if([ [NSString stringWithFormat:@"%@", [item_json valueForKey:@"unit_price"] ] isEqualToString:@"No Price"])
        cell.labelPrice.text=@"No Price";
    else
        cell.labelPrice.text=[NSString stringWithFormat:@"$ %.2f",[[item_json valueForKey:@"unit_price"] floatValue]];
    cell.labelQTY.text=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"modulus"] intValue]];
    
    cell.labelDescription.text = [item_json valueForKey:@"description"];
     cell.backgroundColor = [UIColor whiteColor];
    return cell;
    //    }
    //    else
    //    {
    //        NSString *CellIdentifier = @"OrderInfoListItem";
    //        UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    //        return cell;
    //    }
}
@end
