#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <fstream>
#include <filesystem>
#include "../extract_seal_main/extract_seal_main.h"
#include "remove_border.h"
#include "remove_border_main.h"

using namespace std;
namespace fs = std::filesystem;

void process_seal_remove_border() {
    // TODO: 修改为你的测试图片路径
    // 示例: "c:\\Users\\用户名\\Desktop\\Final project\\image\\1.png" 或使用相对路径 "..\\..\\image\\1.png"
    string img_path = "..\\..\\image\\1.png";

    cout << "Image path: " << img_path << endl;
    
    ifstream file(img_path);
    if (!file.good()) {
        cout << "Error: Image file does not exist!" << endl;
        return;
    }
    file.close();
    
    cv::Mat img = cv::imread(img_path);
    if (img.empty()) {
        cout << "Error: Cannot read image!" << endl;
        return;
    }
    
    cout << "Image loaded: " << img.cols << "x" << img.rows << endl;
    
    vector<vector<double>> det = {
        {408.0, 32.0, 559.0, 186.0, 0.974, 0},
        {267.0, 61.0, 348.0, 143.0, 0.973, 2},
        {54.0, 36.0, 212.0, 147.0, 0.972, 1}
    };

    cout << "Number of detection boxes: " << det.size() << endl;
    
    cout << "Extracting seals..." << endl;
    try {
        vector<SealInfo> seal_set = extract_seal_main(img, det);

        cout << "Total extracted seals: " << seal_set.size() << endl;

        if (seal_set.empty()) {
            cout << "Error: No seals extracted!" << endl;
            return;
        }
        
        for (size_t i = 0; i < seal_set.size(); ++i) {
            cout << "Seal " << i << ":" << endl;
            cout << "  Shape: " << seal_set[i].shape_info.type << endl;
            cout << "  Adjusted image size: " << seal_set[i].adj_img.cols << "x" << seal_set[i].adj_img.rows << endl;
        }

        cout << endl << "Removing borders..." << endl;
        remove_border_main(seal_set, false);

        string output_dir = "..\\remove_border\\test_image";
        fs::create_directories(output_dir);
        
        cout << endl << "========== Results after remove_border ==========" << endl;
        for (size_t i = 0; i < seal_set.size(); ++i) {
            cout << endl << "========== Seal " << i << " ==========" << endl;
            
            if (seal_set[i].rm_bd_img.empty()) {
                cout << "  Warning: rm_bd_img is empty!" << endl;
                continue;
            }
            
            string save_path = output_dir + "\\seal_" + to_string(i) + "_rm_bd_img.png";
            cv::imwrite(save_path, seal_set[i].rm_bd_img);
            cout << "rm_bd_img saved: " << save_path << endl;
            cout << "rm_bd_img size: " << seal_set[i].rm_bd_img.cols << "x" << seal_set[i].rm_bd_img.rows << endl;
            
            cout << "shape_info.type: " << seal_set[i].shape_info.type << endl;
            cout << "shape_info.minAreaRect: center=(" 
                 << seal_set[i].shape_info.minAreaRect.center.x << ", " 
                 << seal_set[i].shape_info.minAreaRect.center.y << "), size=(" 
                 << seal_set[i].shape_info.minAreaRect.size.width << ", " 
                 << seal_set[i].shape_info.minAreaRect.size.height << "), angle=" 
                 << seal_set[i].shape_info.minAreaRect.angle << endl;
        }
        
        cout << endl << "Done!" << endl;
    } catch (const exception& e) {
        cout << "Error: " << e.what() << endl;
    } catch (...) {
        cout << "Error: Unknown exception occurred!" << endl;
    }
}

int main() {
    process_seal_remove_border();
    return 0;
}
