package com.sjkj.appthreefloor_tsgz.activity;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gyf.immersionbar.ImmersionBar;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.window.EasyWindow;
import com.hjq.window.draggable.SpringBackDraggable;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.activity.viewModel.ResureDetailsViewModel;
import com.sjkj.appthreefloor_tsgz.adapter.ImagResourceDetailsAdapter;
import com.sjkj.appthreefloor_tsgz.app.App;
import com.sjkj.appthreefloor_tsgz.bean.ImageBean;
import com.sjkj.appthreefloor_tsgz.bean.ResureDetailsBean;
import com.sjkj.appthreefloor_tsgz.databinding.ActivityResureDetailsBinding;
import com.sjkj.appthreefloor_tsgz.net.ResureDetailsRequest;
import com.sjkj.appthreefloor_tsgz.utils.Method;
import com.sjkj.base_lib.app.AppManager;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.net.domain.interactor.ProcessErrorSubscriber;
import com.sjkj.base_lib.utils.LocationUtils;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ResourceDetailsActivity extends BaseActivity {
    ActivityResureDetailsBinding binding;
    ResureDetailsViewModel viewModel;
    private ResureDetailsRequest resureDetailsRequest = new ResureDetailsRequest(App.getInstance());
    private ImagResourceDetailsAdapter adapter;

    @Override
    protected int initLayout() {
        return R.layout.activity_resure_details;
    }

    @Override
    public void initBinding() {
        binding = (ActivityResureDetailsBinding) getViewDataBinding();
        viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ResureDetailsViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
    }

    /**
     * 显示全局弹窗
     */
    public void showGlobalWindow() {
        SpringBackDraggable springBackDraggable = new SpringBackDraggable(SpringBackDraggable.ORIENTATION_HORIZONTAL);
        springBackDraggable.setAllowMoveToScreenNotch(false);
        EasyWindow.with(ResourceDetailsActivity.this)
                .setContentView(R.layout.window_daohang)
                .setGravity(Gravity.END | Gravity.BOTTOM)
                .setYOffset(500)
                .setXOffset(10)
                // 设置指定的拖拽规则
                .setDraggable(springBackDraggable)
                .setOnClickListener(android.R.id.icon, new EasyWindow.OnClickListener<ImageView>() {

                    @Override
                    public void onClick(EasyWindow<?> easyWindow, ImageView view) {
                        // 点击后跳转到拨打电话界面
                        // Intent intent = new Intent(Intent.ACTION_DIAL);
                        // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        // toast.startActivity(intent);
                        // 安卓 10 在后台跳转 Activity 需要额外适配
                        // https://developer.android.google.cn/about/versions/10/privacy/changes#background-activity-starts
                        if (viewModel.longitude.get() != 0 && viewModel.latitude.get() != 0) {
                            LocationUtils.getInstance().mapNav(viewModel.latitude.get(), viewModel.longitude.get(), "");
                        }

                    }
                })
                .show();
    }

    @Override
    public void initView() {

        super.initView();
        getTitleView().setText(getIntent().getStringExtra("tagValeText"));
        ImmersionBar.with(ResourceDetailsActivity.this)
                .statusBarDarkFont(true, 0.2f)
                .statusBarColor(R.color.white)
                .init();
        getTitleView().setTextColor(getResources().getColor(R.color.black));
        int drawableLeft = R.drawable.icon_back; // 替换为你的drawable资源ID
        Drawable drawable = ContextCompat.getDrawable(ResourceDetailsActivity.this, drawableLeft);
        if (drawable != null) {
            getBackView().setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null); // 使用Relative版本以支持RTL布局方向
        }
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.white));

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.VERTICAL);
        binding.recyclerView.setLayoutManager(manager);
        adapter = new ImagResourceDetailsAdapter(this);
        binding.recyclerView.setAdapter(adapter);
        showGlobalWindow();


    }

    private void CallPhone(String phone) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.show("暂无联系方式");
            return;
        }

        XXPermissions.with(AppManager.getAppManager().currentActivity())
                .permission(Permission.CALL_PHONE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            Method.getPhoneNumber(ResourceDetailsActivity.this, phone);
                        } else {
                            ToastUtils.show("获取部分权限成功，但部分权限未正常授予");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ToastUtils.show("被永久拒绝授权，请手动授予权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(AppManager.getAppManager().currentActivity(), permissions);
                        } else {
                            ToastUtils.show("获取权限失败");
                        }
                    }
                });

    }

    @Override
    public void initListener() {
        super.initListener();

        binding.tvCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CallPhone(viewModel.phone.get());
            }
        });
        binding.rlCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CallPhone(viewModel.phone.get());
            }
        });
        binding.ivCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CallPhone(viewModel.phone.get());
            }
        });
    }

    @Override
    public void loadData() {
        super.loadData();
        Params params = new Params();
//        params.put("tag", getIntent().getStringExtra("tag"));
//        params.put("id", getIntent().getStringExtra("id"));
        params.put("tag", "waterintake");
        params.put("id", "1c6d2c6bb5a84077be00f1d96396c2b0");
        resureDetailsRequest.setParams(params);
        resureDetailsRequest.execute(new ProcessErrorSubscriber<ResureDetailsBean>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onNext(ResureDetailsBean bean) {
                super.onNext(bean);
                if (bean.getDetail() != null) {
                    viewModel.latitude.set(bean.getDetail().getLatitude());
                    viewModel.longitude.set(bean.getDetail().getLongitude());
                    viewModel.name.set(bean.getDetail().getName());
                    viewModel.deptName.set(bean.getDetail().getDeptName());
                    viewModel.deptNameAdm.set(bean.getDetail().getDeptNameAdm());
                    viewModel.contact.set(bean.getDetail().getContact());
                    viewModel.phone.set(bean.getDetail().getPhone());
                    if (null != bean.getDetail().getUrl() && !TextUtils.isEmpty(bean.getDetail().getUrl())) {
                        List<String> list = Arrays.asList(bean.getDetail().getUrl().split(","));
                        List<ImageBean> imageBeanList = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            ImageBean imageBean = new ImageBean();
                            imageBean.setRealUrl(list.get(i));
                            imageBeanList.add(imageBean);

                        }
                        adapter.refreshList(imageBeanList);
                    }
                }

            }
        });
    }
}
