# -*- coding: utf-8 -*-

__author__ = 'TUMCIVIL'
__version__ = '2.0.0'
__title__ = 'เซ็ตระดับหลังผนัง\nเท่าหลังผนัง'
__doc__ = """Created by : TUMCIVIL TEAM
Version : 2.0.0
Update : วันที่ 03-03-2025
_________________________________________________________________
รายการ :
เซ็ตระดับหลังผนังให้เท่าหลังผนังแต่ระดับท้องผนังคงเดิม
_________________________________________________________________
ข้อจำกัดการใช้งาน :
1.ไม่สามารถใช้กับผนังที่ Slope ได้
2.ไม่สามารถใช้กับผนังที่สร้างจาก Model In-Place ได้
3.ระดับท้องผนังที่ต้องการเซ็ตต้องต่ำกว่าระดับหลังผนังหลัก
_________________________________________________________________
วิธีการใช้ :
1. เลือกผนังหลัก
2. เลือกผนังที่ต้องการเช็ตระดับได้ไม่มีที่สิ้นสุด
3. ต้องการออกกด 'Esc'
 """

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> IMPORTS
from pyrevit import forms, revit
from Autodesk.Revit.DB import (BuiltInParameter)

# >>>>>>>>>> CUSTOM IMPORTS
from Snippets._convert_units import convert_internal_units
from Snippets._level import levels,get_levels_walls
from Snippets._level import *
from Snippets._selection import pick_wall

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> VARIABLES
doc   = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app   = __revit__.Application

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MAIN
if __name__ == '__main__':
    levels_model = levels()

    #>>>>>>>>>> SELECT MAIN WALL
    try:    Selected_wall = pick_wall(uidoc)
    except: forms.alert("Script is canceled.",exitscript=True,title="Script Canceled.")

    Main_levels_wall = get_levels_walls(levels_model,Selected_wall)
    Main_Top_elevation = Main_levels_wall[1]
    Main_Bottom_elevation = Main_levels_wall[0]
    Main_wall_location = Selected_wall.Location
    Main_wall_curve = Main_wall_location.Curve

    #>>>>>>>>>> TOP
    Main_Top_Constraint     = Selected_wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE)
    Main_Top_Constraint_Id  = Selected_wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).AsElementId()
    Main_is_unconnected     = str(Main_Top_Constraint_Id) == "-1"  # True = Unconnected / False = Up to Level
    # print("Main_is_unconnected = {}".format(Main_is_unconnected))

    #>>>>>>>>>> BASE
    Main_Base_Containt  = Selected_wall.get_Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).AsElementId()
    Main_Base_Offset    = Selected_wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).AsDouble()

    #>>>>>>>>>> LOOP: SELECT MATCH WALL
    with forms.WarningBar(title="Pick Wall to match base constrains:", handle_esc=True):
        while True:

            #>>>>>>>>>> PICK WALL
            try:    Match_wall = pick_wall(uidoc)
            except: break
            if not Match_wall: break

            Match_levels_wall = get_levels_walls(levels_model,Match_wall)
            Match_Top_elevation = Match_levels_wall[1]
            Match_Bottom_elevation = Match_levels_wall[0]
            wall_location = Match_wall.Location
            wall_curve = wall_location.Curve

            #>>>>>>>>>> SET PARAMETERS
            if Match_Bottom_elevation < Main_Top_elevation:
                with revit.Transaction("Wall"):

                    Match_wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).Set(Main_Top_Constraint_Id)

                    #>>>>>>>>>> IF TOP CONSTRAINT: UNCONNECTED
                    if Main_is_unconnected:

                        #>>>>>>>>>> MAIN WALL PARAMETERS
                        main_unconnected_height = Selected_wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble()
                        if isinstance(Main_wall_curve, Line):
                            main_Z_Top = (Selected_wall.Location.Curve.Origin.Z) + main_unconnected_height + Main_Base_Offset
                        elif isinstance(Main_wall_curve, Arc):
                            main_Z_Top = (Selected_wall.Location.Curve.Center.Z) + main_unconnected_height + Main_Base_Offset

                        #>>>>>>>>>> MATCH WALL PARAMETERS
                        match_base_offset        = Match_wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).AsDouble()
                        match_unconnected_height = Match_wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble()
                        if isinstance(wall_curve, Line):
                            match_Z_Top = (Match_wall.Location.Curve.Origin.Z + match_base_offset) + match_unconnected_height
                        elif isinstance(wall_curve, Arc):
                            match_Z_Top = (Match_wall.Location.Curve.Center.Z + match_base_offset) + match_unconnected_height

                        #>>>>>>>>>> NEW UNCONNECTED HEIGHT
                        new_unconnected_height = match_unconnected_height + main_Z_Top - match_Z_Top
                        Match_wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).Set(new_unconnected_height)

                    #>>>>>>>>>> IF TOP CONSTRAINT: LEVEL
                    else:
                        Main_Top_Offset = Selected_wall.get_Parameter(BuiltInParameter.WALL_TOP_OFFSET).AsDouble()
                        # print("Main_Top_Offset = {}".format(Main_Top_Offset))
                        Match_wall.get_Parameter(BuiltInParameter.WALL_TOP_OFFSET).Set(Main_Top_Offset)
            else:
                forms.alert("ระดับท้องผนังที่ต้องการเซ็ต\nต้องต่ำกว่าระดับหลังผนังหลัก",exitscript=False,title="Script Canceled.")
                pass