//
//  postMessageFirstCatViewController.m
//  Open
//
//  Created by 看看智能 on 2017/3/8.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "postMessageFirstCatViewController.h"
#import "UIImageView+WebCache.h"
#import "UIImageView+LBBlurredImage.h"
#import "feedbackViewController.h"
#import "VoucherRecordViewController.h"
#import <SDWebImage/SDImageCache.h>
@interface postMessageFirstCatViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet  UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (weak, nonatomic) IBOutlet UILabel *craftLab;
@property (weak, nonatomic) IBOutlet UILabel *childCraftLab;
@property (weak, nonatomic) IBOutlet UIButton *telBut;
@property (weak, nonatomic) IBOutlet UITextView *textViews;

@property (weak, nonatomic) IBOutlet UIImageView *imageView_1;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight_1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight_2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_3;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight_3;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_4;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight_4;

@property (weak, nonatomic) IBOutlet UIScrollView *scroll;
@property (weak, nonatomic) IBOutlet UIView *behindView;

//电话号码的按钮点击
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel_tel;
@property (weak, nonatomic) IBOutlet UIButton *editIphone;

@property (weak, nonatomic) IBOutlet UIButton *closeButt;

@property (strong, nonatomic) IBOutlet UIView *backGround;

@property(nonatomic,strong)UIImageView *bg;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrolWidth;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrolWidth_2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *publishheigh;




@end

@implementation postMessageFirstCatViewController

//JAnalytics
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [JANALYTICSService startLogPageView:@"免费发布 - 发布详情"];
    [MobClick beginLogPageView:@"免费发布 - 发布详情"];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [JANALYTICSService stopLogPageView:@"免费发布 - 发布详情"];
    [MobClick endLogPageView:@"免费发布 - 发布详情"];
    
}

