#include "ml307_board.h"
#include "audio_codecs/es8311_audio_codec.h"
#include "display/lcd_display.h"
#include "application.h"
#include "button.h"
#include "config.h"
#include "iot/thing_manager.h"

#include "esp_lcd_mipi_dsi.h"
#include "esp_lcd_jd9365_10_1.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_mipi_dsi.h"
#include "esp_lcd_touch_gt911.h"
#include <lvgl.h>
// #include "lv_disp.h"  // 包含显示相关声明

#include <esp_lcd_ek79007.h>
#include <wifi_station.h>
#include <esp_log.h>
#include <esp_lcd_panel_vendor.h>
#include <driver/i2c_master.h>
#include <esp_lvgl_port_touch.h>
#include <esp_ldo_regulator.h>

#define TAG "esp_sparkbot"

LV_FONT_DECLARE(font_puhui_20_4);
LV_FONT_DECLARE(font_awesome_20_4);

// 自定义音频编解码器
class SparkBotEs8311AudioCodec : public Es8311AudioCodec {
public:
    SparkBotEs8311AudioCodec(i2c_master_bus_handle_t i2c_handle, i2c_port_t i2c_port, 
                        int input_sample_rate, int output_sample_rate,
                        gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, 
                        gpio_num_t dout, gpio_num_t din, gpio_num_t pa_pin, 
                        uint8_t es8311_addr, bool use_mclk = true)
        : Es8311AudioCodec(i2c_handle, i2c_port, input_sample_rate, output_sample_rate,
                           mclk, bclk, ws, dout, din, pa_pin, es8311_addr, use_mclk) {}

    void EnableOutput(bool enable) override {
        if (enable == output_enabled_) return;
        if (enable) Es8311AudioCodec::EnableOutput(enable);
    }
};

// 自定义背光控制类
class CustomBacklight : public Backlight {
private:
    i2c_master_bus_handle_t i2c_handle_;
    uint8_t i2c_address_;
    uint8_t reg_;

public:
    CustomBacklight(i2c_master_bus_handle_t i2c_handle, uint8_t i2c_addr, uint8_t reg)
        : i2c_handle_(i2c_handle), i2c_address_(i2c_addr), reg_(reg) {}

    void SetBrightnessImpl(uint8_t brightness) override {
        uint8_t data[2] = {reg_, brightness};
        i2c_master_dev_handle_t dev_handle;
        i2c_device_config_t dev_cfg = {
            .dev_addr_length = I2C_ADDR_BIT_LEN_7,
            .device_address = i2c_address_,
            .scl_speed_hz = 100000,
        };
        esp_err_t err = i2c_master_bus_add_device(i2c_handle_, &dev_cfg, &dev_handle);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Failed to add I2C device: %s", esp_err_to_name(err));
            return;
        }
        err = i2c_master_transmit(dev_handle, data, sizeof(data), -1);
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "Failed to transmit brightness: %s", esp_err_to_name(err));
        } else {
            ESP_LOGI(TAG, "Backlight brightness set to %u", brightness);
        }
    }
};

class EspSparkBot : public Ml307Board {
private:
    i2c_master_bus_handle_t i2c_bus_;
    Button boot_button_;
    Display* display_;
    Backlight* backlight_;

    // 初始化I2C总线
    void InitializeI2c() {
        i2c_master_bus_config_t i2c_bus_cfg = {
            .i2c_port = 1,
            .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
            .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
            .clk_source = I2C_CLK_SRC_DEFAULT,
            .glitch_ignore_cnt = 7,
            .intr_priority = 0,
            .trans_queue_depth = 0,
            .flags = {.enable_internal_pullup = 1},
        };
        ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
    }

