package jj.tech.paolu.task;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.jdbc.lock.JdbcLockRegistry;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import jj.tech.paolu.biz.webadmin.component.Hgt8Component;
import jj.tech.paolu.biz.webadmin.service.Idc9998Sevice;
import jj.tech.paolu.repository.mybatis.dao.Idc9998AdminInfoMapper;
import jj.tech.paolu.repository.mybatis.entity.Idc9998AdminInfo;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;


@Component
public class Hgt8Timer {
	private static Logger logger = LoggerFactory.getLogger(Hgt8Timer.class);
	
	@Autowired Idc9998Sevice idc9998Sevice;
	@Autowired Idc9998AdminInfoMapper idc9998AdminInfoMapper;
	@Autowired Hgt8Component hgt8Component;
	
	@Autowired JdbcLockRegistry jdbcLockRegistry;
	
	//@Scheduled(cron = "0/180 * * * * ?")//60s
	@Scheduled(fixedDelay = 1000 * 180)	//上一次执行完毕之后,执行的时间间隔
	public void login() {
		Lock lock = jdbcLockRegistry.obtain("login");
		if(lock.tryLock()) {
			Idc9998AdminInfo admin =  idc9998Sevice.getByUserName("bk7897");
			if(admin != null && admin.getIs_online() != 1) {
				Boolean login_ok =		
				hgt8Component
					.addInstance(admin.getUser_name())
					.forceLogin();
				if(login_ok) {
					//更新用户登录状态
					String loginInfo = hgt8Component.addInstance(admin.getUser_name()).aPIRequestContext.storageState();
					idc9998Sevice.updateLoginInfo(hgt8Component.addInstance(admin.getUser_name()).admin.getUser_name(), loginInfo, 1);
					hgt8Component.addInstance(admin.getUser_name()).admin.setIs_online(1);
					hgt8Component.addInstance(admin.getUser_name()).admin.setLogin_info(loginInfo);
				}
						
			}
			lock.unlock();
		}
		
	}
	
	
	@Scheduled(fixedDelay = 1000 * 60)
	public void isLogin() {
		Lock lock = jdbcLockRegistry.obtain("login");
		if(lock.tryLock()) {
			Hgt8Component bk =  hgt8Component.getInstance("bk7897");
			if(bk!=null && bk.admin!=null) {
				Boolean isLogin =  bk.page.frameLocator("#ifr_main").locator("#jishi-order").isVisible();
				
				if(!isLogin) {
					bk.admin.setIs_online(0);
					idc9998Sevice.updateLoginInfo("bk7897", bk.aPIRequestContext.storageState(), 0);
				}
				
			}
			lock.unlock();
		}
	}
	
	@Scheduled(cron = "0/60 * * * * ?")//5秒
	public void get_g21_curr() {
		
		Lock lock = jdbcLockRegistry.obtain("get_g21_curr");
		if(lock.tryLock()) {
			Hgt8Component bk =  hgt8Component.getInstance("bk7897");
			
			if(bk!=null && bk.admin!=null && bk.admin.getIs_online()==1) {
				bk.wrong_times=new AtomicInteger(0);
				bk.G_24_Curr().join();
			}
			lock.unlock();
		}

	}
	
//	//@Scheduled(cron = "0/60 * * * * ?")//5秒
//	@Scheduled(initialDelay = 1000 * 3, fixedDelay=Long.MAX_VALUE)
//	public void get_g21_curr() {
//		
//		Lock lock = jdbcLockRegistry.obtain("get_g21_curr");
//		if(lock.tryLock()) {
//			Hgt8Component bk =  hgt8Component.getInstance("bk7897");
//			if(bk==null || bk.admin==null) {
//				idc9998Sevice.updateIsOnline("bk7897", 0);
//				lock.unlock();
//				return;
//			}
//			if(bk.admin.getIs_online()==1) {
//				//bk.wrong_times=new AtomicInteger(0);
//				bk.G_24_Curr().join();
//				
////				bk.admin.setIs_online(0);
////				idc9998Sevice.updateIsOnline("bk7897", 0);
//				
//			}
//			lock.unlock();
//		}
//
//	}
	
	
	
//	@Scheduled(cron = "0/20 * * * * ?")//5秒
//	public void get_g21_his() {
//		try {
//			
//			Hgt8Component bk =  hgt8Component.getInstance("bk7897");
//			if(bk != null) {
//				bk.G_24_His(1, "", LocalDate.now().minusDays(1));
//			}
//			
//		} catch (Exception e) {
//			logger.info(e.getMessage());
//			idc9998Sevice.updateIsOnline("bk7897", 0);
//			// TODO: handle exception
//		}
//		
//	}
	
	

}
