package com.ycgis.macall.personalcenter.v.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.base.BaseViewBindFragment;
import com.rs.macall.androidx.basemodel.callback.CustomClickListener;
import com.rs.macall.androidx.basemodel.callback.SimpleListClickListener;
import com.rs.macall.androidx.basemodel.utils.ToastUtil;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.FragmentMyBinding;
import com.ycgis.macall.personalcenter.m.adapterbean.AppBean;
import com.ycgis.macall.personalcenter.m.adapterbean.MyItem;
import com.ycgis.macall.personalcenter.p.app.AppCache;
import com.ycgis.macall.personalcenter.p.app.RuanseeApplication;
import com.ycgis.macall.personalcenter.p.callback.ImageCallBack;
import com.ycgis.macall.personalcenter.p.request.LoadPhotoImage;
import com.ycgis.macall.personalcenter.v.activity.AboutActivity;
import com.ycgis.macall.personalcenter.v.activity.ApplicationGuideActivity;
import com.ycgis.macall.personalcenter.v.activity.AuthenticationActivity;
import com.ycgis.macall.personalcenter.v.activity.BluetoothActivity;
import com.ycgis.macall.personalcenter.v.activity.FrequentlyQuestionsActivity;
import com.ycgis.macall.personalcenter.v.activity.MyDetailsActivity;
import com.ycgis.macall.personalcenter.v.activity.OpinionsSuggestionsActivity;
import com.ycgis.macall.personalcenter.v.adapter.HomeAppListAdapter;
import com.ycgis.macall.personalcenter.v.adapter.MyItemAdapter;

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

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyFragment#newInstance} factory method to
 * create an instance of this fragment.
 * 常见问题 Frequently Asked Questions
 */
public class MyFragment extends BaseViewBindFragment<FragmentMyBinding> {
    private MyItemAdapter adapter;
//    private HomeAppListAdapter commonAdapter;

    public MyFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MyFragment newInstance() {
        return  new MyFragment();
    }

