//
//  NvFeedbackViewController.m
//  SDKDemo
//
//  Created by ms20180425 on 2018/11/19.
 //
//

#import "NvFeedbackViewController.h"
#import "NvsStreamingContext.h"
#import "NvTipsView.h"
#import <sys/utsname.h>
#import "NvHttpRequest.h"

@interface NvFeedbackViewController ()<UITextViewDelegate,UITextFieldDelegate,NvHttpRequestDelegate>

@property (nonatomic, strong) UILabel *placeholderLabel;
@property (nonatomic, strong) UITextView *contentTextview;
@property (nonatomic, strong) UITextField *contactTextfield;
@property (nonatomic, strong) UILabel *numberLabel;

@end

@implementation NvFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"反馈";
    
    [self addSubViews];
    // Do any additional setup after loading the view.
}

#pragma mark 添加子视图
- (void)addSubViews{
    UILabel *feedbackLabel = [[UILabel alloc]init];
    feedbackLabel.text = @"反馈内容";
    feedbackLabel.textColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    feedbackLabel.font = [NvUtils fontWithSize:12];
    [self.view addSubview:feedbackLabel];
    [feedbackLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(13 * SCREANSCALE);
        make.top.equalTo(self.view.mas_top).offset(20 * SCREANSCALE);
    }];
    
    self.contentTextview = [[UITextView alloc]init];
    self.contentTextview.backgroundColor = [UIColor nv_colorWithHexRGB:@"#D8D8D8"];
    self.contentTextview.delegate = self;
    self.contentTextview.font = [NvUtils fontWithSize:12];
    self.contentTextview.textColor = [UIColor nv_colorWithHexARGB:@"#CC000000"];
    [self.view addSubview:self.contentTextview];
    [self.contentTextview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(44 * SCREANSCALE);
        make.left.equalTo(self.view.mas_left).offset(15 * SCREANSCALE);
        make.right.equalTo(self.view.mas_right).offset(-15 * SCREANSCALE);
        make.height.offset(124 * SCREANSCALE);
        
    }];
    
    self.placeholderLabel = [[UILabel alloc]init];
    self.placeholderLabel.text = @"请描述您想反馈的问题内容";
    self.placeholderLabel.textColor = [UIColor nv_colorWithHexRGB:@"#A8A2A2"];
    self.placeholderLabel.font = [NvUtils fontWithSize:12];
    [self.contentTextview addSubview:self.placeholderLabel];
    [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentTextview).offset(8 * SCREANSCALE);
        make.left.equalTo(self.contentTextview).offset(5 * SCREANSCALE);
    }];
    
    UIView *numberView = [[UIView alloc]init];
    numberView.backgroundColor = [UIColor nv_colorWithHexRGB:@"#D8D8D8"];
    [self.view addSubview:numberView];
    [numberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentTextview.mas_bottom);
        make.centerX.equalTo(self.contentTextview.mas_centerX);
        make.width.equalTo(self.contentTextview.mas_width);
        make.height.offset(30 * SCREANSCALE);
    }];
    
    self.numberLabel = [[UILabel alloc]init];
    self.numberLabel.textAlignment = NSTextAlignmentRight;
    self.numberLabel.font = [NvUtils fontWithSize:12];
    self.numberLabel.textColor = [UIColor nv_colorWithHexRGB:@"#A8A2A2"];
    self.numberLabel.text = @"0/1000";
    [numberView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(numberView.mas_centerY);
        make.right.equalTo(numberView.mas_right).offset(-5 * SCREANSCALE);
    }];
    
    UILabel *feedbackLabel1 = [[UILabel alloc]init];
    feedbackLabel1.text = @"联系方式（手机，微信号，QQ号）";
    feedbackLabel1.textColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    feedbackLabel1.font = [NvUtils fontWithSize:12];
    [self.view addSubview:feedbackLabel1];
    [feedbackLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(feedbackLabel.mas_leading);
        make.top.equalTo(numberView.mas_bottom).offset(20 * SCREANSCALE);
    }];
    
    self.contactTextfield = [[UITextField alloc]init];
    self.contactTextfield.backgroundColor = [UIColor nv_colorWithHexRGB:@"#D8D8D8@"];
    self.contactTextfield.placeholder = @"  留下能帮助我们更好的解决问题哦～";
    self.contactTextfield.font = [NvUtils fontWithSize:12];
    self.contactTextfield.textColor = [UIColor nv_colorWithHexARGB:@"#CC000000"];
    [self.view addSubview:self.contactTextfield];
    [self.contactTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(feedbackLabel.mas_leading);
        make.top.equalTo(feedbackLabel1.mas_bottom).offset(10 * SCREANSCALE);
        make.width.offset(346 * SCREANSCALE);
        make.height.offset(25 * SCREANSCALE);
    }];
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    submitBtn.backgroundColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"];
    submitBtn.layer.cornerRadius = 3;
    submitBtn.titleLabel.font = [NvUtils regularFontWithSize:12];
    [submitBtn setTitle:@"提交反馈" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"] forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contactTextfield.mas_bottom).offset(52 * SCREANSCALE);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.offset(150 * SCREANSCALE);
        make.height.offset(40 * SCREANSCALE);
    }];
    
    UILabel *bottomLabel = [[UILabel alloc]init];
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    bottomLabel.text = @"商务合作\nTel:\nMail:";
    bottomLabel.textColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    bottomLabel.numberOfLines = 0;
    bottomLabel.font = [NvUtils fontWithSize:12];
    [self.view addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-25 * SCREANSCALE);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.contactTextfield resignFirstResponder];
    [self.contentTextview resignFirstResponder];
}

