唐浩
1 天以前 3d631dbadf98f3f77c6e955cb36219217ef73ab5
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
package com.ziang.driver.ui.main
 
import android.os.Bundle
import android.os.CountDownTimer
import android.os.PersistableBundle
import android.view.View
import cn.sinata.xldutils.activitys.DialogActivity
import cn.sinata.xldutils.utils.Toast
import cn.sinata.xldutils.utils.clickDelay
import cn.sinata.xldutils.utils.doubleTwo
import cn.sinata.xldutils.utils.gone
import cn.sinata.xldutils.utils.sysErr
import cn.sinata.xldutils.utils.visible
import com.amap.api.maps.AMap
import com.amap.api.maps.model.LatLng
import com.google.gson.Gson
import com.ziang.driver.R
import com.ziang.driver.base.BaseEvent
import com.ziang.driver.base.MyApplication
import com.ziang.driver.base.gaode.AMapKit
import com.ziang.driver.bean.OrderBean
import com.ziang.driver.netUtls.Api
import com.ziang.driver.netUtls.callNet
import com.ziang.driver.netUtls.createView
import com.ziang.driver.netUtls.getMapByAny
import com.ziang.driver.netUtls.timeOver
import com.ziang.driver.ui.UiUtil
import kotlinx.android.synthetic.main.item_get_order.map_view
import kotlinx.android.synthetic.main.item_get_order.tv_car_type
import kotlinx.android.synthetic.main.item_get_order.tv_cargo_money
import kotlinx.android.synthetic.main.item_get_order.tv_cargo_num
import kotlinx.android.synthetic.main.item_get_order.tv_close
import kotlinx.android.synthetic.main.item_get_order.tv_end
import kotlinx.android.synthetic.main.item_get_order.tv_money
import kotlinx.android.synthetic.main.item_get_order.tv_name
import kotlinx.android.synthetic.main.item_get_order.tv_reassign
import kotlinx.android.synthetic.main.item_get_order.tv_remark
import kotlinx.android.synthetic.main.item_get_order.tv_start
import kotlinx.android.synthetic.main.item_get_order.tv_sure
import kotlinx.android.synthetic.main.item_get_order.tv_tance
import kotlinx.android.synthetic.main.item_get_order.tv_time
import kotlinx.android.synthetic.main.item_get_order.tv_title
import kotlinx.android.synthetic.main.item_map_market.view.iv_img
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.startActivity
 
class GetOrderFragment : DialogActivity() {
 
//    1.上班:您正在上班中 或者 xx出行,美好生活
//    2.下班:您已下班
//    3.收到订单:您收到新的【专车/出租车】【实时/预约】订单,距您xx公里,从xx出发,全程约xx公里
//    4.修改终点:请注意,乘客已修改终点
 
    val orderId by lazy {
        intent?.getStringExtra("orderId")
    }
    val orderType by lazy {
        intent?.getStringExtra("orderType")
    }
    val data by lazy {
        intent?.getStringExtra("data")
    }
 
