const axios = require('axios');
const config = require('../etc/config.json');
const helper = require('./helper');
class PullDataService {
    constructor(redis_help) {
        this.mRedis = redis_help
        this.session = axios.create({
            baseURL: config.isDebug?config.pull_data_config.debug_host:config.pull_data_config.release_host,
            headers: {
              'Accept': 'application/json, text/plain, */*',
            }
        });
        this.token = ""
    }


    async getToken() {
        try {
            let maxRetries = 3
            let delay = 1000;
            for (let i = 0; i < maxRetries; i++) {
                const response = await this.session.post(config.pull_data_config.get_token,{
                    userName:config.pull_data_config.userName,
                    verCode:config.pull_data_config.verCode,
                    password:config.pull_data_config.password,
                    loginType:config.pull_data_config.loginType
                });

                if(!response.data.success){
                    if (i === maxRetries - 1) throw response;
                    console.log(`Retry getToken ${i + 1} of ${maxRetries}`);
                    await new Promise(resolve => setTimeout(resolve, delay * (i + 1)));
                }else{
                    this.token = response.data.data.token
                    await this.mRedis.setKeyValue("pull_data_token",this.token)
                    return response.data;
                }
            }
        } catch (error) {
            return  {data:null,success:false,msg:error}
        }
    }


    async get_novel_material_list(cur_timeRange,page,size=500) {
        let maxRetries = 3
        let delay = 1000;
        for (let i = 0; i < maxRetries; i++) {
            try {
                var now = new Date(); // 当前时间

                if(cur_timeRange!=null){
                    now = helper.getTimeStampByHourMinute(cur_timeRange.start)
                }
                let interval_minute = 60;
            
                if(cur_timeRange!=null){
                    interval_minute = cur_timeRange.interval_minute
                }
                // const fiveMinutesAgo = new Date(now - 5 * 60 * 1000); // 5分钟前的时间
                const fiveMinutesAgo = new Date(now - interval_minute * interval_minute * 1000); // 60分钟前的时间
                let data = {
                    startTime:fiveMinutesAgo.getTime(),
                    endTime:now.getTime()
                }
            
                if(cur_timeRange!=null){
                    cur_timeRange.pull_time = data
                }
                let params = `page=${page}&size=${size}&sort=update_time,desc`
                let url = config.pull_data_config.get_novel_material_list+params
                console.log(url,data)
                var response = await this.session.post(url,data, {
                    headers: {
                        'token':await this.mRedis.getKeyValue("pull_data_token")
                    }
                });
                if(!response.data.success){
                    throw response.data
                }else{
                    return response.data;
                }
            } catch (error) {
                if (i === maxRetries - 1) return {data:null,success:false,msg:error};
                console.log(`Retry get_novel_material_list ${i + 1} of ${maxRetries}`);
                await new Promise(resolve => setTimeout(resolve, delay * (i + 1)));
            }
        }

     
    }


    async get_detail(materialId_list) {

        let maxRetries = 3
        let delay = 1000;
        for (let i = 0; i < maxRetries; i++) {
            try {
                let data = {
                    list:materialId_list
                }
                const response = await this.session.post(config.pull_data_config.get_detail,data, {
                    headers: {
                        'token':await this.mRedis.getKeyValue("pull_data_token")
                    }
                });
                if(!response.data.success){
                    throw response.data
                }else{
                    return response.data;
                }
            } catch (error) {
                if (i === maxRetries - 1) return {data:null,success:false,msg:error};
                await this.getToken()
                console.log(`Retry get_detail ${i + 1} of ${maxRetries}`);
                await new Promise(resolve => setTimeout(resolve, delay * (i + 1)));
            }
 
        }
  

    }

}

module.exports = PullDataService;