/**
 需要改动子类需要重写这个方法
 @return 需要显示的返回按钮
 */
- (UIView *)leftNavigationBarItemView {
    UIButton *backButton;
    backButton = [UIButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"icon_back"]];
    backButton.frame = CGRectMake(0, 0, 30, 44);
    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -15*SCREANSCALE, 0, 0);
    
    __weak typeof(self)weakSelf = self;
    [backButton nv_BtnClickHandler:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    return backButton;
}

#pragma mark submitBtnClick——提交按钮点击
- (void)submitBtnClick:(UIButton *)sender{
    if (self.contentTextview.text.length == 0) {
        [self addTipsViewString:@"反馈内容不能为空"];
        return;
    }
    if (self.contactTextfield.text.length == 0) {
        [self addTipsViewString:@"请填写您的联系方式"];
        return;
    }
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *model = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
    int large,minor,revision;
    [NvsStreamingContext getSdkVersion:&large minorVersion:&minor revisionNumber:&revision];
    
    [[NvHttpRequest sharedInstance] feedBackWithContent:self.contentTextview.text withContact:self.contactTextfield.text withSdkVersion:[NSString stringWithFormat:@"%d.%d.%d",large,minor,revision] withDeviceModel:[self currentModel:model] withDelegate:self];
    //        NSLog(@"输入内容=%@,联系方式=%@,手机类型=%@,sdk版本=%@",self.contentTextview.text,self.contactTextfield.text,[self currentModel:model],[NSString stringWithFormat:@"%d.%d.%d",large,minor,revision]);
}

#pragma mark NvHttpRequestDelegate
- (void)feedBackWithDictionary:(NSDictionary *)dic{
    if ([dic[@"errNo"] integerValue] == 0) {
        [self addTipsViewString:@"提交成功"];
    }else{
        [self addTipsViewString:@"网络异常"];
    }
}

#pragma mark UITextFieldDelegate
//return NO键盘不弹出
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    return YES;
}

//当输入框开始时触发 ( 获得焦点触发)
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

#pragma mark 提示视图
- (void)addTipsViewString:(NSString *)text{
    NvTipsView *tip = [[NvTipsView alloc]initWithFrame:self.view.frame withTitle:text withColor:[UIColor nv_colorWithHexRGB:@"#4D4F51"] withCenter:YES];
    [self.view addSubview:tip];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [tip removeFromSuperview];
    });
}

//当输入框文字发生变化时触发 ( 只有通过键盘输入时 , 文字改变 , 触发 )

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@"%@,%@",textField.text,string);
    
    return YES;
}

//询问输入框是否可以结束编辑 ( 键盘是否可以收回)
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    return YES;
}

//当前输入框结束编辑时触发 ( 键盘收回之后触发 )
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    
}

//控制键盘是否回收

- (BOOL)textFieldShouldReturn:(UITextField*)textField{
    
    return YES;
}

#pragma mark UITextViewDelegate
//在text view获得焦点之前会调用textViewShouldBeginEditing: 方法。
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    NSLog(@"获得焦点");
    if (textView.text.length == 0) {
        self.placeholderLabel.hidden = NO;
    }else{
        self.placeholderLabel.hidden = YES;
    }
//    [_titleField resignFirstResponder];
//    [_discriptionTextView resignFirstResponder];
    return YES;
}

//当text view失去焦点之前会调用textViewShouldEndEditing
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    NSLog(@"失去焦点");
    if (textView.text.length == 0) {
        self.placeholderLabel.hidden = NO;
    }else{
        self.placeholderLabel.hidden = YES;
    }
    return YES;
}

//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView{
    NSLog(@"结束编辑");
}

//内容将要发生改变编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSLog(@"内容将要发生改变编辑");
    
    return YES;
}

//内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        self.placeholderLabel.hidden = NO;
        self.numberLabel.text = @"0/1000";
    }else{
        self.placeholderLabel.hidden = YES;
        UITextRange *range = textView.markedTextRange;
        NSUInteger length = [self getToInt:textView.text] / 2;
        if (!range) {
            if (length > 1000) {
                [NvToast showInfoWithMessage:@"描述最多输入1000个字"];
            }else{
                self.numberLabel.text = [NSString stringWithFormat:@"%ld/1000",[self getToInt:textView.text] / 2];
            }
        }
    }
    NSLog(@"内容发生改变编辑");
}

- (NSInteger)getToInt:(NSString*)strtemp {
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData* da = [strtemp dataUsingEncoding:enc];
    return [da length];
}

