//
//  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;
//}

+ (instancetype)tempChannelM3UModelWithSongs:(NSArray<id<MKRRadioResorce>> *)songs startIndex:(NSInteger)startIndex{
    MKRM3UListModel *model = [[MKRM3UListModel alloc] init];
    [model selectTempChannelTargetSongsForM3u:songs startIndex:startIndex];
    model.channelNO = @"13";
    model.timeString = [NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
    model.startIndex = 0;
    model.endIndex = (int)(songs.count > 50 ? 49 : songs.count - 1);
    model.m3uSize = (int)(songs.count >= 50 ? 50 : songs.count);
    model.channelInfoString = @"3//NULL//NULL//NULL";
    model.channelID = @"0";
    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.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.songID).stringValue];
        NSString *songIDString = [NSString stringWithFormat:@"AIRSMART//ID_%zd//",obj.songID];
        [self.urlList addObject:songIDString];
        NSString *songFrom = [NSString stringWithFormat:@"1//%zd//%zd//",[self.class convertAudioTypeWithSorceType:obj.sourceType],obj.songID];
        [self.songFromList addObject: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];
}

//后台资源类型转成本地资源类型
//type:1音乐直播 2直播电台 3音乐歌单 4主播电台 5音乐单曲 6电台节目 7电台精选
+ (MKRSourceType)convertSorceTypeWithAudioType:(NSInteger)type{
    
#define ConvertSourceTypeCase(_value,_sourceType) case _value: \
                                                    return _sourceType; \
                                                break;
    switch (type) {
        ConvertSourceTypeCase(1, MKRSourceTypeMusicRadio);
        ConvertSourceTypeCase(2, MKRSourceTypeBroadcast);
        ConvertSourceTypeCase(3, MKRSourceTypeMusic);
        ConvertSourceTypeCase(4, MKRSourceTypePodcast);
        ConvertSourceTypeCase(5, MKRSourceTypeMusic);
        ConvertSourceTypeCase(6, MKRSourceTypePodcast);
        ConvertSourceTypeCase(7, MKRSourceTypeHandPick);
    }
    return MKRSourceTypeMusic;
}

+ (NSInteger)convertAudioTypeWithSorceType:(MKRSourceType)sourceType{
    #define ConvertAudioTypeCase(_sourceType,_value) case _sourceType: \
                                                    return _value; \
                                                break;
    switch (sourceType) {
        ConvertAudioTypeCase(MKRSourceTypeMusic, 3);
        ConvertAudioTypeCase(MKRSourceTypePodcast, 4);
        ConvertAudioTypeCase(MKRSourceTypeBroadcast, 2);
        ConvertAudioTypeCase(MKRSourceTypeHandPick, 7);
        ConvertAudioTypeCase(MKRSourceTypeMusicRadio, 1);
    }
    return MKRSourceTypeMusic;
}

@end
