//
//  MKRM3UListModel.m
//  MKRAVPlayer
//
//  Created by yyqxiaoyin on 2019/4/18.
//

#import "MKRM3UListModel.h"

@implementation MKRM3UListModel

- (instancetype)init {
    if (self = [super init]) {
        [self setupProperties];
    }
    return self;
}

+ (instancetype)tempChannelM3UModelWithSongs:(NSArray <id <MKRRadioResorce>> *)songs
                                  startIndex:(NSInteger)startIndex {
    MKRM3UListModel *model = [[MKRM3UListModel alloc] init];
    [model selectTempChannelTargetSongsForM3u:songs startIndex:startIndex];
    model.startIndex = 0;
    model.endIndex = (int)(songs.count > 50 ? 49 : songs.count - 1);
    model.channelNO = @"12";
    model.m3uSize = (int)(songs.count >= 50 ? 50 : songs.count);
    model.channelPure = @"NO";
    model.channelAlbumID = @"FREESTYLE";
    model.channelInfoString = @"3//NULL//NULL//NULL";
    model.timeString = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
    return model;
}

- (void)setupProperties{
    self.secondsList = [NSMutableArray array];
    self.durationList = [NSMutableArray array];
    self.artistList = [NSMutableArray array];
    self.albumList = [NSMutableArray array];
    self.albumArtUriList = [NSMutableArray array];
    self.titleList = [NSMutableArray array];
    self.urlList = [NSMutableArray array];
    self.cueList = [NSMutableArray array];
    self.protocolInfoList = [NSMutableArray array];
    self.upnpclassList = [NSMutableArray array];
    self.channelNumList = [NSMutableArray array];
    self.songOrderList = [NSMutableArray array];
    self.startTimeList = [NSMutableArray array];
    self.endTimeList = [NSMutableArray array];
    self.vipCanPlay = [NSMutableArray array];
    self.songIDList = [NSMutableArray array];
    self.songFromList = [NSMutableArray array];
    self.songAlbumIDList = [NSMutableArray array];
}

- (void)createM3UWithSongs:(NSArray <id <MKRRadioResorce>> *)songs{
    [songs enumerateObjectsUsingBlock:^(id <MKRRadioResorce> obj, NSUInteger idx, BOOL *stop) {
        [self.durationList addObject:@"00:00:00"];
        [self.secondsList addObject:@"0"];
        [self.artistList addObject:obj.songArtist];
        [self.albumList addObject:obj.songAlbum];
        [self.titleList addObject:obj.songName];
        [self.albumArtUriList addObject:obj.songAlbumCover];
        [self.urlList addObject:obj.songUrl];
        [self.protocolInfoList addObject:@"http-get:*:audio/mpeg:*"];
        [self.upnpclassList addObject:@"object.item.audioItem.musicTrack"];
        [self.channelNumList addObject:@(obj.songChannel).stringValue];
        [self.songOrderList addObject:@"0"];
        [self.cueList addObject:@"null"];
        [self.startTimeList addObject:@"00:00:00"];
        [self.endTimeList addObject:@"00:00:00"];
        [self.vipCanPlay addObject:[NSString stringWithFormat:@"%zd",obj.vip_canPlay]];
        [self.songIDList addObject:obj.songInfoID?:@""];
        [self.songFromList addObject:[NSString stringWithFormat:@"%zd",obj.songFrom]];
        [self.songAlbumIDList addObject:obj.songAlbumID?:@""];
    }];
}

- (void)selectTempChannelTargetSongsForM3u:(NSArray <id <MKRRadioResorce>> *)songs startIndex:(NSInteger)startIndex{
    if (songs.count <= 50) {
        [self createM3UWithSongs:songs];
        return;;
    }
    NSMutableArray *targetSongs = [NSMutableArray array];
    NSMutableArray *songsBeforeStartIndex = [songs subarrayWithRange:NSMakeRange(0, startIndex)].mutableCopy;
    NSMutableArray *songsAfterStartIndex = [songs subarrayWithRange:NSMakeRange(startIndex, songs.count - startIndex)].mutableCopy;
    if (songsAfterStartIndex.count < 50) {
        NSArray *appendSongs = [songsBeforeStartIndex subarrayWithRange:NSMakeRange(0, 50 - songsAfterStartIndex.count)];
        [targetSongs addObjectsFromArray:songsAfterStartIndex];
        [targetSongs addObjectsFromArray:appendSongs];
    }else{
        NSArray *arr = [songsAfterStartIndex subarrayWithRange:NSMakeRange(0, 50)];
        [targetSongs addObjectsFromArray:arr];
    }
    [self createM3UWithSongs:targetSongs];
}

@end
