from os.path import splitext


class RecSetup(object):
    """Setup parameters for reconstruction."""

    def __init__(self):
        pass

    @classmethod
    def from_args(cls, args):
        """Create from parsed argument list."""

        setup = cls()

        setup.verbose = args.verbose

        setup.input_filename = args.input
        setup.input_grid_spacing = args.pixel_size
        setup.num_counts = args.counts
        setup.output_grid_spacing = args.voxel_size
        setup.output_mrc_filename = args.output
        setup.output_base = splitext(setup.output_mrc_filename)[0]

        setup.input_size = args.input_size

        setup.num_components = int(args.components)
        setup.mean_variance = None
        if args.prior_mean:
            setup.mean_variance = float(args.prior_mean**2)
        setup.gamma_alpha = float(args.prior_variance[0])
        setup.gamma_beta = float(args.prior_variance[1])
        setup.alpha_0 = float(args.prior_weights)

        setup.use_global = args.global_rotations
        setup.num_global_rotations = args.random_rotations
        if setup.num_global_rotations < 1:
            print('Number of global rotations should be at least 1.')
            setup.num_global_rotations = 1


        setup.num_steps = args.steps[0]
        if len(args.steps) >= 3:
            setup.num_mixture_steps = args.steps[1]
            setup.num_transformation_steps = args.steps[2]
        else:
            setup.num_mixture_steps = 100
            setup.num_transformation_steps = 100

        if setup.verbose >= 2:
            print('Number of steps = %d.' % setup.num_steps)
            if setup.use_global:
                print('Mixture and transformation steps = (%d, %d).' % (
                    setup.num_mixture_steps,setup.num_transformation_steps))

        if not setup.use_global:
            setup.num_sub_steps = 1
        else:
            setup.num_sub_steps = (setup.num_mixture_steps +
                                   setup.num_transformation_steps)
        if setup.verbose >= 2:
            print('Number of inner loop steps = %d.' % setup.num_sub_steps)
        setup.fix_mixture = args.fix_mixture
        setup.fix_transformations = args.fix_transformations
        setup.std_range = args.std_range
        setup.initial_mixture_file = args.mixture
        setup.initial_transformations_file = args.transformations

        setup.num_parallel = args.parallel

        return setup
