import numpy as np
from scipy.spatial import cKDTree
from utils.pts_util import PtsUtil

class ReconstructionUtil:
    
    @staticmethod
    def compute_coverage_rate(target_point_cloud, combined_point_cloud, threshold=0.01):
        kdtree = cKDTree(combined_point_cloud)
        distances, _ = kdtree.query(target_point_cloud)
        covered_points_num = np.sum(distances < threshold*2)
        coverage_rate = covered_points_num / target_point_cloud.shape[0]
        return coverage_rate, covered_points_num
    
    @staticmethod
    def check_overlap(new_point_cloud, combined_point_cloud, overlap_area_threshold=25, voxel_size=0.01):
        kdtree = cKDTree(combined_point_cloud)
        distances, _ = kdtree.query(new_point_cloud)
        overlapping_points = np.sum(distances < voxel_size*2)
        cm = 0.01
        voxel_size_cm = voxel_size / cm
        overlap_area = overlapping_points * voxel_size_cm * voxel_size_cm
        return overlap_area > overlap_area_threshold


    @staticmethod
    def get_new_added_points(old_combined_pts, new_pts, threshold=0.005):
        if old_combined_pts.size == 0:
            return new_pts
        if new_pts.size == 0:
            return np.array([])

        tree = cKDTree(old_combined_pts)
        distances, _ = tree.query(new_pts, k=1)
        new_added_points = new_pts[distances > threshold]
        return new_added_points
    
    @staticmethod
    def compute_next_best_view_sequence_with_overlap(target_point_cloud, point_cloud_list, scan_points_indices_list, threshold=0.01, soft_overlap_threshold=0.5, hard_overlap_threshold=0.7, init_view = 0, scan_points_threshold=5, status_info=None):
        selected_views = [init_view]
        combined_point_cloud = point_cloud_list[init_view]
        history_indices = [scan_points_indices_list[init_view]]
        
        max_rec_pts = np.vstack(point_cloud_list)
        downsampled_max_rec_pts = PtsUtil.voxel_downsample_point_cloud(max_rec_pts, threshold)
        
        max_rec_pts_num = downsampled_max_rec_pts.shape[0]
        max_real_rec_pts_coverage, _ = ReconstructionUtil.compute_coverage_rate(target_point_cloud, downsampled_max_rec_pts, threshold)
        
        new_coverage, new_covered_num = ReconstructionUtil.compute_coverage_rate(downsampled_max_rec_pts, combined_point_cloud, threshold)
        current_coverage = new_coverage
        current_covered_num = new_covered_num
        
        remaining_views = list(range(len(point_cloud_list)))
        view_sequence = [(init_view, current_coverage)]
        cnt_processed_view = 0
        remaining_views.remove(init_view)
        curr_rec_pts_num = combined_point_cloud.shape[0]
        
        while remaining_views:
            best_view = None
            best_coverage_increase = -1
            best_combined_point_cloud = None
            best_covered_num = 0
        
            for view_index in remaining_views:
                if point_cloud_list[view_index].shape[0] == 0:
                    continue
                if selected_views:
                    new_scan_points_indices = scan_points_indices_list[view_index]
                    if not ReconstructionUtil.check_scan_points_overlap(history_indices, new_scan_points_indices, scan_points_threshold):
                        overlap_threshold = hard_overlap_threshold
                    else:
                        overlap_threshold = soft_overlap_threshold
                    overlap_rate = ReconstructionUtil.compute_overlap_rate(point_cloud_list[view_index],combined_point_cloud, threshold)
                    if overlap_rate < overlap_threshold:
                        continue
                new_combined_point_cloud = np.vstack([combined_point_cloud, point_cloud_list[view_index]])
                new_downsampled_combined_point_cloud = PtsUtil.voxel_downsample_point_cloud(new_combined_point_cloud,threshold)
                new_coverage, new_covered_num = ReconstructionUtil.compute_coverage_rate(downsampled_max_rec_pts, new_downsampled_combined_point_cloud, threshold)

                coverage_increase = new_coverage - current_coverage
                if coverage_increase > best_coverage_increase:
                    best_coverage_increase = coverage_increase
                    best_view = view_index
                    best_covered_num = new_covered_num
                    best_combined_point_cloud = new_downsampled_combined_point_cloud
                    
            
            if best_view is not None:
                if best_coverage_increase <=1e-3 or best_covered_num - current_covered_num <= 5:
                    break
                
                selected_views.append(best_view)
                best_rec_pts_num = best_combined_point_cloud.shape[0]
                print(f"Current rec pts num: {curr_rec_pts_num}, Best rec pts num: {best_rec_pts_num}, Best cover pts: {best_covered_num}, Max rec pts num: {max_rec_pts_num}")
                print(f"Current coverage: {current_coverage}, Best coverage increase: {best_coverage_increase}, Max Real coverage: {max_real_rec_pts_coverage}")
                current_covered_num = best_covered_num
                curr_rec_pts_num = best_rec_pts_num
                combined_point_cloud = best_combined_point_cloud
                remaining_views.remove(best_view)
                history_indices.append(scan_points_indices_list[best_view])
                current_coverage += best_coverage_increase
                cnt_processed_view += 1
                if status_info is not None:
                    sm = status_info["status_manager"]
                    app_name = status_info["app_name"]
                    runner_name = status_info["runner_name"]
                    sm.set_status(app_name, runner_name, "current coverage", current_coverage)
                    sm.set_progress(app_name, runner_name, "processed view", cnt_processed_view, len(point_cloud_list))
            
                view_sequence.append((best_view, current_coverage))
                
            else:
                break
        if status_info is not None:
            sm = status_info["status_manager"]
            app_name = status_info["app_name"]
            runner_name = status_info["runner_name"]
            sm.set_progress(app_name, runner_name, "processed view", len(point_cloud_list), len(point_cloud_list))
        return view_sequence, remaining_views, combined_point_cloud

    @staticmethod
    def compute_next_best_view_with_overlap(scanned_pts, point_cloud_list, selected_view, history_indices, scan_points_indices_list, threshold=0.01, overlap_area_threshold=25, scan_points_threshold=5):
        max_rec_pts = np.vstack(point_cloud_list)
        downsampled_max_rec_pts = PtsUtil.voxel_downsample_point_cloud(max_rec_pts, threshold)
        best_view = None
        new_downsampled_combined_point_cloud = PtsUtil.voxel_downsample_point_cloud(scanned_pts,threshold)
        best_coverage,_ = ReconstructionUtil.compute_coverage_rate(downsampled_max_rec_pts, new_downsampled_combined_point_cloud, threshold)
        best_covered_num = new_downsampled_combined_point_cloud.shape[0]
        #import ipdb; ipdb.set_trace()
        for view in range(len(point_cloud_list)):
            print(f"Processing view {view}/{len(point_cloud_list)}: {best_coverage},{best_covered_num}")
            if point_cloud_list[view].shape[0] == 0:
                continue
            if view in selected_view:
                continue
            
            new_scan_points_indices = scan_points_indices_list[view]
            
            # if not ReconstructionUtil.check_scan_points_overlap(history_indices, new_scan_points_indices, scan_points_threshold):
            #     curr_overlap_area_threshold = overlap_area_threshold
            # else:
            #     curr_overlap_area_threshold = overlap_area_threshold * 0.8
                
            # if not ReconstructionUtil.check_overlap(point_cloud_list[view], scanned_pts, overlap_area_threshold = curr_overlap_area_threshold, voxel_size=threshold):
            #     continue
            
            
            new_combined_point_cloud = np.vstack([scanned_pts ,point_cloud_list[view]])
            new_downsampled_combined_point_cloud = PtsUtil.voxel_downsample_point_cloud(new_combined_point_cloud,threshold)
           
            new_coverage, _ = ReconstructionUtil.compute_coverage_rate(downsampled_max_rec_pts, new_downsampled_combined_point_cloud, threshold)
            new_covered_num = new_downsampled_combined_point_cloud.shape[0]
            print("the new coverage_rate: ", new_coverage, " the new covered num: ", new_covered_num, " the fake covered num:" , _)
            if new_coverage > best_coverage :
                #import ipdb; ipdb.set_trace()
                best_coverage = new_coverage
                best_covered_num = new_covered_num
                best_view = view
                
        return best_view, best_coverage, best_covered_num
        
    @staticmethod
    def generate_scan_points(display_table_top, display_table_radius, min_distance=0.03, max_points_num = 500, max_attempts = 1000):
        points = []
        attempts = 0
        while len(points) < max_points_num and attempts < max_attempts:
            angle = np.random.uniform(0, 2 * np.pi)
            r = np.random.uniform(0, display_table_radius)
            x = r * np.cos(angle)
            y = r * np.sin(angle)
            z = display_table_top
            new_point = (x, y, z)
            if all(np.linalg.norm(np.array(new_point) - np.array(existing_point)) >= min_distance for existing_point in points):
                points.append(new_point)
            attempts += 1
        return points
    
    @staticmethod
    def compute_covered_scan_points(scan_points, point_cloud, threshold=0.01):
        
        tree = cKDTree(point_cloud)
        covered_points = []
        indices = []
        for i, scan_point in enumerate(scan_points):
            if tree.query_ball_point(scan_point, threshold):
                covered_points.append(scan_point)
                indices.append(i)
        return covered_points, indices
    
    @staticmethod
    def check_scan_points_overlap(history_indices, indices2, threshold=5):
        try:
            if len(indices2) == 0:
                return False
            for indices1 in history_indices:
                if len(set(indices1).intersection(set(indices2))) >= threshold:
                    return True
        except Exception as e:
            print(e)
            import ipdb; ipdb.set_trace()
        return False

        