    @Override
    protected FragmentMyBinding getViewBinding() {
        return FragmentMyBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {

    }

    @Override
    protected void initView(View view) {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 3);
//        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int itemViewType = adapter.getItemViewType(position);
                if (itemViewType == 1){
                    return 1;
                }else {
                    return 3;
                }
            }
        });
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        viewBinding.myRvFunction.setLayoutManager(manager);
//        viewBinding.myRvCommon.setLayoutManager(manager1);
    }

    @Override
    protected void startView(View view) {

    }

    @Override
    public void onResume() {
        super.onResume();
        getFunctionData();
    }

    public void getPhotoImage() {
        viewBinding.myIvPolicePhoto.setImageDrawable(getDrawables(RuanseeApplication.getUserData().getErrorPhotoByIdCardNum()));
        Bitmap bitmap =  new LoadPhotoImage().loadPhotoImage(RuanseeApplication.getUserData().getPhoto(), viewBinding.myIvPolicePhoto, new ImageCallBack() {
            @Override
            public void imageLoad(ImageView imageView, Bitmap bitmap) {
//                viewBinding.myIvPolicePhoto.setImageBitmap(bitmap);
                if (getActivity().isDestroyed())return;
                Glide.with(getContext())
                        .load(bitmap)
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(viewBinding.myIvPolicePhoto);
            }

            @Override
            public void imageLoadError(ImageView imageView, String message) {
//                ToastUtil.centered(getContext(),message);
//                viewBinding.myIvPolicePhoto.setImageDrawable(getDrawables(RuanseeApplication.getUserData().getErrorPhotoByIdCardNum()));
            }
        });

        if (bitmap != null){
//            viewBinding.myIvPolicePhoto.setImageBitmap(bitmap);
            Glide.with(getContext())
                    .load(bitmap)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(viewBinding.myIvPolicePhoto);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        if (resultCode == MyDetailsActivity.UPDATE_INFO){
            setUserInfoUI();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 显示用户信息
     */
    private void setUserInfoUI() {
        viewBinding.myTvPoliceName.setText("姓名："+RuanseeApplication.getUserData().getUserName());
        viewBinding.myTvUserCode.setText(RuanseeApplication.getUserData().getUserCode());
        viewBinding.myTvDept.setText("单位："+RuanseeApplication.getUserData().getDeptName());
//        viewBinding.myTvPosition.setText("职务："+RuanseeApplication.getUserData().getPolice_post());
        viewBinding.myTvPhone.setText("工作号码："+RuanseeApplication.getUserData().getPhone1());
        getPhotoImage();
    }

    private void getFunctionData() {
        setUserInfoUI();
        List<MyItem<BaseViewBindActivity>> dataList = new ArrayList<>();
        MyItem bean = new MyItem();
        bean.setBaseType(1);
        bean.setTitle("应用指南");
        bean.setIcon(R.drawable.icon_operation_guide);
        bean.setActivityClass(ApplicationGuideActivity.class);
        dataList.add(bean);
        bean = new MyItem();
        bean.setBaseType(1);
        bean.setTitle("意见反馈");
        bean.setIcon(R.drawable.icon_feedback);
        bean.setActivityClass(OpinionsSuggestionsActivity.class);
        dataList.add(bean);
        bean = new MyItem();
        bean.setBaseType(1);
        bean.setTitle("常见问题");
        bean.setIcon(R.drawable.icon_problen);
        bean.setActivityClass(FrequentlyQuestionsActivity.class);
        dataList.add(bean);

        bean = new MyItem();
        bean.setBaseType(5);
        dataList.add(bean);

        MyItem item1 = new MyItem();
        item1.setIcon(R.drawable.icon_authen);
        item1.setTitle("认证管理");
        item1.setActivityClass(AuthenticationActivity.class);
        dataList.add(item1);

        item1 = new MyItem();
        item1.setActivityClass(AboutActivity.class);
        item1.setIcon(R.drawable.icon_about);
        item1.setTitle("关于");
        dataList.add(item1);

//        item1 = new MyItem();
//        item1.setActivityClass(BluetoothActivity.class);
//        item1.setIcon(R.drawable.icon_about);
//        item1.setTitle("蓝牙调试");
//        dataList.add(item1);

        initData(dataList);
    }

//    private void initCommonData(List<AppBean> commonList) {
//        if (commonAdapter == null){
//            commonAdapter = new HomeAppListAdapter(getContext(),commonList);
//            viewBinding.myRvCommon.setAdapter(commonAdapter);
//            commonAdapter.setListClickListener(new SimpleListClickListener() {
//                @Override
//                public void onSingleClick(View v, int pointer) {
//                    if (commonAdapter.getItemViewType(pointer)!=1)return;
//                    AppBean dataItem = commonAdapter.getDataItem(pointer);
//                    if (dataItem.getActivityClass() == null){
//                        ToastUtil.centered(getContext(),"正在紧急开发中！");
//                        return;
//                    }
//                    startActivity(new Intent(getContext(),dataItem.getActivityClass()));
//                }
//            });
//        }else {
//            commonAdapter.upData(commonList);
//        }
//    }

    private void initData(List<MyItem<BaseViewBindActivity>> dataList) {
        if (adapter == null) {
            adapter = new MyItemAdapter(getContext(), dataList);
            adapter.setListClickListener(new SimpleListClickListener() {
                @Override
                public void onSingleClick(View v, int pointer) {
                    MyItem<BaseViewBindActivity> dataItem = adapter.getDataItem(pointer);
                    if (dataItem.getActivityClass() == null) {
                        ToastUtil.centered(getContext(), "正在紧急开发中！");
                        return;
                    }
                    startActivity(new Intent(getContext(),dataItem.getActivityClass()));
                }
            });
            viewBinding.myRvFunction.setAdapter(adapter);
        } else {
            adapter.upData(dataList);
        }
    }
}