"""Wizard for creating an Aster study case on linear static plus
"""
"""
    created by Takeru who belongs
    to Japanese Localization Subcommittee in JSWG (Japan Salome-Meca Working Group)
    on Mar 12 2014
"""

import aster_s.wizards.common as WC
from aster_s.wizards.common import (
    Modelisation,
    GRP_MA,
    GRP_NO,
    Mode3D,
    PlaneStress,
    PlaneStrain,
    AxisSymmetric,
    YoungModulus,
    PoissonRatio,
    DplFromName,
    )


class BoundConds(WC.ConstraintSection):
    """Boundary conditions for mechanical constraints
    """

    def add_to(self, mech_consts, writer):
        """Add to mechanical constraints"""
        self.write_section("DDL_IMPO", mech_consts, writer)

class GrpLoad(WC.ArgsConstraint):
    """A group of axis load on elements
    """
#    def __init__(self, name, fx_val, fy_val, fz_val):
    def __init__(self, name, vals, field_name):
        inputList = []
        if field_name == "vertex-loading":
            print field_name
            inputList.append(WC.Arg("GROUP_NO", WC.quote(name)))
        else:
            inputList.append(WC.Arg("GROUP_MA", WC.quote(name)))
        if vals[0] != 0:
            if vals[0] != None:
                inputList.append(WC.Arg("FX", vals[0]))
            
        if vals[1] != 0:
            if vals[1] != None:
                inputList.append(WC.Arg("FY", vals[1]))
            
        if len(vals) > 2:
            if vals[2] != 0:
                if vals[2] != None:
                    inputList.append(WC.Arg("FZ", vals[2]))
                
        #WC.ArgsConstraint.__init__(self, [
        #    WC.Arg("GROUP_MA", WC.quote(name)),
        #    WC.Arg("FX", fx_val),
        #    WC.Arg("FY", fy_val),
        #    WC.Arg("FZ", fz_val),
        #])
        WC.ArgsConstraint.__init__(self, inputList)
        self.name = name

    def add_to(self, axisloads, writer):
        """Add to mechanical constraints"""
        WC.ArgsConstraint.add_to(self, axisloads, writer)
        axisloads.add_to_mesh_change(self.name)


class GrpLoad2(WC.ArgsConstraint):
    """A group of axis load on elements
    """
    def __init__(self, name, fx_val, fy_val):
        WC.ArgsConstraint.__init__(self, [
            WC.Arg("GROUP_MA", WC.quote(name)),
            WC.Arg("FX", fx_val),
            WC.Arg("FY", fy_val),
        ])
        #WC.ArgsConstraint.__init__(self, inputList)
        self.name = name

    def add_to(self, axisloads, writer):
        """Add to mechanical constraints"""
        WC.ArgsConstraint.add_to(self, axisloads, writer)
        axisloads.add_to_mesh_change(self.name)


class GrpLoad3(WC.ArgsConstraint):
    """A group of axis load on elements
    """
    def __init__(self, name, fx_val, fy_val, fz_val):
        WC.ArgsConstraint.__init__(self, [
            WC.Arg("GROUP_MA", WC.quote(name)),
            WC.Arg("FX", fx_val),
            WC.Arg("FY", fy_val),
            WC.Arg("FZ", fy_val),
        ])
        #WC.ArgsConstraint.__init__(self, inputList)
        self.name = name

    def add_to(self, axisloads, writer):
        """Add to mechanical constraints"""
        WC.ArgsConstraint.add_to(self, axisloads, writer)
        axisloads.add_to_mesh_change(self.name)


class GrpPres(WC.ArgsConstraint):
    """A group of pressure on elements
    """
    
    def __init__(self, name, value):
        WC.ArgsConstraint.__init__(self, [
            WC.Arg("GROUP_MA", WC.quote(name)),
            WC.Arg("PRES", value),
        ])
        self.name = name

    def add_to(self, pressures, writer):
        """Add to mechanical constraints"""
        WC.ArgsConstraint.add_to(self, pressures, writer)
        pressures.add_to_mesh_change(self.name)


class LoadVertex(WC.ConstraintSection):
    """Pressure constraints
    """

    def __init__(self):
        WC.ConstraintSection.__init__(self)
        self._grp_names_for_mesh = []
        self.lines = None
        self.model_give_dim = 0

    def add_to_mesh_change(self, name):
        """Add a group name to mesh change"""
        self._grp_names_for_mesh.append(name)

    def _write_mesh_change(self, writer):
        """Write the mesh change"""
        bloc = ""
        grps = self._grp_names_for_mesh
        model = writer.get(Modelisation)
        self.model_give_dim = model.give_dim()
        #if grps and model: 
        #    lines = WC.Lines()
        #    cmd = "MAIL=MODI_MAILLAGE("
        #    lines.add(cmd + "reuse=MAIL,")
        #    lines.init_idt = " " * len(cmd)
        #    lines.add("MAILLAGE=MAIL,")
        #    opt = "ORIE_PEAU_%sD=_F(GROUP_MA=%s,),"
        #    #lines.add(opt % (model.give_dim(), tuple(grps)))
        #    lines.add(opt % (self.model_give_dim, tuple(grps)))
        #    lines.add(");")
        #    bloc = lines.build_part()
        writer.subs("mesh_change_key", bloc)

    def add_to(self, mech_consts, writer):
        """Add to mechanical constraints"""
        self.lines = mech_consts.lines
        self.write_section("FORCE_NODALE", self, writer)
        self._write_mesh_change(writer)


