lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
package com.kuanzhai.driver.ui.main
 
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import cn.sinata.xldutils.utils.*
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.AMap
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.Polyline
import com.kuanzhai.driver.R
import com.kuanzhai.driver.base.BaseEvent
import com.kuanzhai.driver.base.MyApplication
import com.kuanzhai.driver.base.MyBaseActivity
import com.kuanzhai.driver.base.gaode.AMapKit
import com.kuanzhai.driver.base.gaode.AMapKit.addMarker
import com.kuanzhai.driver.base.gaode.AMapKit.getIcon
import com.kuanzhai.driver.base.gaode.AMapKit.getTimeType
import com.kuanzhai.driver.base.gaode.AMapKit.initMap
import com.kuanzhai.driver.base.gaode.AMapKit.moveCamera
import com.kuanzhai.driver.base.gaode.gpsnav.GPSNaviActivity
import com.kuanzhai.driver.bean.OrderBean
import com.kuanzhai.driver.bean.OrderSimpleData
import com.kuanzhai.driver.bean.RessignBean
import com.kuanzhai.driver.netUtls.*
import com.kuanzhai.driver.ui.DialogUtil
import com.kuanzhai.driver.ui.main.major.MajorSureMoneyActivity
import com.kuanzhai.driver.utils.DateUtil
import com.kuanzhai.driver.utils.MyUtils
import com.google.gson.Gson
import com.trello.rxlifecycle3.android.ActivityEvent
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_trip.*
import kotlinx.android.synthetic.main.dialog_select_pay_type.view.*
import kotlinx.android.synthetic.main.include_order_top.*
import kotlinx.android.synthetic.main.item_map_market.view.*
import kotlinx.android.synthetic.main.view_no.view.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.util.concurrent.TimeUnit
 
 
class TripActivity : MyBaseActivity() {
 
    /***
     * //    流程操作状态(3=开始出发预约点,4=到达预约点,5=开始服务,6=服务结束)
     * 页面一共4个marker 1:预约地点 2:起点 3终点 4:司机点
     */
    val orderType by lazy {
        intent.getStringExtra("orderType")
    }
 
    val orderId by lazy {
        intent.getStringExtra("orderId")
    }
    var isTimeStart = false //限制计时器
 
    var makeMarker: Marker? = null
    var startMarker: Marker? = null
    var endMarker: Marker? = null
    var carMarker: Marker? = null
    var markerLine: Polyline? = null
    lateinit var aMap: AMap
    lateinit var orderBean: OrderBean
 
 
    override fun setContentView() {
        setContentView(R.layout.activity_trip)
    }
 
