# -*- coding: utf-8 -*-
"""
@File: dev.py
@Date: 2020-09-14
@author: A2TEC
@section MODIFYINFO 수정정보
- 수정자/수정일   : 수정내역
- 2022-01-14/hsj100@a2tec.co.kr : refactoring
@brief: Developments Test
"""
import struct

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
import bcrypt
from starlette.requests import Request

from main_rest.app.common import consts
from main_rest.app import models as M
from main_rest.app.database.conn import db, Base
from main_rest.app.database.schema import Users, UserLog

from main_rest.app.utils.extra import FernetCrypto, AESCryptoCBC, AESCipher
from custom_logger.main_log import main_logger as LOG


# mail test
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def send_mail():
    """
    구글 계정사용시 : 보안 수준이 낮은 앱에서의 접근 활성화

    :return:
    """
    sender = 'jolimola@gmail.com'
    sender_pw = '!ghkdtmdwns1'
    # recipient = 'hsj100@a2tec.co.kr'
    recipient = 'jwkim@daooldns.co.kr'
    list_cc = ['cc1@gmail.com', 'cc2@naver.com']
    str_cc = ','.join(list_cc)

    title = 'Test mail'
    contents = '''
    This is test mail
    using smtplib.
    '''

    smtp_server = smtplib.SMTP(  # 1
        host='smtp.gmail.com',
        port=587
    )

    smtp_server.ehlo()  # 2
    smtp_server.starttls()  # 2
    smtp_server.ehlo()  # 2
    smtp_server.login(sender, sender_pw)  # 3

    msg = MIMEMultipart()  # 4
    msg['From'] = sender  # 5
    msg['To'] = recipient  # 5
    # msg['Cc'] = str_cc  # 5
    msg['Subject'] = contents  # 5
    msg.attach(MIMEText(contents, 'plain'))  # 6

    smtp_server.send_message(msg)  # 7
    smtp_server.quit()  # 8


router = APIRouter(prefix='/dev')


@router.get('/test', summary='테스트', response_model=M.SWInfo)
async def test(request: Request):
    """
    ## ELB 상태 체크용 API

    **결과**
     - SWInfo
    """

    a = M.SWInfo()

    a.name = '!ekdnfeldpsdptm1'
    # a.name = 'testtesttest123'
    simpleEnDecrypt = FernetCrypto()
    a.data1 = simpleEnDecrypt.encrypt(a.name)
    a.data2 = bcrypt.hashpw(a.name.encode('utf-8'), bcrypt.gensalt())

    t = bytes(a.name.encode('utf-8'))

    enc = AESCryptoCBC().encrypt(t)
    dec = AESCryptoCBC().decrypt(enc)

    t = enc.decode('utf-8')

    # enc = AESCipher('daooldns12345678').encrypt(a.name).decode('utf-8')
    # enc = 'E563ZFt+yJL8YY5yYlYyk602MSscPP2SCCD8UtXXpMI='
    # dec = AESCipher('daooldns12345678').decrypt(enc).decode('utf-8')

    a.data3 = f'enc: {enc}, {t}'
    a.data4 = f'dec: {dec}'
    
    a.name = '!ekdnfeldpsdptm1'
    
    
    # simpleEnDecrypt = SimpleEnDecrypt()
    a.data5 = simpleEnDecrypt.encrypt(a.name)
    a.data6 = bcrypt.hashpw(a.name.encode('utf-8'), bcrypt.gensalt())

    key = consts.ADMIN_INIT_ACCOUNT_INFO['aes_cbc_key']

    t = bytes(a.name.encode('utf-8'))

    enc = AESCryptoCBC(key).encrypt(t)
    dec = AESCryptoCBC(key).decrypt(enc)

    t = enc.decode('utf-8')

    # enc = AESCipher('daooldns12345678').encrypt(a.name).decode('utf-8')
    # enc = 'E563ZFt+yJL8YY5yYlYyk602MSscPP2SCCD8UtXXpMI='
    # dec = AESCipher('daooldns12345678').decrypt(enc).decode('utf-8')

    print(f'key: {key}')
    a.data7 = f'enc: {enc}, {t}'
    a.data8 = f'dec: {dec}'

    
    eee = "gAAAAABioV5NucuS9nQugZJnz-KjVG_FGnaowB9KAfhOoWjjiQ4jGLuYJh4Qe94mT_lCm6m3HhuOJqUeOgjppwREDpIQYzrUXA=="
    a.data8  = simpleEnDecrypt.decrypt(eee)
    
    return a


