package test05.config;

import com.alibaba.druid.pool.DruidDataSource;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

//@Component
public class DatabaseConfig {

//    @Autowired   可以注入bean
//    ISysUserService userService;
    DruidDataSource ds;

    Boolean input = false;

    @Autowired
    DatabaseConfig(DruidDataSource ds) {
        this.ds = ds;
    }

        @PostConstruct
        public void init() {
            Thread inputThread = new Thread(() -> {
                try {
                    System.out.println(System.getProperty("user.dir"));
                    String workingDir = System.getProperty("user.dir");
                    if (new File(workingDir+"\\db_student.db").exists()) {
                        ds.restart();
                        ds.setDriverClassName("org.sqlite.JDBC");
                        ds.setUrl("jdbc:sqlite:"+workingDir+"\\db_student.db");
                        System.out.println("使用外部数据库文件！");
                    }else{
                        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("请选择数据库（1：mysql，2：sqlite）");
                        this.input = true;
                        int type = 0;
                        try {
                            type = Integer.parseInt(br.readLine());
                        } catch (NumberFormatException | NullPointerException e) {
                            System.out.println("将会使用内部部数据库文件！");
                            return;
                        }
                        switch (type) {
                            case 1:

                                System.out.println("请输入数据库url（仅地址）,留空使用localhost:3306");
                                String url= br.readLine();
                                System.out.println("请输入用户名：");
                                String username=br.readLine();
                                System.out.println("请输入密码：");
                                String password=br.readLine();
                                ds.restart();
                                ds.setUsername(username);
                                ds.setPassword(password);
                                ds.setUrl("jdbc:mysql:"+(url.isEmpty()?"localhost:3306":url)+(url.endsWith("/")?"":"/")+"db_student");
                                ds.setDriverClassName("com.mysql.jdbc.Driver");
                            case 2:
                                System.out.println("使用内嵌测试数据，修改将不会被保存！");
                                ds.setDriverClassName("org.sqlite.JDBC");
                                ds.setUrl("jdbc:sqlite::resource:db/db_student.db");
                        }
                    }
                } catch (IOException | SQLException e) {
                    throw new RuntimeException(e);
                }
            });
            inputThread.start();
            try {
                inputThread.join(10000);  // 等待10秒
                if (inputThread.isAlive() && !input) {
                    // 如果10秒后线程还活着，说明没有输入
                    inputThread.interrupt();
                    System.out.println("未配置数据库");
                    return;
                }
            } catch (InterruptedException e) {
                return ;
            } finally {
                inputThread.interrupt();
            }
        }
}
