import configargparse


def config_parser():
    parser = configargparse.ArgumentParser()
    # general
    parser.add_argument('--config', is_config_file=True, help='config file path')
    parser.add_argument('--expname', type=str, help='experiment name')
    parser.add_argument('--logdir', type=str, help='log folder')
    parser.add_argument('--ckptdir', type=str, help='checkpoint folder')
    parser.add_argument('--distributed', action='store_true', help='if use distributed training')
    parser.add_argument("--local_rank", type=int, default=0, help='rank for distributed training')
    parser.add_argument('-j', '--workers', default=0, type=int, metavar='N',
                        help='number of data loading workers (default: 8)')
    parser.add_argument('--debug', action='store_true', help='debug mode (only takes one datum)')

    ########## dataset options ##########
    ## train and eval dataset
    parser.add_argument('--data_path', type=str, help='the dataset to train')
    parser.add_argument('--data_type', type=str, default='srn', help='dataset type to use')
    parser.add_argument('--img_hw', type=int, nargs='+', help='image size option for dataset')
    parser.add_argument('--closest_n_views', type=int,
                        default=-1,
                        help='choose closest n views from the dataset (-1 means all views)')
    parser.add_argument('--batch_size', type=int, default=1, help='training batch size')

    ########## model options ##########
    ## ray sampling options
    parser.add_argument('--sample_mode', type=str, default='uniform',
                        help='how to sample pixels from images for training:'
                             'uniform|center|bbox|bbox_sample_full')
    parser.add_argument('--center_ratio', type=float, default=0.8, help='the ratio of center crop to keep')
    parser.add_argument('--N_rand', type=int, default=128,
                        help='batch size (number of random rays per gradient step)')
    parser.add_argument('--chunk_size', type=int, default=128,
                        help='number of rays processed in parallel, decrease if running out of memory')
    
    ## model options
    parser.add_argument('--im_feat_dim', type=int, default=256, help='image feature dimension')
    parser.add_argument('--mlp_feat_dim', type=int, default=512, help='mlp hidden dimension')
    parser.add_argument('--freq_num', type=int, default=10, help='how many frequency bases for positional encodings')
    parser.add_argument('--mlp_block_num', type=int, default=2, help='how many resnet blocks for coarse network')
    parser.add_argument('--coarse_only', action='store_true', help='use coarse network only')
    parser.add_argument("--anti_alias_pooling", type=int, default=1, help='if use anti-alias pooling')
    parser.add_argument('--num_source_views', type=int, default=1, help='number of views')
    parser.add_argument('--freeze_pos_embed', action='store_true', help='freeze positional embeddings')
    parser.add_argument('--no_skip_conv', action='store_true', help='disable skip convolution')

    ########## checkpoints ##########
    parser.add_argument('--no_reload', action='store_true',
                        help='do not reload weights from saved ckpt')
    parser.add_argument('--ckpt_path', type=str, default='',
                        help='specific weights npy file to reload for coarse network')
    parser.add_argument('--no_load_opt', action='store_true',
                        help='do not load optimizer when reloading')
    parser.add_argument('--no_load_scheduler', action='store_true',
                        help='do not load scheduler when reloading')

    ########### iterations & learning rate options ##########
    parser.add_argument('--n_iters', type=int, default=500000, help='num of iterations')
    parser.add_argument('--lrate_feature', type=float, default=1e-3, help='learning rate for feature extractor')
    parser.add_argument('--lrate_mlp', type=float, default=5e-4, help='learning rate for mlp')
    parser.add_argument('--lrate_decay_factor', type=float, default=0.5,
                        help='decay learning rate by a factor every specified number of steps')
    parser.add_argument('--lrate_decay_steps', type=int, default=50000,
                        help='decay learning rate by a factor every specified number of steps')
    parser.add_argument('--warmup_steps', type=int, default=10000, help='num of iterations for warm-up')
    parser.add_argument('--scheduler', type=str, default='steplr', help='scheduler type to use [steplr]')
    parser.add_argument('--use_warmup', action='store_true', help='use warm-up scheduler')
    parser.add_argument('--bbox_steps', type=int, default=100000, help='iterations to use bbox sampling')

    ########## rendering options ##########
    parser.add_argument('--N_samples', type=int, default=64, help='number of coarse samples per ray')
    parser.add_argument('--N_importance', type=int, default=64, help='number of important samples per ray')
    parser.add_argument('--inv_uniform', action='store_true',
                        help='if True, will uniformly sample inverse depths')
    parser.add_argument('--det', action='store_true', help='deterministic sampling for coarse and fine samples')
    parser.add_argument('--white_bkgd', action='store_true',
                        help='apply the trick to avoid fitting to white background')
    parser.add_argument('--render_stride', type=int, default=1,
                        help='render with large stride for validation to save time')

    ########## logging/saving options ##########
    parser.add_argument('--i_print', type=int, default=100, help='frequency of terminal printout')
    parser.add_argument('--i_img', type=int, default=1000, help='frequency of tensorboard image logging')
    parser.add_argument('--i_weights', type=int, default=25000, help='frequency of weight ckpt saving')
    parser.add_argument('--train_indices', type=int, nargs='+', default=[0, 100, 200],
                        help='indices for visualization of training data')
    parser.add_argument('--train_src_views', type=int, nargs='+', default=[0, 0, 0],
                        help='source view indices for visualization of training data')
    parser.add_argument('--train_tgt_views', type=int, nargs='+', default=[5, 15, 25],
                        help='target view indices for visualization of training data')
    parser.add_argument('--val_indices', type=int, nargs='+', default=[0, 3, 43],
                        help='indices for visualization of validation data')
    parser.add_argument('--val_src_views', type=int, nargs='+', default=[64, 64, 64],
                        help='source view indices for visualization of validation data')
    parser.add_argument('--val_tgt_views', type=int, nargs='+', default=[0, 50, 120],
                        help='target view indices for visualization of validation data')

    return parser