    override fun initView() {
        setTitleText("准备出发-去接客户-等待客户-服务中")
        tv_Right.text = "申请改派"
        callOrder()
        initMineLocation()
        onclick()
        addSockectCancel()
        MyApplication.getInstance()!!.initUpPoint()
    }
 
    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        super.onPermissionsDenied(requestCode, perms)
        if (requestCode == 1006){
            onBackPressed()
        }
    }
 
    private fun addSockectCancel() {
        checkOrderStatue()
        MyApplication.addOrderView(object : MyApplication.OrderStatueView {
            override fun orderInfo(data: OrderSimpleData?) {
                data?.let {
                    when(it.orderType){
                        1,2,3 -> {
                            if (it.status == 10 || it.status == 12) {
                                Handler(Looper.getMainLooper()).post {
                                    toast("用户已取消订单")
                                    MyApplication.getTTsManager().setVideoText("用户已取消订单")
                                    MyApplication.currentOrderType = ""
                                    MyApplication.currentOrderId = ""
                                    finish()
                                }
                            }
                        }
                    }
                }
            }
        })
    }
 
    var handler: Handler? = null
    fun checkOrderStatue() {
        var map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        callNet(false, Api.orderStateSocket, map) {
            val orderSimpleData =
                Gson().fromJson(
                    it,
                    OrderSimpleData::class.java
                )
            orderSimpleData?.let {
                if (it.status == 10 || it.status == 12) {
                    Handler(Looper.getMainLooper()).post {
                        toast("用户已取消订单")
                        MyApplication.getTTsManager().setVideoText("用户已取消订单")
                        MyApplication.currentOrderType = ""
                        MyApplication.currentOrderId = ""
                        finish()
                    }
                }
            }
 
        }
        handler?.postDelayed({ checkOrderStatue() }, 3000)
    }
 
 
    override fun onBackPressed() {
        EventBus.getDefault().post(BaseEvent(BaseEvent.UPDATA_MAIN_CAR))
        super.onBackPressed()
    }
 
    override fun finish() {
        EventBus.getDefault().post(BaseEvent(BaseEvent.UPDATA_MAIN_CAR))
        super.finish()
    }
 
    private fun onclick() {
        tv_Right.clickDelay {
            var map = getMapByAny()
            map["orderId"] = orderId
            map["orderType"] = orderType
            callNet(Api.queryReassignMoney, map) {
                val data = gson.fromJson<RessignBean>(it, RessignBean::class.java)
                if (data.data.amount.isNullOrEmpty()) {
                    startActivity<ReassignActivity>(
                        "orderId" to orderId,
                        "orderType" to orderType
                    )
                } else {
                    DialogUtil.getDelAndSureDialog(
                        this,
                        "现在改派将收取" + data.data.amount + "元改派费,您是否要进行改派?",
                        {},
                        {
                            //跳转改派页面
                            startActivity<ReassignActivity>(
                                "orderId" to orderId,
                                "orderType" to orderType,
                                "money" to data.data.amount.toString()
                            )
                        })
                }
 
 
            }
        }
        iv_move.setOnClickListener {
            orderBean.let {
                when (it.data.orderState) {
                    2, 3 -> {
                        moveCamera(aMap, makeMarker!!.position, carMarker!!.position)
                    }
                    4 -> {
                        moveCamera(aMap, carMarker!!.position)
                    }
                    5, 6 -> {
                        if (endMarker == null){
                            moveCamera(aMap, carMarker!!.position)
                        }else{
                            moveCamera(aMap, carMarker!!.position, endMarker!!.position)
                        }
                    }
                }
            }
        }
 
        iv_police.clickDelay {
            orderBean.let {
                MyUtils.getInstans().CallPhone(this, it.data.emergencyCall)
            }
        }
        iv_to_gd.clickDelay {
            orderBean.let {
                when (it.data.orderState) {
                    2, 3 -> {
                        AMapKit.startNavi(carMarker!!.position.latitude,carMarker!!.position.longitude
                                ,makeMarker!!.position.latitude,makeMarker!!.position.longitude,this)
 
                    }
 
                    4 -> {
                        showToast("没有终点,无法导航")
                    }
 
                    5, 6 -> {
                        if (endMarker == null){
                            toast("暂无终点")
                        }else{
                            AMapKit.startNavi(carMarker!!.position.latitude,carMarker!!.position.longitude
                                    ,endMarker!!.position.latitude,endMarker!!.position.longitude,this)
                        }
 
                    }
                    else -> {
 
                    }
                }
            }
        }
 
        slide_btn.onSwipeListener = {
            orderBean.let {
                when (it.data.orderState) {
                    2, 3, 4 -> {
                        var timeByLo = (DateUtil.dateToStamp(
                            DateUtil.TYPE0,
                            it.data.travelTime
                        ) - System.currentTimeMillis()) / 1000L
                        if (it.data.orderState == 2 && it.data.reservation == 2 && timeByLo > 1800L) {//判断预约单时间滑动是否有效
                            toast("只能在预约时间的半小时内出行")
                            return@let
                        }
                        it.data.orderState++
                        callStatue(it.data.orderState,{
                            showStatueMapUI(it)
                        }) {
                            it.data.orderState--
                        }
                    }
                    5, 6 -> {
                        it.data.orderState = 6
                        when(orderType){
                            "2" -> {
                                showMoneyTypeDialog()
                            }
                            "1" -> {
                                callStatue(6,{
                                    startActivity<MajorSureMoneyActivity>(
                                        "orderId" to orderId,
                                        "orderType" to orderType
                                    )
                                    finish()
                                }) {
 
                                }
                            }
                        }
                    }
                }
            }
        }
 
        iv_phone.clickDelay {
            orderBean.let {
                Utils.callPhone(this, it.data.phone)
            }
        }
    }
 
    private fun callStatue(i: Int, click: () -> Unit, clickFail: () -> Unit) {
        var map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        map["state"] = i
        map["lat"] = MyApplication.getLocation().latitude
        map["lon"] = MyApplication.getLocation().longitude
        callNet(Api.process, map,{
            click()
        }) {
            clickFail()
        }
    }
 
 
    private fun showMoneyTypeDialog() {
        var pop = DialogUtil.getPopupwindow(this, R.layout.dialog_select_pay_type)
        var payType = 1 //支付方式(1=OK平台收款,2=其他方式收款)
        pop.showDown(window.decorView)
        pop.contentView.tv_cancel.setOnClickListener {
            pop.dismiss()
        }
        pop.contentView.tv_ok.setOnClickListener {
            pop.dismiss()
            if (payType == 1) {
                callStatue(6,{
                    startActivity<FillOutActivity>("orderId" to orderId, "orderType" to orderType)
                    finish()
                }) {
 
                }
            } else {
                DialogUtil.getDelAndSureDialog(this, "请确定是否收款?", {}, {
                    pop.dismiss()
                    val map = getMapByAny()
                    map["orderId"] = orderId
                    map["orderType"] = orderType
                    map["type"] = payType
                    callStatue(6,{
                        callNet(Api.confirmFees, map) {
                            finish()
                            startActivity<OrderOverActivity>(
                                "orderId" to orderId,
                                "orderType" to orderType
                            )
                        }
                    }) {
 
                    }
                })
            }
        }
        pop.contentView.tv_select_ok.setOnClickListener {
            pop.contentView.tv_select_ok.alpha = 1f
            pop.contentView.tv_select_ok.textSize = 16f
            pop.contentView.tv_select_or.alpha = 0.6f
            pop.contentView.tv_select_or.textSize = 14f
            payType = 1
        }
        pop.contentView.tv_select_or.setOnClickListener {
            pop.contentView.tv_select_ok.alpha = 0.6f
            pop.contentView.tv_select_ok.textSize = 14f
            pop.contentView.tv_select_or.alpha = 1f
            pop.contentView.tv_select_or.textSize = 16f
            payType = 2
        }
    }
 
 
    /***
     * 定位时司机与线路的改变
     */
    private fun initMineLocation() {
        AMapKit.initLocation(this, AMapLocationListener {
            it?.let {
                MyApplication.aMapLocation = it
                carMarker!!.position = LatLng(it.latitude, it.longitude)
            }
            orderBean.let {
                when (it.data.orderState) {
                    2, 3 -> {
                        changeCarThree(false)
                    }
                    5, 6 -> {
                        changeCarFive(false)
                    }
                }
            }
        })
    }
 
    private fun changeCarThree(isMove: Boolean) {
        if (makeMarker != null && carMarker != null) {
            AMapKit.initRouteLine(
                this,
                makeMarker!!.position,
                carMarker!!.position,
                object : (MutableList<LatLng>, Float, Long) -> Unit {
                    override fun invoke(p1: MutableList<LatLng>, p2: Float, p3: Long) {
                        var view = creatCarView("距预约点" + AMapKit.getTance(p2))
                        carMarker!!.setIcon(getIcon(view))
                        addline(p1)
                        if (isMove)
                            moveCamera(aMap, makeMarker!!.position, carMarker!!.position)
                    }
                })
        }
    }
 
    private fun changeCarFive(isMove: Boolean) {
        if (carMarker != null && endMarker != null) {
            AMapKit.initRouteLine(
                this,
                carMarker!!.position,
                endMarker!!.position,
                object : (MutableList<LatLng>, Float, Long) -> Unit {
                    override fun invoke(p1: MutableList<LatLng>, p2: Float, p3: Long) {
                        addline(p1)
                        var view =
                            creatCarView("剩余" + AMapKit.getTance(p2) + "\n" + "预计还需" + p3.toInt() + "分钟")
                        carMarker!!.setIcon(getIcon(view))
                        if (isMove) {
                            moveCamera(aMap, carMarker!!.position, endMarker!!.position)
                        }
                    }
                })
        }else{
            var view =
                creatCarView()
            carMarker?.setIcon(getIcon(view))
            if (isMove) {
                moveCamera(aMap, carMarker!!.position)
            }
        }
    }
 
    private fun callOrder() {
        MyApplication.currentOrderId = orderId
        MyApplication.currentOrderType = orderType
        var map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        callNet(Api.queryOrderInfo, map) {
            orderBean = gson.fromJson(it, OrderBean::class.java)
            showUi(orderBean)
        }
    }
 
    private fun showUi(orderBean: OrderBean?) {
        orderBean?.let {
            tv_name.text = orderBean.data.nickName
            tv_car_num.text = it.data.historyNum.toString() + "次乘车"
            tv_time.text = it.data.travelTime_
            tv_start_address.text = it.data.startAddress
            tv_end_address.text = it.data.endAddress
            tv_end_address.visibility = if (it.data.endAddress.isNullOrEmpty()) View.GONE else View.VISIBLE
            if (orderType == "1"){
                tv_red_money.gone()
            }
            tv_red_money.text = it.data.tipMoney.toString() + "元红包"
            showStatueMapUI(it)
        }
    }
 
    private fun getAllMark(it: OrderBean) {
        if (makeMarker == null) {
            val view = createView(R.layout.item_map_market_make, this)
            view.iv_img.setImageResource(R.mipmap.trip_yuyue)
            makeMarker = addMarker(aMap, it.data.startLat, it.data.startLon, view, "")
        }
        makeMarker!!.isVisible = false
        if (startMarker == null) {
            val view = createView(R.layout.item_map_market, this)
            view.iv_img.setImageResource(R.mipmap.starting_point)
            startMarker = addMarker(aMap, it.data.startLat, it.data.startLon, view, "")
        }
        startMarker!!.isVisible = false
        if (endMarker == null) {
            val view = createView(R.layout.item_map_market, this)
            view.iv_img.setImageResource(R.mipmap.end_point)
            endMarker = addMarker(aMap, it.data.endLat, it.data.endLon, view, "")
        }
        endMarker?.isVisible = false
        if (carMarker == null) {
            val carView = creatCarView("")
            carMarker = addMarker(
                aMap,
                MyApplication.getLocation().latitude,
                MyApplication.getLocation().longitude,
                carView,
                ""
            )
        }
        carMarker!!.isVisible = false
        if (markerLine != null) {
            markerLine!!.isVisible = false
        }
    }
 
    fun creatCarView(content: String): View {
        val carView = createView(R.layout.item_map_market, this)
        carView.iv_img.setImageResource(R.mipmap.car_trip)
        carView.tv_hint.visible()
        carView.tv_hint.text = content
        return carView
    }
 
    fun creatCarView(): View {
        val carView = createView(R.layout.item_map_market, this)
        carView.iv_img.setImageResource(R.mipmap.car_trip)
        carView.tv_hint.gone()
        return carView
    }
 
 
 
    fun creatCarViewHint(content: String): View {
        val carView = createView(R.layout.item_map_market, this)
        carView.iv_img.setImageResource(R.mipmap.car_trip)
        carView.tv_hint.visible()
        carView.tv_hint.setColorBuild(this,content,R.color.main_yellow,4,content.length)
        return carView
    }
 
    //    流程操作状态(3=开始出发预约点,4=到达预约点,5=开始服务,6=服务结束)
    private fun showStatueMapUI(it: OrderBean) {
        getAllMark(it)
        tv_Right.visible()
        when (it.data.orderState) {
            2, 3 -> {
                if (it.data.reassign == 1){
                    tv_Right.gone()
                }
                iv_to_gd.visible()
                makeMarker?.isVisible = true
                carMarker?.isVisible = true
                changeCarThree(true)
                closeTimeStart()
                if (it.data.orderState == 2) {
                    slide_btn.changeButtonText("出发前往预约地点")
                    setTitleText("准备出发")
                } else {
                    slide_btn.changeButtonText("到达预约地点")
                    setTitleText("去接客户")
                }
            }
            4 -> {
                iv_to_gd.gone()
                if (it.data.reassign == 1){
                    tv_Right.gone()
                }
                closeLine()
                carMarker?.isVisible = true
                startTimeStart(
                    ((System.currentTimeMillis() - DateUtil.dateToStamp(
                        DateUtil.TYPE0,
                        it.data.waitTime
                    )) / 1000).toInt()
                )
                moveCamera(aMap, carMarker!!.position)
                setTitleText("等待客户")
                slide_btn.changeButtonText("乘客已上车")
            }
            5, 6 -> {
                closeTimeStart()
                iv_to_gd.visible()
                tv_Right.gone()
                startMarker?.isVisible = true
                endMarker?.isVisible = true
                carMarker?.isVisible = true
                changeCarFive(true)
                setTitleText("服务中")
                slide_btn.changeButtonText("送达该乘客")
            }
        }
    }
 
 
    private fun startTimeStart(waitTime: Int) {
        if (!isTimeStart) {
            isTimeStart = true
            refreshTime(waitTime) {
                var view = creatCarViewHint("您已等待" + getTimeType(it))
                carMarker!!.setIcon(getIcon(view))
            }
        }
    }
 
    private var timeDisposable: Disposable? = null
 
    private fun refreshTime(waitTime: Int, click: (num: Int) -> Unit) {
        if (null != timeDisposable && !timeDisposable!!.isDisposed) {
            timeDisposable!!.dispose()
        }
        timeDisposable = Observable
            .interval(1, TimeUnit.SECONDS)
            .take(100000000)
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(bindUntilEvent(ActivityEvent.DESTROY))
            .subscribe { aLong ->
                var num = waitTime + aLong
                click(num.toInt())
            }
    }
 
    private fun closeTimeStart() {
        if (timeDisposable != null) {
            timeDisposable!!.dispose()
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        if (handler != null) {
            handler?.removeCallbacksAndMessages(null)
        }
        closeTimeStart()
    }
 
 
    private fun addline(allLine: MutableList<LatLng>) {
        closeLine()
        markerLine = AMapKit.drawLine(this, aMap, allLine)
    }
 
    fun closeLine() {
        if (markerLine != null) {
            markerLine!!.remove()
        }
    }
 
 
    override fun setOnclick() {
 
    }
 
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        aMap = initMap(savedInstanceState, map_view)
    }
 
    override fun onEventMainThread(event: BaseEvent?) {
        super.onEventMainThread(event)
        when (event!!.code) {
            BaseEvent.REAGASSIGN_SUCCESS -> {
                MyApplication.currentOrderId = ""
                MyApplication.currentOrderType = ""
                finish()
            }
            BaseEvent.BACK_CAR_OVER -> {
                finish()
            }
        }
    }
}