//
//  IMSiLopOALanguageManage.m
//  IMSOpenAccountCustom
//
//  Created by chuntao.wang1 on 2019/4/11.
//  Copyright © 2019年 chuntao.wang1. All rights reserved.
//

#import "IMSiLopOALanguageManage.h"
#import <ALBBOpenAccountCloud/ALBBOpenAccountSDK.h>
#import "IMSiLopOAXibLanguageManage.h"
#import "IMSiLopOALocalizeManage.h"

static NSString * const kIMSiLopOpenAccountCustomBundle = @"IMSOpenAccountCustomResource";

@interface IMSiLopOALanguageManage ()

@property (nonatomic, strong) NSMutableArray <NSDictionary *> *languageArray;
@property (nonatomic, strong) NSString *currentLanguagePrefix;

@end

@implementation IMSiLopOALanguageManage

+ (instancetype)shareInstance {
    static IMSiLopOALanguageManage *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init];
    });
    
    return instance;
}

#pragma mark - Language Setting

- (void)setOpenAccountModuleLanguageWithLanguagePrefix:(NSString *)prefix {
    [self setLanguageWithLanguagePrefix:prefix bundleName:kIMSiLopOpenAccountCustomBundle];
}

- (void)setOpenAccountModuleLanguageWithLanguagePrefix:(NSString *)prefix bundleName:(NSString *)bundleName {
    [self setLanguageWithLanguagePrefix:prefix bundleName:bundleName];
}

- (void)setLanguageWithLanguagePrefix:(NSString *)prefix bundleName:(NSString *)bundleName {
    NSLog(@"IMSOpenAccountCustom info : prefix is %@ , bundle is = %@", prefix, bundleName);
    [IMSiLopOALocalizeManage cleanCache];
    NSString *prefixPath;
    NSString *languagePrefix;
    NSString *locale;
    BOOL isMatchLanguageList = NO;
    
    if (![prefix isKindOfClass:[NSString class]] || prefix == nil || prefix.length == 0) {
        // The default prefix is zh
        prefixPath = @"zh.lproj";
        languagePrefix = @"zh";
        locale = @"zh_CN";
        _currentLanguagePrefix = @"zh";
        NSLog(@"IMSOpenAccountCustom error : prefix parameter is not right");
    } else {
        NSArray *array = @[@"de", @"en", @"es", @"fr", @"ja", @"ko", @"ru", @"zh", @"hi", @"it"];
        for (NSString *language in array) {
            if ([language isEqualToString:prefix]) {
                isMatchLanguageList = YES;
                prefixPath = [prefix stringByAppendingString:@".lproj"];
                languagePrefix = prefix;
                _currentLanguagePrefix = prefix;
                break;
            }
        }
        
        NSArray <NSDictionary *> *languageList = [self jsonFilesThatSupportTheLanguage];
        if (isMatchLanguageList == NO || languageList == nil) {
            prefixPath = @"zh.lproj";
            languagePrefix = @"zh";
            locale = @"zh_CN";
            _currentLanguagePrefix = @"zh";
            NSLog(@"IMSOpenAccountCustom error : prefix is not match or languageList is nil = %@",languageList);
        } else {
            NSLog(@"IMSOpenAccountCustom supported Language list = %@",languageList);
            for (NSDictionary *dict in languageList) {
                if ([dict[@"prefix"] isEqualToString:prefix]) {
                    locale = dict[@"locale"];
                    break;
                }
            }
        }
    }
    
    // 【1】Set up server-side multilingual
    [[ALBBOpenAccountSDK sharedInstance] setRpcLocale:locale];
    
    // 【2】Set up local multilingual
    NSString *bundlePath = [NSString stringWithFormat:@"%@/%@.bundle/%@",[NSBundle mainBundle].bundlePath, bundleName, prefixPath];
    // Specify a filename for one of the supported languages, such as:zh.lproj
    [[ALBBOpenAccountSDK sharedInstance] setLocale:[NSString stringWithFormat:@"%@.lproj",languagePrefix]];
    // Specify the bundle path where the file resides
    [[ALBBOpenAccountSDK sharedInstance] setLocaleBundle:[NSBundle bundleWithPath:bundlePath]];
    
    // 【3】Set up the xib element language
    [[IMSiLopOAXibLanguageManage shareInstance] setUpTheXibResourceBundleName:bundleName];
}

- (NSString *)currentLanguagePrefix {
    return _currentLanguagePrefix ?: @"zh";
}

#pragma mark - Method

- (NSArray <NSDictionary *> *)jsonFilesThatSupportTheLanguage {
    if (_languageArray == nil) {
        NSString *path = [[self ims_getIMSOpenAccountCustomBundle] pathForResource:@"OASupportLanguage.json" ofType:nil];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        if (data == nil) {
            NSLog(@"IMSOpenAccountCustom error : data is nil");
            return nil;
        }
        
        NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if (dictArray == nil) {
            NSLog(@"IMSOpenAccountCustom error : dictArray is nil");
            return nil;
        }
        
        NSMutableArray *list = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in dictArray) {
            [list addObject:dict];
        }
        
        _languageArray = list;
    }
    
    return _languageArray;
}

- (NSBundle *)ims_getIMSOpenAccountCustomBundle {
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:kIMSiLopOpenAccountCustomBundle ofType:@"bundle"]];
    NSLog(@"IMSOpenAccountCustom info: current bundle is %@.bundle",kIMSiLopOpenAccountCustomBundle);
    return bundle;
}

@end
