//
//  VeplayerSubtitle+ TTVideoEngineSubDecInfoProtocol.m
//  Pods
//
//  Created by ByteDance on 2024/9/25.
//

#import "VeplayerSubtitle+TTVideoEngineSubDecInfoProtocol.h"

@interface VeplayerTTVideoEngineSubDecInfoProtocolImpl ()

@property(nonatomic, strong) NSMutableArray *subModelList;

@end

@implementation VeplayerTTVideoEngineSubDecInfoProtocolImpl

+ (instancetype)createWithJson:(NSDictionary *)json {
  return
      [[VeplayerTTVideoEngineSubDecInfoProtocolImpl alloc] initWithJson:json];
}

+ (TTVideoEngineSubDecInfoModel *)getSubtitleInfoModelWithJson:
    (NSDictionary *)json {
  // Check input parameters
  if (!json || ![json isKindOfClass:[NSDictionary class]]) {
    return nil;
  }

  // Get subtitle list
  NSArray *subtitleArray = [json objectForKey:@"list"];
  if (!subtitleArray || ![subtitleArray isKindOfClass:[NSArray class]]) {
    return nil;
  }

  // Field mapping conversion: convert from reference code field format to
  // current code field format
  NSMutableArray *convertedSubtitles = [NSMutableArray array];

  for (NSDictionary *subtitleDict in subtitleArray) {
    if (![subtitleDict isKindOfClass:[NSDictionary class]]) {
      continue;
    }

    // Create converted subtitle dictionary
    NSMutableDictionary *convertedDict = [NSMutableDictionary dictionary];

    // Field mapping: Format -> format
    NSString *format = [subtitleDict objectForKey:@"Format"];
    if (format) {
      [convertedDict setObject:format forKey:@"format"];
    }

    // Field mapping: SubtitleId -> sub_id
    id subtitleId = [subtitleDict objectForKey:@"SubtitleId"];
    if (subtitleId) {
      [convertedDict setObject:subtitleId forKey:@"sub_id"];
    }

    // Field mapping: LanguageId -> language_id
    id languageId = [subtitleDict objectForKey:@"LanguageId"];
    if (languageId) {
      [convertedDict setObject:languageId forKey:@"language_id"];
    }

    // Field mapping: Language -> language (keep original field)
    NSString *language = [subtitleDict objectForKey:@"Language"];
    if (language) {
      [convertedDict setObject:language forKey:@"language"];
    }

    // Field mapping: SubtitleUrl -> url
    NSString *subtitleUrl = [subtitleDict objectForKey:@"SubtitleUrl"];
    if (subtitleUrl) {
      [convertedDict setObject:subtitleUrl forKey:@"url"];
    }

    // Only add to converted array when all required fields exist
    if ([convertedDict objectForKey:@"url"] &&
        [convertedDict objectForKey:@"language_id"] &&
        [convertedDict objectForKey:@"format"] &&
        [convertedDict objectForKey:@"sub_id"]) {
      [convertedSubtitles addObject:convertedDict];
    }
  }

  // Build final subtitle data structure
  NSDictionary *finalSubtitlesDictionary = @{@"list" : convertedSubtitles};

  // Create subtitle subModel
  TTVideoEngineSubDecInfoModel *subtitleInfoModel =
      [[TTVideoEngineSubDecInfoModel alloc]
          initWithDictionary:finalSubtitlesDictionary];

  return subtitleInfoModel;
}

- initWithJson:(NSDictionary *)json {
  self = [super init];
  if (self != nil) {
    _subModelList = [NSMutableArray array];

    NSArray *jsonList = [json objectForKey:@"list"];
    if (jsonList != nil) {
      for (int i = 0; i < jsonList.count; ++i) {
        NSDictionary *model = jsonList[i];
        if (model != nil && [model objectForKey:@"url"] != nil &&
            [model objectForKey:@"language_id"] &&
            [model objectForKey:@"format"] && [model objectForKey:@"sub_id"]) {
          [_subModelList addObject:model];
        }
      }
    }
  }
  return self;
}

- (NSString *_Nullable)jsonString;
{
  NSMutableArray *jsonArr = [NSMutableArray array];
  for (int i = 0; i < _subModelList.count; ++i) {
    NSDictionary *model = _subModelList[i];
    if (model != nil && [model objectForKey:@"url"] != nil &&
        [model objectForKey:@"language_id"] && [model objectForKey:@"format"] &&
        [model objectForKey:@"sub_id"]) {
      [jsonArr addObject:model];
    }
  }

  NSDictionary *dictionary = @{@"list" : jsonArr};
  NSError *error;
  NSData *jsonData =
      [NSJSONSerialization dataWithJSONObject:dictionary
                                      options:NSJSONWritingPrettyPrinted
                                        error:&error];
  if (error) {
    NSLog(@"Error converting dictionary to JSON: %@",
          error.localizedDescription);
    return nil;
  }

  NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                               encoding:NSUTF8StringEncoding];
  return jsonString;
}

- (NSInteger)subtitleCount;
{ return (NSInteger)[_subModelList count]; }

@end