    val timeDown by lazy {
        intent?.getIntExtra("time", -1)
    }
    val type by lazy {
        intent?.getIntExtra("type", -1)
    }
 
 
    lateinit var time: CountDownTimer
 
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //实例化语音引擎
        aMap = AMapKit.initMap(savedInstanceState, map_view)
        if (data.isNullOrEmpty())
            callOrderDetail()
        else
            showData(data)
        var strBtn = "抢单"
        if (type == 2) {
            strBtn = "确定"
        }
        time = timeOver((timeDown!!.toLong() * 1000L)) {
            if (it == 0) {
                finish()
            } else {
                tv_sure.text = strBtn + it.toString() + "S"
            }
        }
        tv_close.clickDelay {
            finish()
        }
        tv_sure.clickDelay {
            if (type == 1) {
                showGetOrder()
            } else if (type == 2) {
                onBackPressed()
                this.startActivity<TripActivity>("orderId" to orderId, "orderType" to orderType)
            }
 
        }
    }
 
    override fun initView() {
 
    }
 
    override fun onPause() {
        super.onPause()
        map_view.onPause()
    }
 
    override fun onResume() {
        super.onResume()
        map_view.onResume()
    }
 
    override fun onLowMemory() {
        super.onLowMemory()
        map_view.onLowMemory()
    }
 
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        EventBus.getDefault().register(this)
        map_view.onCreate(savedInstanceState)
    }
 
    override fun onDestroy() {
        super.onDestroy()
        time.cancel()
        MyApplication.getTTsManager().stopSpeaking()
        EventBus.getDefault().unregister(this)
        map_view.onDestroy()
    }
 
    override fun setContentLayout(): Int {
        return R.layout.item_get_order
    }
 
 
    private fun callOrderDetail() {
        var map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        map["lat"] = MyApplication.getLocation().latitude
        map["lon"] = MyApplication.getLocation().longitude
        callNet(this, Api.queryPushOrder, map) {
           showData(it)
        }
    }
 
    private fun showData(it:String?){
        val data = Gson().fromJson<OrderBean>(it, OrderBean::class.java).data
        sysErr(data)
        val startView = createView(R.layout.item_map_market, this)
        val endView = createView(R.layout.item_map_market, this)
        startView.iv_img.setImageResource(R.mipmap.starting_point)
        endView.iv_img.setImageResource(R.mipmap.end_point)
        AMapKit.addMarker(aMap, data.startLat, data.startLon, startView, "")
        AMapKit.addMarker(aMap, data.endLat, data.endLon, endView, "")
        tv_time.text = data.travelTime
        tv_car_type.text = UiUtil.getNameByType(orderType!!) + "订单"
        tv_remark.visibility = if (data.remark.isNullOrEmpty()) View.GONE else View.VISIBLE
        tv_end.visibility = if (data.endAddress.isNullOrEmpty()) View.GONE else View.VISIBLE
        tv_tance.visibility = if (data.endAddress.isNullOrEmpty()) View.GONE else View.VISIBLE
        if (!data.remark.isNullOrEmpty()) {
            tv_remark.text = data.remark
        }
        if (orderType == "1") {
            tv_money.gone()
        }
        if (orderType == "5" || orderType == "4") {
            tv_money.gone()
            tv_remark.gone()
            tv_name.visible()
            tv_cargo_num.visible()
            tv_cargo_money.visible()
            tv_name.text = data.nickName + "-" + data.phone
            var cargoStrType = if (data.cargoType == "1") "普通小件X" else "贵重物品X"
            tv_cargo_num.text = cargoStrType + data.peopleNumber
            if (data.tipMoney != null && data.tipMoney > 0.0) {
                tv_cargo_money.text = doubleTwo(data.tipMoney) + "元加急费"
            } else {
                tv_cargo_money.gone()
            }
        }
        tv_start.text = data.startAddress
        tv_end.text = data.endAddress
        tv_money.text = data.tipMoney.toString() + "元小费"
        tv_reassign.text = data.type
 
        if (data.endLat != null&&data.endLat != 0.0&& data.endLon != null&& data.endLon != 0.0) {
            AMapKit.drawLineAndMove(
                this,
                aMap,
                LatLng(data.startLat, data.startLon),
                LatLng(data.endLat, data.endLon), 100
            )
            AMapKit.initRouteLine(
                this,
                LatLng(data.startLat, data.startLon),
                LatLng(data.endLat, data.endLon),
                object : (MutableList<LatLng>, Float, Long) -> Unit {
                    override fun invoke(
                        latLngs: MutableList<LatLng>,
                        lineTance: Float,
                        lineTime: Long
                    ) {
                        tv_tance.text = "全程约" + AMapKit.getTance(lineTance)
                        MyApplication.getTTsManager().setVideoText(
                            "收到新的" + UiUtil.getNameByType(orderType!!) + "订单," + "从" + data.startAddress + "出发,全程约" + AMapKit.getTance(
                                lineTance
                            )
                        )
                    }
 
                })
        }else{
            AMapKit.moveCamera(aMap,LatLng(data.startLat, data.startLon))
            MyApplication.getTTsManager().setVideoText(
                "收到新的" + UiUtil.getNameByType(orderType!!) + "订单," + "从" + data.startAddress + "出发"
            )
        }
 
        AMapKit.initRouteLine(
            this,
            LatLng(MyApplication.getLocation().latitude, MyApplication.getLocation().longitude),
            LatLng(data.startLat, data.startLon),
            object : (MutableList<LatLng>, Float, Long) -> Unit {
                override fun invoke(
                    latLngs: MutableList<LatLng>,
                    lineTance: Float,
                    lineTime: Long
                ) {
                    tv_title.text = "距您约" + AMapKit.getTance(lineTance)
                }
            })
    }
 
    lateinit var aMap: AMap
 
    fun showGetOrder() {
        var map = getMapByAny()
        map["orderId"] = orderId
        map["orderType"] = orderType
        callNet(this, Api.grabOrder, map) {
            Toast.create(this).show("抢单成功")
            onBackPressed()
            when (orderType) {
                "2" -> {
                    this.startActivity<TripActivity>("orderId" to orderId, "orderType" to orderType)
                }
                "1" -> {
                    this.startActivity<TripActivity>("orderId" to orderId, "orderType" to orderType)
                }
                "3" -> {
 
                }
                "4", "5" -> {
 
                }
            }
        }
    }
 
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventMainThread(event: BaseEvent) {
        when (event.code) {
            BaseEvent.ORDER_GRAB_EVENT -> {
                if (orderId == event.msg)
                finish()
            }
        }
    }
 
 
}