!  Copyright (C) 2017 The authors of Py-ChemShell
!
!  This file is part of Py-ChemShell.
!
!  Py-ChemShell is free software: you can redistribute it and/or modify
!  it under the terms of the GNU Lesser General Public License as
!  published by the Free Software Foundation, either version 3 of the
!  License, or (at your option) any later version.
!
!  Py-ChemShell is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU Lesser General Public License for more details.
!
!  You should have received a copy of the GNU Lesser General Public
!  License along with Py-ChemShell.  If not, see
!  <http://www.gnu.org/licenses/>.

! YL: better call this module ClusterOptionsModule to store fragment and other information
module ClusterCreateModule

    use iso_c_binding
    use DL_PY2F

    private
    public init, finalise
    public get_cluster_size
    public PyFrag, PyCluster, PyOptions, PyCell, PyField, PyBQs, PyShells
!    public nneighbours, neighbours, neighb_vectors

    integer(kind=8)            :: ncores
!    integer(kind=8), pointer   :: nneighbours(:)
!    integer(kind=8), pointer   :: neighbours(:,:)
    integer(kind=8), parameter :: MAXCONN = 14
!    real(kind=8)   , pointer   :: neighb_vectors(:,:)
    type(dictType) , pointer   :: PyFrag, PyCluster, PyOptions, PyCell, PyField, PyBQs, PyShells


!    type, bind(c) :: frag_pass
        ! This type must match the frag_pass C struct on the python side
!        integer(c_int) :: nat
!        type(c_ptr)    :: ncores, nshells 
!    end type frag_pass
!    type :: fragment
     ! This is our Fortran fragment data structure
!        integer(c_int), dimension(:)  ,pointer  :: ncores
!        integer(c_int), dimension(:)  ,pointer  :: nshells
     !   real(c_double), dimension(:,:),pointer  :: pos
     !   real(c_double), dimension(:)  ,pointer  :: charge
!    end type fragment


    contains

    subroutine init(fragPtr, optionsPtr) bind(c)

        implicit none
        type(PyType)         , intent(in) :: fragPtr
        type(PyType)         , intent(in) :: optionsPtr
        type(PyType), pointer             :: cellPtr

        real(kind=8) :: floatbuff

        ! frag
        allocate(PyFrag, source=ptr2dict(fragPtr))

        ! options
        allocate(PyOptions, source=ptr2dict(optionsPtr))

        ! cell
        call PyFrag%get('cell', cellPtr)
        allocate(PyCell, source=ptr2dict(cellPtr))

        ! YL: didn't we forget to set ncores which is needed by set_neighbours()?
        call PyFrag%get('npts', ncores)

    endsubroutine init
    subroutine finalise() bind(c)

        implicit none

        deallocate(PyFrag, PyOptions, PyCell)

!        if(associated(nneighbours)) then
!            deallocate(nneighbours)
!        endif        
!        if(associated(neighbours)) then
!            deallocate(neighbours)
!        endif
!        if(associated(neighb_vectors)) then
!            deallocate(neighb_vectors)
!        endif
    
    endsubroutine finalise
    subroutine cluster_transfer_data()

        implicit none
        integer :: ierr
        print *, "### FORTRAN: cluster_transfer_data"
    endsubroutine cluster_transfer_data


endmodule clusterCreateModule
