"""ABINIT-MPログファイルを解析してCPFファイルに変換するCLIツール。"""
import os
import abmptools
import argparse
import datetime

# Author: Koji Okuwaki


def get_args():
    """コマンドライン引数を解析して返す。

    Returns:
        argparse.Namespace: 入力ログファイル名、対象フラグメント、出力ファイル名等を含む引数。
    """
    parser = argparse.ArgumentParser(
                prog='log2cpf',
                usage='',
                description='Analysis script for ABINIT-MP log',
                epilog='end',
                add_help=True,
                )

    # add args
    parser.add_argument('-f', '--frag',
                        help='tgt fragid info',
                        nargs='*'
                        )

    parser.add_argument('-np', '--pynp',
                        type=int,
                        help='python np',
                        default=1)

    parser.add_argument('-i', '--input',
                        default=None,
                        required=True,
                        help='input file',
                        )

    parser.add_argument('-o', '--output',
                        default=None,
                        help='output file',
                        )

    parser.add_argument('-nof90', '--nof90so',
                        help='use f90',
                        action='store_false',
                        default=True)

    args = parser.parse_args()
    print('## setup info')
    print('tgtfrag =', args.frag)
    print('process =', args.pynp)

    return args


def main():
    args = get_args()
    aobj = abmptools.LOGManager()

    # aobj.readifiewrap(args.input)
    # print(aobj.ifdf)
    # print(aobj.pidf)

    # def read_fraginfo(self, fname):
    aobj.parse(args.input)

    cpf = abmptools.CPFManager()
    cpf.cpfver = 23
    cpf.labels = aobj.labels
    cpf.atominfo = aobj.atominfo
    cpf.fraginfo = aobj.fraginfo
    cpf.condition = aobj.condition
    cpf.static_data = aobj.static_data
    cpf.mominfo = aobj.mominfo
    cpf.diminfo = aobj.diminfo

    if not args.output:
        args.output = os.path.splitext(args.input)[0] + '.cpf'

    d_today = str(datetime.datetime.today())
    cpf.write('Generated by ABMPTools log2cpf ' + d_today, args.output)

#     frags = aobj.readfraginfo(args.input)
#     print(frags)
#     nf = aobj.getlognf(args.input, 'auto')
#     print(nf)

    # required info
    # self.cpfver = cpfver
    # self.atominfo = pd.DataFrame(atominfo)
    # self.fraginfo = fraginfo
    # self.condition = condition
    # self.static_data = static_data
    # self.mominfo = pd.DataFrame(mominfo)
    # self.diminfo = pd.DataFrame(diminfo)
    # self.labels = labels

    # frag-dist
#     if aobj.anlmode == 'frag' and aobj.tgt2type == 'dist':
#         aobj = aobj.readifiewrap(aobj.ilog_head, tgtfrag1)
#     # ffmatrix, fragids
#     if aobj.anlmode == 'frag' and aobj.tgt2type == 'frag':
#         aobj = aobj.readifiewrap(aobj.ilog_head, tgtfrag1, tgtfrag2)
#     # fraginmol
#     if aobj.anlmode == 'fraginmol' or aobj.anlmode == 'mol':
#         aobj = aobj.readifiewrap(aobj.ilog_head)

#     #  filter(for single mode) and write section
#     #  with pb
#     if aobj.matrixtype == 'frags-frags' and aobj.pbflag:
#         aobj = aobj.filterifiewrap()
#         aobj.writecsvwrap(word='gas')
#         aobj = aobj.filterifiewrap(
#             myifdf=aobj.pbifdf, mypidf=aobj.pbpidf, is_pb=True)
#         aobj.writecsvwrap(word='pb', pbwrite=True)
#
#     # only-gas
#     else:
#         aobj = aobj.filterifiewrap()
#         aobj.writecsvwrap()


if __name__ == '__main__':
    main()