- (NSString *)currentModel:(NSString *)phoneModel {
    
    if ([phoneModel isEqualToString:@"iPhone3,1"] ||
        [phoneModel isEqualToString:@"iPhone3,2"])   return @"iPhone 4";
    if ([phoneModel isEqualToString:@"iPhone4,1"])   return @"iPhone 4S";
    if ([phoneModel isEqualToString:@"iPhone5,1"] ||
        [phoneModel isEqualToString:@"iPhone5,2"])   return @"iPhone 5";
    if ([phoneModel isEqualToString:@"iPhone5,3"] ||
        [phoneModel isEqualToString:@"iPhone5,4"])   return @"iPhone 5C";
    if ([phoneModel isEqualToString:@"iPhone6,1"] ||
        [phoneModel isEqualToString:@"iPhone6,2"])   return @"iPhone 5S";
    if ([phoneModel isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if ([phoneModel isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if ([phoneModel isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    if ([phoneModel isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    if ([phoneModel isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    if ([phoneModel isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    if ([phoneModel isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    if ([phoneModel isEqualToString:@"iPhone10,1"] ||
        [phoneModel isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    if ([phoneModel isEqualToString:@"iPhone10,2"] ||
        [phoneModel isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
    if ([phoneModel isEqualToString:@"iPhone10,3"] ||
        [phoneModel isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    
    if ([phoneModel isEqualToString:@"iPad1,1"]) return @"iPad";
    if ([phoneModel isEqualToString:@"iPad2,1"] ||
        [phoneModel isEqualToString:@"iPad2,2"] ||
        [phoneModel isEqualToString:@"iPad2,3"] ||
        [phoneModel isEqualToString:@"iPad2,4"]) return @"iPad 2";
    if ([phoneModel isEqualToString:@"iPad3,1"] ||
        [phoneModel isEqualToString:@"iPad3,2"] ||
        [phoneModel isEqualToString:@"iPad3,3"]) return @"iPad 3";
    if ([phoneModel isEqualToString:@"iPad3,4"] ||
        [phoneModel isEqualToString:@"iPad3,5"] ||
        [phoneModel isEqualToString:@"iPad3,6"]) return @"iPad 4";
    if ([phoneModel isEqualToString:@"iPad4,1"] ||
        [phoneModel isEqualToString:@"iPad4,2"] ||
        [phoneModel isEqualToString:@"iPad4,3"]) return @"iPad Air";
    if ([phoneModel isEqualToString:@"iPad5,3"] ||
        [phoneModel isEqualToString:@"iPad5,4"]) return @"iPad Air 2";
    if ([phoneModel isEqualToString:@"iPad6,3"] ||
        [phoneModel isEqualToString:@"iPad6,4"]) return @"iPad Pro 9.7-inch";
    if ([phoneModel isEqualToString:@"iPad6,7"] ||
        [phoneModel isEqualToString:@"iPad6,8"]) return @"iPad Pro 12.9-inch";
    if ([phoneModel isEqualToString:@"iPad6,11"] ||
        [phoneModel isEqualToString:@"iPad6,12"]) return @"iPad 5";
    if ([phoneModel isEqualToString:@"iPad7,1"] ||
        [phoneModel isEqualToString:@"iPad7,2"]) return @"iPad Pro 12.9-inch 2";
    if ([phoneModel isEqualToString:@"iPad7,3"] ||
        [phoneModel isEqualToString:@"iPad7,4"]) return @"iPad Pro 10.5-inch";
    
    if ([phoneModel isEqualToString:@"iPad2,5"] ||
        [phoneModel isEqualToString:@"iPad2,6"] ||
        [phoneModel isEqualToString:@"iPad2,7"]) return @"iPad mini";
    if ([phoneModel isEqualToString:@"iPad4,4"] ||
        [phoneModel isEqualToString:@"iPad4,5"] ||
        [phoneModel isEqualToString:@"iPad4,6"]) return @"iPad mini 2";
    if ([phoneModel isEqualToString:@"iPad4,7"] ||
        [phoneModel isEqualToString:@"iPad4,8"] ||
        [phoneModel isEqualToString:@"iPad4,9"]) return @"iPad mini 3";
    if ([phoneModel isEqualToString:@"iPad5,1"] ||
        [phoneModel isEqualToString:@"iPad5,2"]) return @"iPad mini 4";
    
    if ([phoneModel isEqualToString:@"iPod1,1"]) return @"iTouch";
    if ([phoneModel isEqualToString:@"iPod2,1"]) return @"iTouch2";
    if ([phoneModel isEqualToString:@"iPod3,1"]) return @"iTouch3";
    if ([phoneModel isEqualToString:@"iPod4,1"]) return @"iTouch4";
    if ([phoneModel isEqualToString:@"iPod5,1"]) return @"iTouch5";
    if ([phoneModel isEqualToString:@"iPod7,1"]) return @"iTouch6";
    
    if ([phoneModel isEqualToString:@"i386"] || [phoneModel isEqualToString:@"x86_64"]) return @"iPhone Simulator";
    
    return @"Unknown";
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