//scrollDelegate
- (void)updateViewConstraints {
    [super updateViewConstraints];
    //设置为两个屏幕的宽度

    //利用sizeWithAttributes:计算
    CGSize deSize = [self.textViews sizeThatFits:CGSizeMake(self.textViews.bounds.size.width,CGFLOAT_MAX)];
    NSLog(@"%lf-------%lf",deSize.height,deSize.height+self.textViews.frame.origin.y);
    self.publishheigh.constant =deSize.height+20;
    

    
    
    if ([self.modelSB.publish_image componentsSeparatedByString:@","].count ==0) {
        self.scrolWidth_2.constant =  self.imageView_1.frame.origin.y-ABS(110-deSize.height) ;

    }
    if ([self.modelSB.publish_image componentsSeparatedByString:@","].count ==1) {
//        self.scrolWidth_2.constant =  self.imageView_1.frame.origin.y +self.imageView_1.bounds.size.height-ABS(110-deSize.height);
             self.scrolWidth_2.constant =self.publishheigh.constant +self.textViews.frame.origin.y + 1*30 +1*(self.imageView_1.bounds.size.height)+10;
    }
    if ([self.modelSB.publish_image componentsSeparatedByString:@","].count ==2) {
//        self.scrolWidth_2.constant =  self.imageView_2.frame.origin.y +self.imageView_2.bounds.size.height-ABS(110-deSize.height);
             self.scrolWidth_2.constant =self.publishheigh.constant +self.textViews.frame.origin.y + 2*30 +2*(self.imageView_1.bounds.size.height)+10;
    }
    if ([self.modelSB.publish_image componentsSeparatedByString:@","].count ==3) {
//        self.scrolWidth_2.constant =  self.imageView_3.frame.origin.y +self.imageView_3.bounds.size.height-ABS(110-deSize.height);
             self.scrolWidth_2.constant =self.publishheigh.constant +self.textViews.frame.origin.y + 3*30 +3*(self.imageView_1.bounds.size.height)+10;
    }
    if ([self.modelSB.publish_image componentsSeparatedByString:@","].count ==4) {
//        self.scrolWidth_2.constant =  self.imageView_4.frame.origin.y +self.imageView_4.bounds.size.height-ABS(110-deSize.height);
        self.scrolWidth_2.constant =self.publishheigh.constant +self.textViews.frame.origin.y + 4*30 +4*(self.imageView_1.bounds.size.height)+10;
    }
    

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.navigationController.toolbarHidden=YES;
    self.closeButt.hidden=YES;
    self.phoneView.hidden=YES;
    self.scroll.bounces=NO;
            self.titleLab.text =[EnercryTool decryptUseDES:self.modelSB.publishName key:DESKEY];
            self.textViews.text =[EnercryTool decryptUseDES:self.modelSB.publish_content key:DESKEY];
            self.addressLab.text =self.modelSB.publish_address;
            self.timeLab.text =self.modelSB.timeString;
            self.craftLab.text =self.modelSB.publish_parentName;
            self.childCraftLab.text =self.modelSB.publish_catalogName;
     self.textViews.font = [UIFont systemFontOfSize:19];
           __weak NSArray *arrayNew = [self.modelSB.publish_image componentsSeparatedByString:@","];
                NSLog(@"new array = %@", arrayNew);
                [self.imageView_1 sd_setImageWithURL:[NSURL URLWithString:arrayNew[0]]];
    NSLog(@"arraYID:%lu",(unsigned long)arrayNew.count);
//    __weak __typeof(self)weakSelf = self;
 __weak __block NSInteger inter =   arrayNew.count;
  
    
    if (inter > 3) {
        [self.imageView_4 sd_setImageWithURL:[NSURL URLWithString:arrayNew[3]]];
    }else if (inter > 2) {
        [self.imageView_3 sd_setImageWithURL:[NSURL URLWithString:arrayNew[2]]];
    }else if (inter > 1) {
        [self.imageView_2 sd_setImageWithURL:[NSURL URLWithString:arrayNew[1]]];
    }else if (inter > 0) {
        [self.imageView_1 sd_setImageWithURL:[NSURL URLWithString:arrayNew[0]]];
    }


 
  [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];

    
    self.textViews.userInteractionEnabled =NO;
}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
    [[SDWebImageManager sharedManager] cancelAll];
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
}
- (IBAction)showTelmessgae:(id)sender {
    self.closeButt.hidden=YES;
    self.phoneView.hidden=YES;
    self.nameLabel.text =self.modelSB.publish_name;
    self.addressLabel_tel.text =self.modelSB.publish_address;
    [self.editIphone setTitle:self.modelSB.publish_telphone forState:UIControlStateNormal];

    self.editIphone.layer.masksToBounds=YES;
    self.editIphone.layer.cornerRadius =10;
    self.phoneView.layer.masksToBounds=YES;
    self.phoneView.layer.cornerRadius =10;
    self.bg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.bg.contentMode = UIViewContentModeScaleAspectFill;
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
  UIImage *bg  = [self viewSnapshot:self.view withInRect:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+rectNav.size.height+rectStatus.size.height, self.view.bounds.size.width, self.view.bounds.size.height )];
    [self.bg setImageToBlur:bg blurRadius:2 completionBlock:nil];
  
    [self.behindView addSubview:self.bg];
    self.closeButt.hidden=NO;
    self.phoneView.hidden=NO;
    [self.behindView bringSubviewToFront:self.phoneView];
      [self.behindView bringSubviewToFront:self.closeButt];
    




}
        
- (IBAction)calliphone:(id)sender {
    
    
    
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",self.modelSB.publish_telphone]]];
    
    
}




- (IBAction)closeButt:(id)sender {
       self.behindView.backgroundColor=[UIColor whiteColor];
    self.closeButt.hidden=YES;
    self.phoneView.hidden=YES;
    self.textViews.backgroundColor =[UIColor clearColor];
    self.imageView_1.backgroundColor =[UIColor clearColor];
//    [self.behindView willRemoveSubview:self.bg];
    [self.bg removeFromSuperview];
    
}
- (UIImage *)viewSnapshot:(UIView *)view withInRect:(CGRect)rect;
{
    //UIGraphicsBeginImageContext(view.bounds.size);
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:view.bounds.size];
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage,rect);
    image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return image;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.behindView;
}
//举报
- (IBAction)jubao:(id)sender {
    UIStoryboard *sb  =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    VoucherRecordViewController *tc=[sb instantiateViewControllerWithIdentifier:@"feedbackViewController"];
    [self.navigationController pushViewController:tc animated:YES];
}
@end
