package task

import (
	"fmt"
	"git.listensoft.net/tool/jspkit/common"
	"git.listensoft.net/tool/jspkit/common/models"
	"git.listensoft.net/tool/jspkit/common/variable"
	"git.listensoft.net/tool/jspkit/logger"
	"git.listensoft.net/tool/jspkit/taxerr"
	"github.com/go-rod/rod"
	"github.com/tidwall/gjson"
	"regexp"
	"strings"
)

func GetEacComInfo(task models.TaxTask) (models.CompanyInfo, error) {
	//基础信息暂不校验
	//去除税号中的换行符
	task.TaxNo = strings.TrimSpace(task.TaxNo)
	Compile := regexp.MustCompile("^[\u4e00-\u9fa5]+$") // 全中文判断
	if Compile.MatchString(task.TaxNo) {
		return models.CompanyInfo{}, taxerr.TaxNoError
	}
	cominfo := models.CompanyInfo{
		ComName:  task.ComName,
		TaxNo:    task.TaxNo,
		IdNo:     task.IdNo,
		Password: task.Password,
		Zzrxm:    task.Zzrxm,
		Zzridno:  task.Zzridno,
		Zzrmm:    task.Zzrmm,
		Xzsf:     task.Xzsf,
		Tel:      task.Tel,
		Area:     task.Address,
		Period:   task.Period,
	}
	if task.Address == "sichuan" || task.Address == "xinjiang" {
		regRuler := "^[0-9a-zA-Z]+$"
		// 正则调用规则
		reg := regexp.MustCompile(regRuler)
		// 返回 MatchString 是否匹配
		is_tel := reg.MatchString(task.TaxNo)
		if !is_tel {
			return cominfo, common.HandleError(nil, taxerr.NewUser(fmt.Sprintf(`"税号[%s]有误，请核实后重新发起"`, task.TaxNo)))
		}
	}
	fmt.Println(gjson.Get(task.Data, "setting").String())
	kjze := variable.Kjze(gjson.Get(task.Data, "setting.comKj").String())
	if !kjze.Check() {
		return cominfo, taxerr.NewUserV3("会计准则有误！", "任务发起异常请重新发起")
	}
	qysdslx := variable.Qysdslx(gjson.Get(task.Data, "setting.qysdsAOrB").String())
	if !qysdslx.Check() {
		return cominfo, taxerr.NewUserV3("企业所得税类型有误！", "任务发起异常请重新发起")
	}
	nsrlx := variable.Nsrlx(gjson.Get(task.Data, "setting.taxType").String())
	if !nsrlx.Check() {
		return cominfo, taxerr.NewUserV3("纳税人类型有误！", "任务发起异常请重新发起")
	}
	gslx := variable.Gslx(gjson.Get(task.Data, "setting.comType").String())
	if !gslx.Check() {
		return cominfo, taxerr.NewUserV3("公司类型有误！", "任务发起异常请重新发起")
	}
	xjllbSb := gjson.Get(task.Data, "setting.xjllbSb").String()
	if xjllbSb == "是" {
		cominfo.XjllbSb = true
	}
	bsbcwbb := gjson.Get(task.Data, "setting.bsbcwbb").String()
	if bsbcwbb == "是" {
		cominfo.Bsbcwbb = true
	}
	//不去判断这个东西了 不是 是 默认 否 即可
	vatBc := gjson.Get(task.Data, `setting.vatBc`).String()
	if vatBc == "是" {
		cominfo.VatBc = true
	}
	sffgs := gjson.Get(task.Data, `setting.sffgs`).String()
	if sffgs == "是" {
		cominfo.Sffgs = true
	}
	lslf := gjson.Get(task.Data, `setting.lslf`).String()
	if lslf == "是" {
		cominfo.Lslf = true
	}
	sfxsbdc := gjson.Get(task.Data, `setting.sfxsbdc`).String()
	if sfxsbdc == "是" {
		cominfo.Sfsxbdc = true
	}
	sfhqdkfp := gjson.Get(task.Data, `setting.sfhqdkfp`).String() //是否获取代开发票
	if sfhqdkfp == "是" {
		cominfo.HqDkFp = true
	}
	sfhqckts := gjson.Get(task.Data, `setting.ckts`).String() //是否获取出口退税发票
	if strings.Contains(sfhqckts, "是") {
		cominfo.Ckts = true
	}
	gxqrAll := gjson.Get(task.Data, `setting.gxqrAll`).String() //是否勾选全部
	if gxqrAll == "是" {
		cominfo.GxqrAll = true
	}
	cscsts := gjson.Get(task.Data, `setting.cscsts`).String() //是否获取代开发票
	if cscsts == "是" {
		common.InfoCscsts = true
		cominfo.Cscsts = true
	}
	jcwq := gjson.Get(task.Data, `setting.jcwq`).String() //是否获取代开发票
	if jcwq == "是" {
		cominfo.Jcwq = true
	}
	noTxfFp := gjson.Get(task.Data, `setting.noTxfFp`).String() //是否不采集通行费发票
	if noTxfFp == "是" {
		cominfo.NoTxfFp = true
	}
	wqsb := gjson.Get(task.Data, `setting.wqsb`).String() //是否获取代开发票
	if wqsb == "是" {
		cominfo.Wqsb = true
	}
	qzsbcwbb := gjson.Get(task.Data, `setting.qzsbcwbb`).String() //强制申报财报，如果已申报就直接更正
	if qzsbcwbb == "是" || qzsbcwbb == "true" {
		cominfo.Qzsbcwbb = true
	}
	if agTaxNo := gjson.Get(task.Data, `setting.dlLogin.idNo`).String(); agTaxNo != "" {
		cominfo.Dldl = true
		cominfo.AgTaxNo = agTaxNo
		cominfo.AgTel = gjson.Get(task.Data, `setting.dlLogin.zzridno`).String()
		cominfo.AgPwd = gjson.Get(task.Data, `setting.dlLogin.zzrmn`).String()
	}
	zgswjg := gjson.Get(task.Data, `setting.zgswjg`).String()
	sfxy := gjson.Get(task.Data, `setting.sfxy`).String()
	sbPwd := gjson.Get(task.Data, `setting.shebaoPassword`).String()
	shebaoPasswordLogin := gjson.Get(task.Data, `setting.shebaoPasswordLogin`).String()
	cominfo.Zgswjg = zgswjg
	cominfo.Dlfs = task.LoginType
	if cominfo.Dlfs == "新版登陆" { // 这边强制改一下 别跑任务提示登录方式不对 = =
		cominfo.Dlfs = "新版登录"
	}
	cominfo.AccountSystem = kjze
	cominfo.Qysdslx = qysdslx
	cominfo.Nsrlx = nsrlx
	cominfo.Gslx = gslx
	cominfo.Sfxy = sfxy
	cominfo.SheBaoPassword = sbPwd
	cominfo.SheBaoPasswordLogin = shebaoPasswordLogin
	rod.Try(func() {
		if strings.Contains(cominfo.Tel, "#") {
			cominfo.Tel = strings.Split(cominfo.Tel, "#")[0]
		}
	})
	common.ComInfo = cominfo
	logger.Info(cominfo)
	return cominfo, nil
}
