package com.casic.dcms.view.bridge;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.LatLonPoint;
import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivityBridgeBinding;
import com.casic.dcms.model.ActionResultBean;
import com.casic.dcms.model.BridgeConfigBean;
import com.casic.dcms.utils.LocationHelper;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.utils.callback.IAddressListener;
import com.casic.dcms.utils.callback.ILocationListener;
import com.casic.dcms.vm.BridgeViewModel;
import com.casic.dcms.widgets.SignInDialog;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.LongKit;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.LoadingDialog;
import com.pengxh.androidx.lite.utils.SaveKeyValues;
import com.pengxh.androidx.lite.widget.TitleBarView;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet.BottomListSheetBuilder;

import java.util.List;

public class BridgeMaintainActivity extends AndroidxBaseActivity<ActivityBridgeBinding> {

    private static final String TAG = "BridgeMaintainActivity";
    private final Context context = this;
    private BridgeViewModel bridgeViewModel;
    private String bridgeId = "";

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);
        binding.titleView.setOnClickListener(new TitleBarView.OnClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
                ContextKit.navigatePageTo(context, MaintainRecordActivity.class);
            }
        });
    }

    @Override
    protected void observeRequestState() {
        bridgeViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (loadState == LoadState.Loading) {
                    LoadingDialog.show(BridgeMaintainActivity.this, "养护记录签到中，请稍后");
                } else {
                    LoadingDialog.dismiss();
                }
            }
        });
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        bridgeViewModel = new ViewModelProvider(this).get(BridgeViewModel.class);
        bridgeViewModel.submitResultModel.observe(this, new Observer<ActionResultBean>() {
            @Override
            public void onChanged(ActionResultBean resultBean) {
                String maintainAddress = (String) SaveKeyValues.getValue("maintainAddress", "");
                if (resultBean.getCode() == 200) {
                    new SignInDialog.Builder()
                            .setContext(context)
                            .setTitle("桥梁养护记录提交成功")
                            .setAddress(maintainAddress.equals("") ? "详细位置解析失败" : maintainAddress)
                            .setOnDialogClickListener(new SignInDialog.OnDialogClickListener() {
                                @Override
                                public void onViewClick() {
                                    ContextKit.navigatePageTo(context, MaintainRecordActivity.class);
                                }
                            }).build().show();
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        binding.bridgeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<BridgeConfigBean.BridgesBean> bridges = StringHelper.getBridgeBeanList();
                if (bridges.size() == 0) {
                    StringKit.show(context, "获取桥梁数据异常");
                    return;
                }
                BottomListSheetBuilder sheetBuilder = new BottomListSheetBuilder(context);
                sheetBuilder.setTitle("选择桥梁");
                for (BridgeConfigBean.BridgesBean bean : bridges) {
                    sheetBuilder.addItem(bean.getName());
                }
                sheetBuilder.setGravityCenter(true).setOnSheetItemClickListener(new BottomListSheetBuilder.OnSheetItemClickListener() {
                    @Override
                    public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                        dialog.dismiss();
                        bridgeId = String.valueOf(bridges.get(position).getId());
                        binding.bridgeNameView.setText(tag);
                        binding.bridgeNameView.setTextColor(ContextCompat.getColor(context, R.color.mainTextColor));
                    }
                }).build().show();
            }
        });

        binding.maintainContentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new BottomListSheetBuilder(context)
                        .setTitle("养护内容")
                        .addItem("日常养护，无异常")
                        .addItem("清除表面污垢")
                        .addItem("修补混凝土空洞、破损、剥落和表面风化及裂缝")
                        .addItem("清除暴露钢筋的锈渍、恢复保护层")
                        .addItem("处理各种横、纵向构件的开裂、开焊和锈蚀")
                        .setGravityCenter(true)
                        .setOnSheetItemClickListener(new BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                                dialog.dismiss();
                                binding.maintainView.setText(tag);
                                binding.maintainView.setTextColor(ContextCompat.getColor(context, R.color.mainTextColor));
                            }
                        }).build().show();
            }
        });

        String systemDate = LongKit.timestampToDate(System.currentTimeMillis());
        binding.maintainDateView.setText(systemDate);
        binding.liablePersonView.setText(StringHelper.getUserBean().getAccount());

        binding.submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LocationHelper.get().getCurrentLocation(context, new ILocationListener() {

                    @Override
                    public void onAMapLocationGet(AMapLocation aMapLocation) {
                        if (TextUtils.isEmpty(bridgeId)) {
                            StringKit.show(context, "请先选择养护的桥梁");
                            return;
                        }
                        String maintainContent = binding.maintainView.getText().toString();
                        if (TextUtils.isEmpty(maintainContent)) {
                            StringKit.show(context, "请选择养护的内容");
                            return;
                        }
                        LatLonPoint latLonPoint = new LatLonPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude());
                        LocationHelper.get().antiCodingLocation(context, latLonPoint, new IAddressListener() {
                            @Override
                            public void onGetAddress(String address) {
                                Log.d(TAG, "onGetAddress: " + address);
                                SaveKeyValues.putValue("maintainAddress", address);

                                //提交养护记录
                                bridgeViewModel.submitMaintain(
                                        context, bridgeId,
                                        maintainContent,
                                        binding.maintainDateView.getText().toString(),
                                        StringHelper.getUserBean().getId(),
                                        String.valueOf(aMapLocation.getLongitude()),
                                        String.valueOf(aMapLocation.getLatitude()),
                                        binding.remarksView.getText().toString()
                                );
                            }
                        });
                    }
                }, true);
            }
        });
    }
}