Untitled

 avatar
unknown
kotlin
3 years ago
21 kB
15
Indexable
package com.treniti.fishtools.fragment.mapper

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import com.google.gson.GsonBuilder
import com.treniti.fishtools.CastConverterDialog
import com.treniti.fishtools.R
import com.treniti.fishtools.activity.MapperActivity
import com.treniti.fishtools.entity.converter.ConverterEntity
import com.treniti.fishtools.entity.mapper.BottomFeature
import com.treniti.fishtools.entity.mapper.MapCellEntity
import com.treniti.fishtools.entity.mapper.MapEntity
import com.treniti.fishtools.viewmodel.MapViewModel
import kotlin.math.cos


class CreateMarkerCastStep3Fragment(
    private val data: MutableList<MapCellEntity>,
    private val lastRetrieve: Int,
    private val converter: ConverterEntity,
    private val direction: Int,
    private val pole: String,
    private val mapID: String
) : Fragment() {

    private lateinit var lastRetrieveValTextView: TextView
    private lateinit var countValTextView: TextView
    private lateinit var depthValTextView: TextView
    private lateinit var maxDepthValTextView: TextView
    private lateinit var distanceValTextView: TextView
    private lateinit var converterTextView: TextView

    private var minDepth = 0.0
    private var maxDepth = 0.0

    private var totalRetrieve = 0

    private lateinit var btnConverter: ImageView

    private var currentDistance = -1
    private var cDistance = -1

    private lateinit var btnBack: ImageView

    private lateinit var btnSave: LinearLayout

    private lateinit var mapViewModel: MapViewModel
    private var mainList: MutableList<MutableList<MapCellEntity>> = mutableListOf()

    private lateinit var myMap: MapEntity

    private var mCenter: Int = -1

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_create_marker_cast_step3, container, false)
        initView(view)

        data.removeLast()

        converterTextView.text = converter.converterTitle


        mapViewModel = ViewModelProvider(this).get(MapViewModel::class.java)

        mapViewModel.getMap(mapID).observe(viewLifecycleOwner, { data ->
            myMap = data
            mCenter = myMap.currentDegree
            getListFromJson()


        })


        for (item in data) {
            if (item.converterCount.toInt() != -1) {
                totalRetrieve += item.converterCount.toInt()
            }

            if (item.depthExactValue != null) {

                if (minDepth == 0.0) {
                    minDepth = item.depthExactValue!!
                }

                if (maxDepth == 0.0) {
                    maxDepth = item.depthExactValue!!
                }

                if (item.depthExactValue!! < minDepth) {
                    minDepth = item.depthExactValue!!
                }

                if (item.depthExactValue!! > maxDepth) {
                    maxDepth = item.depthExactValue!!
                }
            }
        }

        totalRetrieve += lastRetrieve

        currentDistance = ((converter.totalValue * totalRetrieve.toDouble()) / 100).toInt()
        cDistance = ((converter.totalValue * totalRetrieve.toDouble()) / 100).toInt()
        data[0].converterCount = totalRetrieve.toDouble()
        data[0].distance = currentDistance
        distanceValTextView.text =
            "${((converter.totalValue * totalRetrieve.toDouble()) / 100).toInt()}${requireContext().getString(R.string.value_m)}"

        depthValTextView.text = minDepth.toString()
        maxDepthValTextView.text = maxDepth.toString()

        lastRetrieveValTextView.text = lastRetrieve.toString()
        countValTextView.text = data.size.toString()

        btnConverter.setOnClickListener {

            val ddialog = CastConverterDialog(
                requireContext(), requireFragmentManager(), (context as MapperActivity),
                converter, totalRetrieve.toDouble(), viewLifecycleOwner
            )
            ddialog.initDialog()
        }

        btnBack.setOnClickListener {
            requireFragmentManager().popBackStack()
        }

        btnSave.setOnClickListener {

            data.forEachIndexed { index, mapCellEntity ->
                if (index != 0){
                  val d = ((converter.totalValue * mapCellEntity.converterCount) / 100).toInt()
                    cDistance -= d
                    mapCellEntity.distance = cDistance
                }
            }


            for (marker in data) {
                val distance = ((converter.totalValue * marker.converterCount) / 100).toInt()
                var depth = 0.0

                if (marker.depthExactValue != null){
                    depth = marker.depthExactValue!!
                }

                getDotPosition(marker.distance!!, depth, marker.bottomFeature)
            }

            requireFragmentManager().popBackStack(
                "MapFragment",
                FragmentManager.POP_BACK_STACK_INCLUSIVE
            )
            requireFragmentManager().popBackStack(
                "CreateMarkerCastStep1Fragment",
                FragmentManager.POP_BACK_STACK_INCLUSIVE
            )
            requireFragmentManager().popBackStack(
                "CreateMarkerCastStep2Fragment",
                FragmentManager.POP_BACK_STACK_INCLUSIVE
            )
            requireFragmentManager().popBackStack(
                "CreateMarkerCastStep3Fragment",
                FragmentManager.POP_BACK_STACK_INCLUSIVE
            )
            requireFragmentManager().beginTransaction()
                .setCustomAnimations(
                    R.anim.slide_in,
                    R.anim.fade_out,
                    R.anim.fade_in,
                    R.anim.slide_out
                )
                .addToBackStack("MapFragment")
                .replace(R.id.mapperFrameLayout, MapFragment(mapID)).commit()
        }

        return view
    }



    private fun getDotPosition(distance: Int, depth: Double, bottomFeature: BottomFeature?) {
        val mapCenter = mCenter
        var mapMin = mapCenter - 90
        var mapMax = mapCenter + 90

        if (mapMin < 0) {
            mapMin += 360
        }

        if (mapMax > 360) {
            mapMax -= 360
        }


        var newDotDegree = -1
        if (direction < mapCenter) {
            newDotDegree = mapCenter - direction
        } else {
            newDotDegree = (mapMax - mapCenter) - (mapMax - direction)
        }

        val a = cos(Math.toRadians(newDotDegree.toDouble()))
        val positionVertical = distance * a


        val r = 180 - (90 + newDotDegree)
        val c = cos(Math.toRadians(r.toDouble()))
        var positionHorizontal = 0.0


        var count = -1
        var position = -1

        if (direction < mapCenter) {
            count = mainList[0].size / 2
            positionHorizontal = distance * c
            position = (count - positionHorizontal).toInt()
        } else {
            count = mainList[0].size
            positionHorizontal = distance * c
            val aaa = count - positionHorizontal.toInt()
            if (aaa > positionHorizontal){
                position = aaa
            } else {
                position = positionHorizontal.toInt()
            }

        }


        val data = addDepths(
            position,
            distance,
            depth,
            mainList,
            positionVertical.toInt(),
            bottomFeature
        )
        if (data != null) {

            val gson = GsonBuilder()
                .disableHtmlEscaping()
                .create()
            myMap.map = gson.toJson(data.asReversed())
        }

        mapViewModel.update(myMap)
    }

    private fun getListFromJson() {
        mainList.clear()
        val gson = GsonBuilder()
            .disableHtmlEscaping()
            .create()

        val list = gson.fromJson(myMap.map!!, Array<Array<MapCellEntity>>::class.java)
        val newList: MutableList<MutableList<MapCellEntity>> = mutableListOf()
        for (item in list) {
            val ii: MutableList<MapCellEntity> = mutableListOf()
            for (itm in item) {
                ii.add(itm)
            }
            newList.add(ii)
        }

        mainList.addAll(newList)
    }

    private fun addDepths(
        dotPositionHorizontal: Int,
        ddd: Int,
        depth: Double,
        data: MutableList<MutableList<MapCellEntity>>,
        positionVertical: Int,
        bottomFeature: BottomFeature?
    ): MutableList<MutableList<MapCellEntity>>? {
        var distance = ddd
        var countNewCellsVertical = 0
        var countNewCellsHorizontal = 0
        val countCellsVertical = data.size
        val countCellsHorizontal = data[0].size

        val needCells = (positionVertical + ((ddd / 100.0) * 10))
        var needResizeHor = false
        val ab = dotPositionHorizontal - ((ddd / 100.0) * 10)
        val bc = (dotPositionHorizontal + ((ddd / 100.0) * 10))

        if (ab < 0) {
            needResizeHor = true
        }

        if (bc > countCellsHorizontal - 1) {
            needResizeHor = true
        }

        Log.d("TestNeedCellsHor", ab.toString())
        Log.d("TestNeedCellsHor", bc.toString())

        if (needCells > countCellsVertical) {
            countNewCellsVertical =
                ((positionVertical + (ddd / 100.0) * 10) - countCellsVertical).toInt()
        }




        Log.d("TestSizeMap", "NeedCells: $needCells")
        Log.d("TestSizeMap", "ddd: $ddd")
        Log.d("TestSizeMap", "PositionVertical: $positionVertical")
        Log.d("TestSizeMap", "CountCellsVertical: $countCellsVertical")
        Log.d("TestSizeMap", "CountNewCellsVertical: $countNewCellsVertical")

        if (needResizeHor) {
            if (dotPositionHorizontal - ((ddd / 100.0) * 10).toInt() < 0) {
                val a = countCellsHorizontal - dotPositionHorizontal + ((ddd / 100.0) * 10).toInt()
                countNewCellsHorizontal = a - countCellsHorizontal
            }

            if (dotPositionHorizontal + ((ddd / 100.0) * 10).toInt() > countCellsHorizontal - 1) {
                val a = countCellsHorizontal + dotPositionHorizontal + ((ddd / 100.0) * 10).toInt()
                countNewCellsHorizontal = a - countCellsHorizontal
            }

        }


        Log.d("TestHorizontalSize", "CountNewCellsHorizontal: $countNewCellsHorizontal")
        Log.d("TestHorizontalSize", "DotPositionHorizontal: $dotPositionHorizontal")


        if (countNewCellsVertical != 0) {
            for (i in 0..countNewCellsVertical) {
                Log.d("TestSizeMap", "ArrayItterable: $i")
                val dataList: MutableList<MapCellEntity> = mutableListOf()
                for (ih in 0 until data[0].size) {
                    dataList.add(MapCellEntity(mutableListOf(), null, false, null, 0, "", null))
                }
                data.add(dataList)
            }
        }


        if (countNewCellsHorizontal != 0) {
            for (i in data) {
                for (ih in 0 until countNewCellsHorizontal) {
                    i.add(0, MapCellEntity(mutableListOf(), null, false, null, 0, "", null))
                    i.add(MapCellEntity(mutableListOf(), null, false, null, 0, "", null))
                }
            }
        }

        Log.d("TestSizeMap", "DataNewSize: ${data.size}")
        Log.d("TestSizeMap", "DataNewHorizontalSize: ${data[0].size}")



        distance = positionVertical - 1
        Log.d("adadgkglfd", distance.toString())
        val reversedData = data.asReversed()
        reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].bottomFeature =
            bottomFeature
        if (depth != 0.0) {
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].isExactValue =
                true
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].depthExactValue =
                depth
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].direction =
                direction
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].distance = ddd
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].pole = pole
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].converter =
                converter
            reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].converterCount =
                currentDistance.toDouble()

            val childArray: MutableList<String> = mutableListOf()
            val countOfCell = (ddd * 0.10).toInt()


            if (dotPositionHorizontal - countOfCell > -1 || dotPositionHorizontal + countOfCell <= data[0].size || distance - countOfCell > -1 || distance + countOfCell <= data.size) {

                if (ddd >= 30) {
                    val minPositionHorizontal =
                        (dotPositionHorizontal + countNewCellsHorizontal) - countOfCell
                    val maxPositionHorizontal =
                        dotPositionHorizontal + countOfCell + countNewCellsHorizontal
                    val minPositionVertical = distance - countOfCell
                    val maxPositionVertical = distance + countOfCell
                    Log.d("qwerty", minPositionHorizontal.toString())
                    Log.d("qwerty", maxPositionHorizontal.toString())
                    var currentPositionVertical = maxPositionVertical
                    var currentPositionHorizontal = maxPositionHorizontal


                    currentPositionVertical = positionVertical - 1
                    currentPositionHorizontal = minPositionHorizontal

                    while (currentPositionHorizontal < (dotPositionHorizontal + countNewCellsHorizontal) && currentPositionVertical < maxPositionVertical) {
                        var pos = currentPositionHorizontal
                        while (pos <= (dotPositionHorizontal + countNewCellsHorizontal)) {
                            reversedData[currentPositionVertical][pos].depth.add(depth)
                            var size = reversedData[currentPositionVertical][pos].depth.size
                            if (size > 1) {
                                Log.d("TestDot", "1")
                            }
                            childArray.add(reversedData[currentPositionVertical][pos].cellID)
                            pos++
                        }

                        currentPositionHorizontal++
                        currentPositionVertical++
                    }

                    currentPositionVertical = positionVertical - 1
                    currentPositionHorizontal = maxPositionHorizontal

                    while (currentPositionHorizontal > (dotPositionHorizontal + countNewCellsHorizontal) && currentPositionVertical < maxPositionVertical) {
                        var pos = currentPositionHorizontal
                        while (pos > (dotPositionHorizontal + countNewCellsHorizontal)) {
                            reversedData[currentPositionVertical][pos].depth.add(depth)
                            var size = reversedData[currentPositionVertical][pos].depth.size
                            if (size > 1) {
                                Log.d("TestDot", "2")
                            }
                            childArray.add(reversedData[currentPositionVertical][pos].cellID)
                            pos -= 1
                        }

                        currentPositionHorizontal -= 1
                        currentPositionVertical++
                    }

                    //wdas

                    currentPositionVertical = positionVertical - 1
                    currentPositionHorizontal = minPositionHorizontal

                    while (currentPositionHorizontal < (dotPositionHorizontal + countNewCellsHorizontal) && currentPositionVertical > minPositionVertical) {
                        var pos = currentPositionHorizontal
                        if (currentPositionVertical != positionVertical - 1) {
                            while (pos <= (dotPositionHorizontal + countNewCellsHorizontal)) {
                                reversedData[currentPositionVertical][pos].depth.add(depth)
                                var size = reversedData[currentPositionVertical][pos].depth.size
                                if (size > 1) {
                                    Log.d("TestDot", "3")
                                }
                                childArray.add(reversedData[currentPositionVertical][pos].cellID)
                                pos++
                            }
                        }


                        currentPositionHorizontal++
                        currentPositionVertical -= 1
                    }

                    currentPositionVertical = positionVertical - 1
                    currentPositionHorizontal = maxPositionHorizontal

                    while (currentPositionHorizontal > (dotPositionHorizontal + countNewCellsHorizontal) && currentPositionVertical > minPositionVertical) {
                        var pos = currentPositionHorizontal
                        if (currentPositionVertical != positionVertical - 1) {
                            while (pos > (dotPositionHorizontal + countNewCellsHorizontal)) {
                                reversedData[currentPositionVertical][pos].depth.add(depth)
                                var size = reversedData[currentPositionVertical][pos].depth.size
                                if (size > 1) {
                                    Log.d("TestDot", "4")
                                }
                                childArray.add(reversedData[currentPositionVertical][pos].cellID)
                                pos -= 1
                            }
                        }


                        currentPositionHorizontal -= 1
                        currentPositionVertical -= 1
                    }
                } else {

                    val minPositionHorizontal =
                        (dotPositionHorizontal + countNewCellsHorizontal) - countOfCell
                    val maxPositionHorizontal =
                        dotPositionHorizontal + countOfCell + countNewCellsHorizontal


                    val minPositionVertical = distance - countOfCell
                    val maxPositionVertical = distance + countOfCell

                    var currentPositionVertical = maxPositionVertical
                    var currentPositionHorizontal = maxPositionHorizontal


                    currentPositionVertical = minPositionVertical
                    currentPositionHorizontal = minPositionHorizontal

                    while (currentPositionVertical <= maxPositionVertical) {
                        while (currentPositionHorizontal <= maxPositionHorizontal) {
                            reversedData[currentPositionVertical][currentPositionHorizontal].depth.add(
                                depth
                            )
                            childArray.add(reversedData[currentPositionVertical][currentPositionHorizontal].cellID)
                            currentPositionHorizontal++
                        }
                        currentPositionHorizontal = minPositionHorizontal
                        currentPositionVertical++
                    }
                }

                reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].childCells.clear()
                reversedData[distance][(dotPositionHorizontal + countNewCellsHorizontal)].childCells.addAll(
                    childArray
                )

                return reversedData
            } else {
                Toast.makeText(requireContext(), "Err", Toast.LENGTH_SHORT).show()
                return null
            }
        } else {
            return reversedData
        }



    }

    private fun initView(view: View) {
        converterTextView = view.findViewById(R.id.converterTextView)
        btnSave = view.findViewById(R.id.btnSave)
        btnBack = view.findViewById(R.id.btnBack)
        btnConverter = view.findViewById(R.id.btnConverter)
        lastRetrieveValTextView = view.findViewById(R.id.lastRetrieveValTextView)
        countValTextView = view.findViewById(R.id.countValTextView)
        depthValTextView = view.findViewById(R.id.depthValTextView)
        maxDepthValTextView = view.findViewById(R.id.maxDepthValTextView)
        distanceValTextView = view.findViewById(R.id.distanceValTextView)
    }
}
Editor is loading...