import csv
import os
import re
import sqlite3

from qgis.PyQt.QtCore import QThread
from qgis.PyQt.QtWidgets import QFileDialog, QProgressBar
from qgis.core import QgsPoint, QgsWkbTypes
from shapely import wkt

from qgis.PyQt import QtWidgets, uic, QtCore
from qgis.PyQt.QtCore import pyqtSignal

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'cyanlove_export_centroid_geometry_base.ui'))


class cyanlove_export_centroid_geometry(QtWidgets.QDockWidget, FORM_CLASS):
    closingPlugin = pyqtSignal()

    def __init__(self, parent=None):
        super(cyanlove_export_centroid_geometry, self).__init__(parent)
        self.export_thread = None
        self.setupUi(self)
        self.setFloating(True)
        self.setWindowFlags(QtCore.Qt.Dialog)  # 使对话框始终在前
        self.btn_export.clicked.connect(self.export_attribute)

    def closeEvent(self, event):
        self.closingPlugin.emit()
        event.accept()

    def update_label(self, value):
        self.label.setText(value)  # 更新值

    def export_attribute(self, event):

        options = QFileDialog.Options()  # 创建文件对话框选项
        options |= QFileDialog.ReadOnly  # 可以设置一些选项，例如只读
        file_name, _ = QFileDialog.getSaveFileName(self,
                                                   "选择文件",
                                                   "",
                                                   "CSV文件 (*.csv);;所有文件 (*)",  # 可以添加其他过滤器
                                                   options=options)  # 传递 options 参数
        # 检查用户是否选择了文件
        if file_name:

            selected_layer = self.mMapLayerComboBox.currentLayer()
            if selected_layer is None:
                print("未选择有效图层!")
            else:
                print(f"选择的图层: {selected_layer.name()}")
                csv_file_path = file_name
                self.label.setText('开始导出.....')
                # 确保使用现有的进度条
                self.progressBar.setRange(0, 100)  # 设置进度条范围
                self.progressBar.setValue(0)  # 初始化进度条值
                self.progressBar.show()  # 显示进度条 (如果之前是隐藏状态)

                # 启动导出线程
                self.export_thread = ExportThread(selected_layer, csv_file_path)
                self.export_thread.updatelabel.connect(self.update_label)
                self.export_thread.progress.connect(self.update_progress)  # 连接信号到槽
                self.export_thread.start()  # 启动线程


        else:
            print("用户取消了文件选择")  # 处理取消选择的情况

    def update_progress(self, value):
        self.progressBar.setValue(value)  # 更新进度条的值

    def export_finished(self):
        self.progressBar.setValue(100)


class ExportThread(QThread):
    progress = pyqtSignal(int)  # 声明一个信号
    updatelabel = pyqtSignal(str)  # 声明一个更新label新增信号

    def __init__(self, selected_layer, csv_file_path):
        super().__init__()
        self.selected_layer = selected_layer
        self.csv_file_path = csv_file_path

    def run(self):
        # 获取字段名，并添加 WKT_BJ，字段名两边加上双引号
        try:
            # 修改：增加中心经度(图形内)和中心纬度(图形内)两列
            field_names = [f"{field.name()}" for field in self.selected_layer.fields()] + \
                          ['图形内部中心点'] + ['外接矩形中心点'] + ['中心经度(图形内)'] + ['中心纬度(图形内)']

            # 获取要素总数以计算进度
            total_features = self.selected_layer.featureCount()

            with open(self.csv_file_path, mode='w', newline='', encoding='gbk') as csv_file:
                writer = csv.writer(csv_file)
                writer.writerow(field_names)

                for index, feature in enumerate(self.selected_layer.getFeatures()):
                    geom = feature.geometry()
                    row = []
                    for field_name in field_names[:-4]:  # 不包括新增的4列
                        value = feature[field_name]  # 获取每个字段的值
                        row.append(f'{value}')  # 在每个值两边添加双引号

                    # 图形内部中心点
                    centroid = geom.pointOnSurface()
                    centroid_wkt = centroid.asWkt()
                    row.append(centroid_wkt)

                    # 获取图形内部中心点的经纬度坐标
                    centroid_point = centroid.asPoint()
                    centroid_lon = centroid_point.x()  # 经度
                    centroid_lat = centroid_point.y()  # 纬度

                    # 质心（外接矩形中心点）
                    bbox = geom.boundingBox()
                    center_point = QgsPoint((bbox.xMinimum() + bbox.xMaximum()) / 2,
                                            (bbox.yMinimum() + bbox.yMaximum()) / 2)
                    row.append(center_point.asWkt())

                    # 添加中心经度(图形内)和中心纬度(图形内)
                    row.append(str(centroid_lon))  # 经度
                    row.append(str(centroid_lat))  # 纬度

                    writer.writerow(row)

                    # 发送进度更新
                    progress_value = int((index + 1) / total_features * 100)
                    self.progress.emit(progress_value)

            self.updatelabel.emit(f"导出完毕！")
        except Exception as e:
            self.updatelabel.emit(f"处理要素 {index} 时出错: {str(e)}")
            print(f"处理要素 {index} 时出错: {str(e)}")