//
//  CloudPhoneViewController+RBDConnect.m
//  VclustersGemini
//
//  Created by APPLE on 2021/8/16.
//  Copyright © 2021 APPLE. All rights reserved.
//

#import "PlayerViewController+AdjustBtnFrame.h"
#import <objc/runtime.h>
#import "UIView+View.h"

@interface PlayerViewController(AdjustBtnFrame)

@end

@implementation PlayerViewController(AdjustBtnFrame)
@dynamic adjustTime;

- (void)setAdjustTime:(NSInteger)adjustTime {
    objc_setAssociatedObject(self, @selector(adjustTime), @(adjustTime), OBJC_ASSOCIATION_ASSIGN);
}

- (NSInteger)adjustTime {
    NSNumber *adjustTimeNumber =  objc_getAssociatedObject(self, @selector(adjustTime));
    
    return [adjustTimeNumber integerValue];
}

#pragma mark 悬浮球3s后自动靠边隐藏
//（1、3s之后 控制按钮靠边隐藏2/3、降低透明度1.0 2、点击之后 自动弹出控制按钮 透明度改为1.0）
- (void)playerViewControlBtnTouchBegan {
//    HLog(@"云手机推流界面 控制按钮手势开始");
    [self adjustPlayerViewControlBtnFrame2];
}

- (void)playerViewControlBtnTouchEnd {
//    HLog(@"云手机推流界面 控制按钮手势结束");
    self.adjustTime = 3;
}

// 控制按钮3s后 自动靠边算法
- (void)adjustPlayerViewControlBtnFrame1 {
    
    CGFloat margin = 60/2.0;
    CGFloat W_H = 60;
    mPlayerView.controlBtn.alpha = 0.5;
    
    CGPoint centerPoint = mPlayerView.controlBtn.center;
//    HLog(@"云手机推流界面 控制按钮中点%@",NSStringFromCGPoint(centerPoint));
    
    if (mPlayerView.width > mPlayerView.height){
        CGFloat centerY = mPlayerView.height*0.5;
        CGFloat value1Y =  centerY - centerPoint.y;
        CGFloat adaptValue =  (SCREEN_W - mPlayerView.glkshowImageView.hw_h)*0.5; // 补偿刘海屏上下黑边
        
        if (value1Y >= 0) { // 左边
            HLog(@"云手机推流界面 靠上边");
            mPlayerView.controlBtn.y = - W_H + margin;
        }else { // 右边
            HLog(@"云手机推流界面 靠下边");
            mPlayerView.controlBtn.y = SCREEN_H - margin;
        }
        
        if (mPlayerView.controlBtn.x < margin){
            mPlayerView.controlBtn.x = margin;
        }else if (mPlayerView.controlBtn.x > mPlayerView.width - margin - W_H){
            mPlayerView.controlBtn.x = mPlayerView.width - margin - W_H;
        }
        
    }else{
        CGFloat centerX = SCREEN_W*0.5;
        CGFloat value1X =  centerX - centerPoint.x;
        
        if (value1X >= 0) { // 左边
            HLog(@"云手机推流界面 靠左边");
            mPlayerView.controlBtn.x = - W_H + margin;
        }else { // 右边
            HLog(@"云手机推流界面 靠右边");
            mPlayerView.controlBtn.x = SCREEN_W - margin;
        }
        
        if (mPlayerView.controlBtn.y < margin){
            mPlayerView.controlBtn.y = margin;
        }else if (mPlayerView.controlBtn.y > mPlayerView.height - margin - W_H){
            mPlayerView.controlBtn.y = mPlayerView.height - margin - W_H;
        }
    }


    HLog(@"云手机推流界面 控制按钮中点%@",NSStringFromCGPoint(mPlayerView.controlBtn.center));
}

// 控制按钮点击之后 从半隐藏状态弹出算法
- (void)adjustPlayerViewControlBtnFrame2 {
    
//    CGFloat margin = 10;
//    CGFloat W_H = 60;
//    mPlayerView.controlBtn.alpha = 1.0;
//
//    CGPoint centerPoint = mPlayerView.controlBtn.center;
////    HLog(@"云手机推流界面 控制按钮中点%@",NSStringFromCGPoint(centerPoint));
//
//    CGFloat centerX =  SCREEN_W*0.5;
//    CGFloat centerY =  SCREEN_H*0.5;
//    CGFloat value1X =  centerX  - centerPoint.x;
//    CGFloat value1Y =  centerY  - centerPoint.y;
//    CGFloat value2X =  SCREEN_W - centerPoint.x;
//    CGFloat value2Y =  SCREEN_H - centerPoint.y;
//    CGFloat adaptValue =  (SCREEN_H - mPlayerView.glkshowImageView.hw_h)*0.5; // 补偿刘海屏上下黑边
//    if ([self.deviceModel isOpenVirtualKey]) {
//        adaptValue -= 20;
//    }
//
//    if (value1X >= 0) { // 左边
//
//        if (value1Y >= 0) { // 上边
////            HLog(@"云手机推流界面 左上角");
//            if (fabs(centerPoint.x) > fabs(centerPoint.y - adaptValue)) {
//                HLog(@"云手机推流界面 靠上边");
//                mPlayerView.controlBtn.y = margin + adaptValue;
//            }else {
//                HLog(@"云手机推流界面 靠左边");
//                mPlayerView.controlBtn.x = margin;
//            }
//
//        }else { // 下边
////            HLog(@"云手机推流界面 左下角");
//            if (fabs(centerPoint.x) > fabs(value2Y - adaptValue)) {
//                HLog(@"云手机推流界面 靠下边");
//                mPlayerView.controlBtn.y = SCREEN_H - W_H - margin - adaptValue;
//            }else {
//                HLog(@"云手机推流界面 靠左边");
//                mPlayerView.controlBtn.x = margin;
//            }
//        }
//
//    }else { // 右边
//        if (value1Y >= 0) { // 上边
////            HLog(@"云手机推流界面 右上角");
//            if (fabs(value2X) > fabs(centerPoint.y - adaptValue)) {
//                HLog(@"云手机推流界面 靠上边");
//                mPlayerView.controlBtn.y = margin + adaptValue;
//            }else {
//                HLog(@"云手机推流界面 靠右边");
//                mPlayerView.controlBtn.x = SCREEN_W - W_H - margin;
//            }
//
//        }else { // 下边
////            HLog(@"云手机推流界面 右下角");
//            if (fabs(value2X) > fabs(value2Y - adaptValue)) {
//                HLog(@"云手机推流界面 靠下边");
//                mPlayerView.controlBtn.y = SCREEN_H - W_H - margin - adaptValue;
//            }else {
//                HLog(@"云手机推流界面 靠右边");
//                mPlayerView.controlBtn.x = SCREEN_W - W_H - margin;
//            }
//        }
//    }
//
//    HLog(@"云手机推流界面 控制按钮中点%@",NSStringFromCGPoint(mPlayerView.controlBtn.center));
}

// 1、悬浮球3s后 自动靠边隐藏
- (void)extensionAdjustBtnFrameCheckAdjustTime {
    self.adjustTime--;
    
    if (self.adjustTime == 0) {
//        HLog(@"云手机圆形浮标自动隐藏倒计时 3s倒计时结束");
        [self adjustPlayerViewControlBtnFrame1];
    }else {
//        HLog(@"云手机圆形浮标自动隐藏倒计时：%zd", self.adjustTime);
    }
}

@end