class LoadEdge(WC.ConstraintSection):
    """Pressure constraints
    """

    def __init__(self):
        WC.ConstraintSection.__init__(self)
        self._grp_names_for_mesh = []
        self.lines = None
        self.model_give_dim = 0

    def add_to_mesh_change(self, name):
        """Add a group name to mesh change"""
        self._grp_names_for_mesh.append(name)

    def _write_mesh_change(self, writer):
        """Write the mesh change"""
        bloc = ""
        grps = self._grp_names_for_mesh
        model = writer.get(Modelisation)
        self.model_give_dim = model.give_dim()
        #if grps and model: 
        #    lines = WC.Lines()
        #    cmd = "MAIL=MODI_MAILLAGE("
        #    lines.add(cmd + "reuse=MAIL,")
        #    lines.init_idt = " " * len(cmd)
        #    lines.add("MAILLAGE=MAIL,")
        #    opt = "ORIE_PEAU_%sD=_F(GROUP_MA=%s,),"
        #    #lines.add(opt % (model.give_dim(), tuple(grps)))
        #    lines.add(opt % (self.model_give_dim, tuple(grps)))
        #    lines.add(");")
        #    bloc = lines.build_part()
        writer.subs("mesh_change_key", bloc)

    def add_to(self, mech_consts, writer):
        """Add to mechanical constraints"""
        self.lines = mech_consts.lines
        model = writer.get(Modelisation)
        self.model_give_dim = model.give_dim()
        print "self.model_give_dim = %i\n" % self.model_give_dim
        if self.model_give_dim == 2:
            self.write_section("FORCE_CONTOUR", self, writer)
        else:
            self.write_section("FORCE_ARETE", self, writer)
        self._write_mesh_change(writer)


class LoadFace(WC.ConstraintSection):
    """Pressure constraints
    """

    def __init__(self):
        WC.ConstraintSection.__init__(self)
        self._grp_names_for_mesh = []
        self.lines = None
        self.model_give_dim = 0

    def add_to_mesh_change(self, name):
        """Add a group name to mesh change"""
        self._grp_names_for_mesh.append(name)

    def _write_mesh_change(self, writer):
        """Write the mesh change"""
        bloc = ""
        grps = self._grp_names_for_mesh
        model = writer.get(Modelisation)
        self.model_give_dim = model.give_dim()
        #if grps and model: 
        #    lines = WC.Lines()
        #    cmd = "MAIL=MODI_MAILLAGE("
        #    lines.add(cmd + "reuse=MAIL,")
        #    lines.init_idt = " " * len(cmd)
        #    lines.add("MAILLAGE=MAIL,")
        #    opt = "ORIE_PEAU_%sD=_F(GROUP_MA=%s,),"
        #    #lines.add(opt % (model.give_dim(), tuple(grps)))
        #    lines.add(opt % (self.model_give_dim, tuple(grps)))
        #    lines.add(");")
        #    bloc = lines.build_part()
        writer.subs("mesh_change_key", bloc)

    def add_to(self, mech_consts, writer):
        """Add to mechanical constraints"""
        self.lines = mech_consts.lines
        self.write_section("FORCE_FACE", self, writer)
        self._write_mesh_change(writer)


class Pressure(WC.ConstraintSection):
    """Pressure constraints
    """

    def __init__(self):
        WC.ConstraintSection.__init__(self)
        self._grp_names_for_mesh = []
        self.lines = None

    def add_to_mesh_change(self, name):
        """Add a group name to mesh change"""
        self._grp_names_for_mesh.append(name)

    def _write_mesh_change(self, writer):
        """Write the mesh change"""
        bloc = ""
        grps = self._grp_names_for_mesh
        model = writer.get(Modelisation)
        if grps and model: 
            lines = WC.Lines()
            cmd = "MAIL=MODI_MAILLAGE("
            lines.add(cmd + "reuse=MAIL,")
            lines.init_idt = " " * len(cmd)
            lines.add("MAILLAGE=MAIL,")
            opt = "ORIE_PEAU_%sD=_F(GROUP_MA=%s,),"
            lines.add(opt % (model.give_dim(), tuple(grps)))
            lines.add(");")
            bloc = lines.build_part()
        writer.subs("mesh_change_key", bloc)

    def add_to(self, mech_consts, writer):
        """Add to mechanical constraints"""
        self.lines = mech_consts.lines
        self.write_section("PRES_REP", self, writer)
        self._write_mesh_change(writer)


class MechConstraints(WC.Constraints):
    """The part on mechanical constraints
    """
    pattern_key = "mechanical_constraints_key"

    def add_to(self, writer):
        """Add the mechanical constraints"""
        self.write_cmd("CHAR=AFFE_CHAR_MECA(", "MODELE=MODE,", writer)


class CommWriter(WC.CommWriter):
    """The Aster study case on linear static
    """
    _pattern = WC.load_pattern("linear_static_pattern.comm")


