//
//  SpotCallout.m
//  TextFieldTest
//
//  Created by Mac Book DQ02 on 16/7/7.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//

#import "SpotCallout.h"

#define AspectRatio 1.85

@implementation SpotCallout

-(instancetype)initWithFrame:(CGRect)frame andImg:(NSString *)image
{
    self = [super initWithFrame:frame];
    if (self) {
        _isPlaying = NO;
        UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.width/AspectRatio)];
        imgV.image = [UIImage imageNamed:@"loading.jpg"];
        [self addSubview:imgV];
        self.spotImgV = imgV;
        self.imgUrl = image;
        dispatch_queue_t queue = dispatch_queue_create("getSpotImg", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(queue, ^{
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:image]];
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImage *img = [UIImage imageWithData:data];
                self.spotImgV.image = img;
            });
            
        });
        
        UILabel *naLab = [[UILabel alloc]initWithFrame:CGRectMake(0, imgV.frame.size.height-25, frame.size.width, 25)];
        naLab.backgroundColor = [UIColor blackColor];
        naLab.alpha = 0.5;
        naLab.font = [UIFont systemFontOfSize:15];
        naLab.textColor = [UIColor whiteColor];
        [self addSubview:naLab];
        self.nameLab = naLab;
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, frame.size.width/AspectRatio, frame.size.width, 40)];
        view.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:view];
        
        
    }
    return self;
}

-(void)setBtns
{
     NSArray *arr = @[@"jingdian-3.png",@"yuying-2.png",@"daozheli-1.png"];
    
    for (int i=0; i<(int)_btnCount; i++) {
        UIButton *btn;
        if (i==0) {
            btn = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width/_btnCount *i, self.frame.size.width/AspectRatio, (self.frame.size.width-_btnCount-1)/_btnCount+1, 40)];
        }else
        {
            btn = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width/_btnCount *i+1, self.frame.size.width/AspectRatio, (self.frame.size.width-_btnCount-1)/_btnCount+1, 40)];
        }
        btn.tag = 1000+i;
        [btn setBackgroundColor:[UIColor whiteColor]];
        UIImageView *v = [[UIImageView alloc]initWithFrame:CGRectMake((btn.frame.size.width - 43)/1.85, 3, 43, 34)];
        v.image = [[UIImage imageNamed:[arr objectAtIndex:i]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        v.tag = btn.tag +10;
        [btn addSubview:v];
        
        [btn addTarget:self action:@selector(spotBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            
        [self addSubview:btn];
     }
}

-(void)spotBtnClicked:(UIButton *)sender
{
    sender.selected = !sender.selected;

    NSLog(@"btn.tag = %ld",(long)sender.tag);
    if (sender.tag == 1000) {
        //跳转到景点详情页面(加载页面，传什么值？)，音频要不要停掉
        if (_isPlaying) {
            [_player pause];//如果正在播放，停掉，否则，不对播放器做操作
            UIButton *btn = [self viewWithTag:sender.tag+1];
            btn.selected = YES;
            UIImageView *v = [self viewWithTag:sender.tag+11];
            v.image = [UIImage imageNamed:@"yuying-2.png"];
        }
        
        if ([self.delegate respondsToSelector:@selector(pushToDetailPage:andObjectName:)]) {
            //这个地方传值的时候，如果有keyword，加上keyword
            NSString *title = self.nameLab.text;
            if (![self.keyword isEqualToString:@"NW"]) {
                title = [NSString stringWithFormat:@"%@(%@)",title,self.keyword];
            }
            [self.delegate pushToDetailPage:self.spotCode andObjectName:title];
        }
        
    }else if (sender.tag == 1002){
        //点击到这里去的时候，要传递终点值到主控制器,进行路径规划
        if ([self.delegate respondsToSelector:@selector(passEndPToGuideVC:)]) {
            [self.delegate passEndPToGuideVC:self.spotPoint];
        }
    }
    else
    {
        //播放音频文件
        NSLog(@"音频解说");
        if (_player) {
            if (sender.selected) {
                [_player pause];
                UIImageView *v = [self viewWithTag:1011];
                v.image = [UIImage imageNamed:@"yuying-2.png"];
                _isPlaying = NO;
            }else
            {
                [_player play];
                UIImageView *v = [self viewWithTag:1011];
                v.image = [UIImage imageNamed:@"yuying-3.png"];
                _isPlaying = YES;
            }
        }else
        {
            NSString *musicStr =self.audioUrl;
           // NSString *musicStr = @"http://abv.cn/music/光辉岁月.mp3";
            musicStr = [musicStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSURL *url = [NSURL URLWithString:musicStr];
            _player = [[AVPlayer alloc]initWithPlayerItem:[[AVPlayerItem alloc] initWithURL:url]];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playEnd) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
            [_player play];
            UIImageView *v = [self viewWithTag:1011];
            v.image = [UIImage imageNamed:@"yuying-3.png"];
            _isPlaying = YES;
        }
    }
    
}

-(void)playEnd
{
    NSLog(@"播放完毕");
    _isPlaying = NO;
    UIImageView *v = [self viewWithTag:1011];
    v.image = [UIImage imageNamed:@"yuying-2.png"];

    _player = nil;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
