//
//  DefaultAppearance.m
//  HMLG Mobile
//
//  Created by Ray on 5/25/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "DefaultAppearance.h"
#import "LoginViewController.h"
#import "CategoryCellNPD.h"
#import "CategoryHeaderView.h"
#import "DefaultTableHeaderView.h"
#import "CategoryCellSmall.H"
#import "ModelItemCell.h"
#import "PDFListTableViewCell.h"
#import "ContactListTableViewCell.h"
#import "CategoryCellNPD.h"
#import "CommonEditorCellMAction.h"
#import "DefaultImageButton.h"
#import "OrderListTableViewCell.h"
#import "CommonEditorCellModel.h"
#import "BundleDetailButton.h"

@implementation DefaultAppearance
+(void)init_appearance
{
    
    [self load_appearance];
    //gloable
//    [[UIButton appearance] setTitleColor:UIColorFromRGB(0x007aff) forState:UIControlStateNormal];
//    
//    [[UIActivityIndicatorView appearance] setColor:UIColorFromRGB(0x007aff) ];
//    
//    //category
//    [[CategoryCellNPD appearance].layer setBorderColor:[UIColor redColor].CGColor];
//    [[CategoryCellNPD appearance].layer setBorderWidth:0.5];
//    
//    
//    
//    //cell.layer.borderWidth = 0.5;
//    [[UILabel appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[CategoryHeaderView class]]] setTextColor:[UIColor darkGrayColor]];
//    
//    
//   // model detail
//    
//    [[DefaultTableHeaderView appearance] setBackgroundColor:[UIColor lightGrayColor]];
////    
//    [[UILabel appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[DefaultTableHeaderView class]]] setTextColor:[UIColor redColor]];
//    [[UILabel appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[DefaultTableHeaderView class]]] setFont:[UIFont boldSystemFontOfSize:40]];
////    
//
//    [[CategoryCellSmall appearance] setBackgroundColor:[UIColor lightGrayColor]];
//    [[UILabel appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[DefaultTableHeaderView class]]] setTextColor:[UIColor darkGrayColor]];
//    DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
    // myView.backgroundColor =  UIColorFromRGB(0x996633);
    
    
   // [[CategoryCellNPD.layer appearance]..borderColor = UIColorFromRGB(0x996633).CGColor];
//    [[LoginViewController appearance] setTitle:@"HMLG"];
    
}
+(NSString*)get_noneappearance_value:(NSString*) control valuename:(NSString*)valuename
{
    NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle]  pathForResource:@"default_appearance" ofType:@"json" ]];
    if(json==nil)
        return nil;
    NSError *error=nil;
    NSDictionary* appearance_json = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error][@"none_appearance"] ;
    NSDictionary* setting_json = appearance_json[control];
    NSString* value = setting_json[valuename];
    return value;
}
+(void)load_appearance
{
    NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle]  pathForResource:@"default_appearance" ofType:@"json" ]];
    NSError *error=nil;
    if(json==nil)
        return;
    NSDictionary* appearance_json = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error][@"appearance"] ;
    int count = [appearance_json[@"count"] intValue];
    for(int i=0;i<count;i++)
    {
        NSString * group = [NSString stringWithFormat:@"group_%d",i];
        NSDictionary* group_json = appearance_json[group];
        int item_count = [group_json[@"count"] intValue];
        for(int j=0;j<item_count;j++)
        {
            NSString * item = [NSString stringWithFormat:@"item_%d",j];
            NSDictionary* item_json = group_json[item];

            
            [self set_appearance_item:item_json];
        }
    }

}
+(id) get_appearance:(NSString*) control container:(NSDictionary*)container_json
{
    // json 中添加 control 类型必须在此函数中添加对应代码
    if([control isEqualToString:@"UIButton"])
    {
        if(container_json==nil)
            return [UIButton appearance];
        else
        {
             NSArray* arr_container = [self appearance_container:container_json];
            return [UIButton appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    if([control isEqualToString:@"UISegmentedControl"])
    {
        if(container_json==nil)
            return [UISegmentedControl appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [UISegmentedControl appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    
    else if([control isEqualToString:@"BundleDetailButton"])
    {
        if(container_json==nil)
            return [BundleDetailButton appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [BundleDetailButton appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    
    else if([control isEqualToString:@"UISearchBar"])
    {
        if(container_json==nil)
            return [UISearchBar appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [UISearchBar appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    else if([control isEqualToString:@"DefaultImageButton"])
    {
        if(container_json==nil)
            return [DefaultImageButton appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [DefaultImageButton appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    
    else if([control isEqualToString:@"UIActivityIndicatorView"])
        {
            if(container_json==nil)
                return [UIActivityIndicatorView appearance];
            else
            {
                NSArray* arr_container = [self appearance_container:container_json];
                return [UIActivityIndicatorView appearanceWhenContainedIn: arr_container[0], nil];
            }
        }
    else if([control isEqualToString:@"CategoryCellNPD"])
        {
                if(container_json==nil)
                    return [CategoryCellNPD appearance];
                else
                {
                    NSArray* arr_container = [self appearance_container:container_json];
                    return [CategoryCellNPD appearanceWhenContainedIn: arr_container[0], nil];
                }
        }
    else if([control isEqualToString:@"UILabel"])
    {
        if(container_json==nil)
            return [UILabel appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [UILabel appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    else if([control isEqualToString:@"DefaultTableHeaderView"])
    {
        if(container_json==nil)
            return [DefaultTableHeaderView appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [DefaultTableHeaderView appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    else if([control isEqualToString:@"CategoryCellSmall"])
    {
        if(container_json==nil)
            return [CategoryCellSmall appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [CategoryCellSmall appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    else if([control isEqualToString:@"UIBarButtonItem"])
    {
        if(container_json==nil)
            return [UIBarButtonItem appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [UIBarButtonItem appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    else if([control isEqualToString:@"UINavigationBar"])
    {
        if(container_json==nil)
            return [UINavigationBar appearance];
        else
        {
            NSArray* arr_container = [self appearance_container:container_json];
            return [UINavigationBar appearanceWhenContainedIn: arr_container[0], nil];
        }
    }
    return nil;
}
+(void) set_appearance_item:(NSDictionary*) item_json
{
    NSDictionary* container_json= item_json[@"container"];
    NSString* control = item_json[@"control"];
    NSString* function = item_json[@"function"];
    NSString* value = item_json[@"value"];
    id appearance=[self get_appearance:control container:container_json];
    [self appearance_function:appearance function:function value:value];
}
+(void) appearance_function:(id)appearance function:(NSString*)function value:(NSString*)value
{
    // json 中添加 function 类型必须在此函数中添加对应代码
    if([function isEqualToString:@"setTitleColor.Normal"])
    {
        if([value isEqualToString:@"#clearColor"])
        {
            [appearance setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        }
        else
        {
        unsigned long color = strtoul([value UTF8String],0,16);
        [appearance setTitleColor:UIColorFromRGB(color) forState:UIControlStateNormal];
        }
    }
    if([function isEqualToString:@"autocapitalizationType"])
    {
        if([value isEqualToString:@"UITextAutocapitalizationTypeNone"])
        {
            [appearance setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        }
        if([value isEqualToString:@"UITextAutocapitalizationTypeWords"])
        {
            [appearance setAutocapitalizationType:UITextAutocapitalizationTypeWords];
        }
        if([value isEqualToString:@"UITextAutocapitalizationTypeSentences"])
        {
            [appearance setAutocapitalizationType:UITextAutocapitalizationTypeSentences];
        }
        if([value isEqualToString:@"UITextAutocapitalizationTypeAllCharacters"])
        {
            [appearance setAutocapitalizationType:UITextAutocapitalizationTypeAllCharacters];
        }

    }
    else if([function isEqualToString:@"setColor"])
    {
        unsigned long color = strtoul([value UTF8String],0,16);
        [appearance setColor:UIColorFromRGB(color) ];
    }
//    else if([function isEqualToString:@"layer.BorderColor"])
//    {
//        unsigned long color = strtoul([value UTF8String],0,16);
//        
//        [[appearance layer] setBorderColor:UIColorFromRGB(color).CGColor];
////        [[appearance layer] setBorderColor:[UIColor redColor].CGColor];
//      //  [appearance.layer setBorderColor:[UIColor lightGrayColor].CGColor];
//       // [appearance setColor:UIColorFromRGB(color) ];
//    }
    else if([function isEqualToString:@"setTextColor"])
    {
        unsigned long color = strtoul([value UTF8String],0,16);
        [appearance setTextColor:UIColorFromRGB(color) ];
    }
    else if([function isEqualToString:@"setBackgroundColor"])
    {
        unsigned long color = strtoul([value UTF8String],0,16);
        [appearance setBackgroundColor:UIColorFromRGB(color) ];
    }
    else if([function isEqualToString:@"TintColor"])
    {
        unsigned long color = strtoul([value UTF8String],0,16);
        [appearance setTintColor:UIColorFromRGB(color) ];
    }
    
//    else if([function isEqualToString:@"setFont.Bold"])
//    {
//        long size = [value intValue];
//        [appearance setFont:[UIFont boldSystemFontOfSize:size] ];
//    }
//    [[UILabel appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[DefaultTableHeaderView class]]] setTextColor:[UIColor whiteColor]];
//    [[UILabel appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[DefaultTableHeaderView class]]] setFont:[UIFont boldSystemFontOfSize:20]];
//    
//    
//    [[CategoryCellSmall appearance] setBackgroundColor:[UIColor lightGrayColor]];
    
//    [[UILabel appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[CategoryHeaderView class]]] setTextColor:[UIColor darkGrayColor]];

    //[[UIActivityIndicatorView appearance] setColor:UIColorFromRGB(0x007aff) ];
}
+(NSArray*) appearance_container:(NSDictionary*) container_json
{
    // json 中添加 container 类型必须在此函数中添加对应代码
    NSMutableArray* arr=[[NSMutableArray alloc]init];
    
    int count = [container_json[@"count"] intValue];
    // container 只允许有一个，因为多个设置会失败，原因不明。
    count=1;
    for(int i=0;i<count;i++)
    {
        NSString * item = [NSString stringWithFormat:@"item_%d",i];
        NSString* container = container_json[item];
        if([container isEqualToString:@"CategoryHeaderView"])
        {
            [arr addObject:[CategoryHeaderView class]];
        }
        else if([container isEqualToString:@"CommonEditorCellModel"])
        {
            [arr addObject:[CommonEditorCellModel class]];
        }
        else if([container isEqualToString:@"OrderListTableViewCell"])
        {
            [arr addObject:[OrderListTableViewCell class]];
        }
        
        else if([container isEqualToString:@"CommonEditorCellMAction"])
        {
            [arr addObject:[CommonEditorCellMAction class]];
        }
        
        else if([container isEqualToString:@"DefaultTableHeaderView"])
        {
            [arr addObject:[DefaultTableHeaderView class]];
        }
        else if([container isEqualToString:@"UINavigationBar"])
        {
            [arr addObject:[UINavigationBar class]];
        }
        else if([container isEqualToString:@"UINavigationItem"])
        {
            [arr addObject:[UINavigationItem class]];
        }
        else if([container isEqualToString:@"ModelItemCell"])
        {
            [arr addObject:[ModelItemCell class]];
        }
        else if([container isEqualToString:@"PDFListTableViewCell"])
        {
            [arr addObject:[PDFListTableViewCell class]];
        }
        else if([container isEqualToString:@"UIToolbar"])
        {
            [arr addObject:[UIToolbar class]];
        }
        else if([container isEqualToString:@"ContactListTableViewCell"])
        {
            [arr addObject:[ContactListTableViewCell class]];
        }
        else if([container isEqualToString:@"CategoryCellNPD"])
        {
            [arr addObject:[CategoryCellNPD class]];
        }
        
        
    }
    return arr;
}
//[UIBarButtonItem apperanceWhenContainedIn: [UINavigationController class],nil] setBackgroundImage:bimg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
@end