    // 初始化EK79007显示
    void InitializeEk79007Display() {
        esp_err_t ret;
        esp_ldo_channel_handle_t phy_pwr_chan;
        esp_ldo_channel_config_t ldo_cfg = {
            .chan_id = 3,
            .voltage_mv = 2500,
        };
        esp_ldo_acquire_channel(&ldo_cfg, &phy_pwr_chan);
        ESP_LOGI(TAG, "MIPI DSI PHY Powered on");

        esp_lcd_dsi_bus_handle_t mipi_dsi_bus;
        esp_lcd_dsi_bus_config_t bus_config = {
            .bus_id = 0,
            .num_data_lanes = 2,
            .phy_clk_src = MIPI_DSI_PHY_CLK_SRC_DEFAULT,
            .lane_bit_rate_mbps = 1000,
        };
        ret = esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus);
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "DSI bus init failed: %s", esp_err_to_name(ret));
            return;
        }

        ESP_LOGI(TAG, "Install MIPI DSI LCD control panel");
        esp_lcd_panel_io_handle_t io;
        esp_lcd_dbi_io_config_t dbi_config = {
            .virtual_channel = 0,
            .lcd_cmd_bits = 8,
            .lcd_param_bits = 8,
        };

        ret = esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_config, &io);
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "New panel IO failed %s", esp_err_to_name(ret));
            return;
        }

        esp_lcd_panel_handle_t disp_panel = NULL;
        esp_lcd_dpi_panel_config_t dpi_config = {
            .virtual_channel = 0,
            .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
            .dpi_clock_freq_mhz = 52,
            .pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565,
            .num_fbs = 1,
            .video_timing = {
                .h_size = DISPLAY_WIDTH,
                .v_size = DISPLAY_HEIGHT,
                .hsync_pulse_width = 10,
                .hsync_back_porch = 160,
                .hsync_front_porch = 160,
                .vsync_pulse_width = 1,
                .vsync_back_porch = 23,
                .vsync_front_porch = 12,
            },
            .flags = {.use_dma2d = true},
        };
        ek79007_vendor_config_t vendor_config = {
            .mipi_config = {
                .dsi_bus = mipi_dsi_bus,
                .dpi_config = &dpi_config,
            },
        };
        esp_lcd_panel_dev_config_t lcd_dev_config = {
            .reset_gpio_num = DISPLAY_RST_GPIO,
            .rgb_ele_order = ESP_LCD_COLOR_SPACE_RGB,
            .bits_per_pixel = 16,
            .vendor_config = &vendor_config,
        };
        ret = esp_lcd_new_panel_ek79007(io, &lcd_dev_config, &disp_panel);
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "New LCD panel EK79007 failed %s", esp_err_to_name(ret));
            return;
        }
        ret = esp_lcd_panel_reset(disp_panel);
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "LCD panel reset failed %s", esp_err_to_name(ret));
            return;
        }
        ret = esp_lcd_panel_init(disp_panel);
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "LCD panel init failed %s", esp_err_to_name(ret));
            return;
        }

        display_ = new MipiLcdDisplay(io, disp_panel,i2c_bus_,
            DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, 
            DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
            {
                .text_font = &font_puhui_20_4,
                .icon_font = &font_awesome_20_4,
                .emoji_font = font_emoji_64_init(),    
            });

        esp_lcd_touch_config_t tp_cfg = {
            .x_max = DISPLAY_WIDTH,
            .y_max = DISPLAY_HEIGHT,
            .rst_gpio_num = GPIO_NUM_NC,
            .int_gpio_num = GPIO_NUM_NC,
            .levels = {.reset = 0, .interrupt = 0},
            .flags = {.swap_xy = 0, .mirror_x = 1, .mirror_y = 1},
        };

        esp_lcd_panel_io_handle_t tp_io_handle = NULL;
        esp_lcd_touch_handle_t tp;
        esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
        tp_io_config.scl_speed_hz = 400000;
        ret = esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle);
        if (ret != ESP_OK) {
           ESP_LOGE(TAG, "esp_lcd_new_panel_io_i2c failed %s", esp_err_to_name(ret));
           return;
        }
        esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp);
        const lvgl_port_touch_cfg_t touch_cfg = {
           .disp = lv_display_get_default(),
           .handle = tp,
        };
        lvgl_port_add_touch(&touch_cfg);
    }

    // 初始化JD9365显示
    void InitializeJd9365Display() {
        bsp_enable_dsi_phy_power();
        esp_lcd_panel_io_handle_t io = NULL;
        esp_lcd_panel_handle_t disp_panel = NULL;

        esp_lcd_dsi_bus_handle_t mipi_dsi_bus = NULL;
        esp_lcd_dsi_bus_config_t bus_config = JD9365_PANEL_BUS_DSI_2CH_CONFIG();
        esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus);

        ESP_LOGI(TAG, "Install MIPI DSI LCD control panel");
        esp_lcd_dbi_io_config_t dbi_config = JD9365_PANEL_IO_DBI_CONFIG();
        esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_config, &io);

        esp_lcd_dpi_panel_config_t dpi_config = {
            .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
            .dpi_clock_freq_mhz = 80,
            .pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565,
            .num_fbs = 1,
            .video_timing = {
                .h_size = DISPLAY_WIDTH,
                .v_size = DISPLAY_HEIGHT,
                .hsync_pulse_width = 10,
                .hsync_back_porch = 160,
                .hsync_front_porch = 160,
                .vsync_pulse_width = 3,
                .vsync_back_porch = 23,
                .vsync_front_porch = 12,
            },
            .flags = {.use_dma2d = true},
        };

        jd9365_vendor_config_t vendor_config = {
            .mipi_config = {
                .dsi_bus = mipi_dsi_bus,
                .dpi_config = &dpi_config,
                .lane_num = 2,
            },
            .flags = {.use_mipi_interface = 1},
        };

        const esp_lcd_panel_dev_config_t lcd_dev_config = {
            .reset_gpio_num = PIN_NUM_LCD_RST,
            .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
            .bits_per_pixel = 16,
            .vendor_config = &vendor_config,
        };
        esp_lcd_new_panel_jd9365(io, &lcd_dev_config, &disp_panel);
        esp_lcd_panel_reset(disp_panel);
        esp_lcd_panel_init(disp_panel);

        display_ = new MipiLcdDisplay(io, disp_panel,i2c_bus_,
            DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, 
            DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
            {
                .text_font = &font_puhui_20_4,
                .icon_font = &font_awesome_20_4,
                .emoji_font = font_emoji_64_init(),
            });

        backlight_ = new CustomBacklight(i2c_bus_, 0x45, 0x96);
        backlight_->RestoreBrightness();



            //   lv_obj_t *touch_area = lv_obj_create(lv_scr_act());
            //  lv_area_t scr_area;
            // scr_area.x1 = 0;
            // scr_area.y1 = 0;
            // scr_area.x2 = DISPLAY_HEIGHT - 1;
            // scr_area.y2 = DISPLAY_WIDTH - 1;
            // lv_obj_set_size(touch_area, 1280, 800);
            // lv_obj_align(touch_area, LV_ALIGN_CENTER, 0, 0); // Center it on the screen
    }

    static esp_err_t bsp_enable_dsi_phy_power(void) {
#if MIPI_DSI_PHY_PWR_LDO_CHAN > 0
        static esp_ldo_channel_handle_t phy_pwr_chan = NULL;
        esp_ldo_channel_config_t ldo_cfg = {
            .chan_id = MIPI_DSI_PHY_PWR_LDO_CHAN,
            .voltage_mv = MIPI_DSI_PHY_PWR_LDO_VOLTAGE_MV,
        };
        esp_ldo_acquire_channel(&ldo_cfg, &phy_pwr_chan);
        ESP_LOGI(TAG, "MIPI DSI PHY Powered on");
#endif
        return ESP_OK;
    }

    // 初始化按钮
    void InitializeButtons() {
        boot_button_.OnClick([this]() {
            auto& app = Application::GetInstance();
            if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
                // 4G板无WiFi重置功能
            }
            app.ToggleChatState();
        });
    }

    // 初始化物联网功能
    void InitializeIot() {
        auto& thing_manager = iot::ThingManager::GetInstance();
        thing_manager.AddThing(iot::CreateThing("Speaker"));
        thing_manager.AddThing(iot::CreateThing("Screen"));
    }

public:
    EspSparkBot() : Ml307Board(ML307_TX_PIN, ML307_RX_PIN, 4096), 
                    boot_button_(BOOT_BUTTON_GPIO),
                    display_(nullptr),
                    backlight_(nullptr) {
        InitializeI2c();
        InitializeButtons();
        InitializeIot();

        // 根据配置选择显示初始化
#if CONFIG_LCD_EK79007_1024X600
        InitializeEk79007Display();
        backlight_ = new PwmBacklight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
#elif CONFIG_LCD_TYPE_800_1280_10_1_INCH || CONFIG_LCD_TYPE_800_1280_10_1_INCH_A
        InitializeJd9365Display();
#endif

        if (backlight_) {
            backlight_->RestoreBrightness();
        }
    }



    AudioCodec* GetAudioCodec() override {
        static SparkBotEs8311AudioCodec audio_codec(i2c_bus_, I2C_NUM_0, 
            AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
            AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, 
            AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
            AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
        return &audio_codec;
    }

    Display* GetDisplay() override {
        return display_;
    }

    Backlight* GetBacklight() override {
        return backlight_;
    }
};

DECLARE_BOARD(EspSparkBot);
// #endif // 闭合条件编译