package com.unis.sxzcl.ui.home;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.PieEntry;
import com.unis.model.DataCallback;
import com.unis.model.NoticeModel;
import com.unis.model.UrlPath;
import com.unis.model.UserModel;
import com.unis.model.bean.UserBean;
import com.unis.model.event.TodoEvent;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentHomeBinding;
import com.unis.sxzcl.widget.MyBarChart;
import com.unis.sxzcl.widget.MyPieChart;
import com.unis.sxzcl.widget.banner.BannerUtils;
import com.unis.sxzcl.widget.banner.TopLineAdapter;
import com.youth.banner.Banner;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

/**
 * 包名: com.unis.sxzcl.ui.home
 * 类名: HomeFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/2 19:55
 * 更新人:
 * 更新时间: 2020/12/2 19:55
 * 更新说明:
 * 版本号: 1.0
 */
public class HomeFragment extends BaseFragment<FragmentHomeBinding> {

    private MyPieChart mPieChartHouseStatus;
    private MyBarChart mBarChartFoodType;
    private MyPieChart mPieChartStorageStatus;

    @Override
    protected FragmentHomeBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentHomeBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        mBinding.vStatus.setHeight(getStatusHeight());

        initRefreshView(mBinding.refreshLayout, () -> {
            // TODO: 2020/11/10 刷新数据
            new Handler().postDelayed(() -> mBinding.refreshLayout.setRefreshing(false), 500);
            onTodoEvent(new TodoEvent());
        });

        mBinding.ivNotice.setOnClickListener(v -> {
            // 通知
            navigation(R.id.noticeListFragment);
        });
        mBinding.ivTodo.setOnClickListener(v -> {
            // 待办
            navigation(R.id.todoHomeFragment);
        });
        mBinding.ivUser.setOnClickListener(v -> {
            // 个人信息
            navigation(R.id.userFragment);
        });
//        mBinding.layoutCompany.setOnClickListener(v -> {
//            // 库列表
//            navigation(R.id.companyListFragment);
//        });
        mBinding.tvDepotCount.setOnClickListener(v -> {
            // 库列表
            navigation(R.id.companyListFragment);
        });
        mBinding.tvHouseCount.setOnClickListener(v -> {
            // 仓列表
            Bundle bundle = new Bundle();
            bundle.putBoolean("showTitle", true);
            navigation(R.id.houseListFragment, bundle);
        });
        mBinding.tvPersonCount.setOnClickListener(v -> {
            // 人员列表
            Bundle bundle = new Bundle();
            bundle.putBoolean("showTitle", true);
            navigation(R.id.personListFragment, bundle);
        });

        // 初始化轮播图
        initBanner();

        // 初始化标签
        initNotice();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onFirstShow() {
//        getRole();
        getHouseStatus();
        getFoodType();
        getStorageStatus();

        UserBean user = UserModel.get().getUser();
        if (user == null) return;
        mBinding.tvCompany.setText(user.getDepotName());
        Glide.with(getContext())
                .load(UrlPath.image_host + user.getPheadPortrait())
                .placeholder(R.drawable.ic_img_loading)
                .error(R.drawable.ic_user_empty)
                .into(mBinding.ivUser);

        onTodoEvent(new TodoEvent());

    }

    /**
     * 初始化轮播图
     */
    private void initBanner() {
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.ic_banner1);
        list.add(R.drawable.ic_banner1);
        list.add(R.drawable.ic_banner1);
        BannerUtils mBannerUtils = new BannerUtils(mBinding.banner);
        mBannerUtils.setImagesResId(list);
    }

    /**
     * 初始化标签
     */
    private void initNotice() {

        List<String> bannerData = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            bannerData.add("这是一条公告");
        }
        mBinding.announceBanner.setAdapter(new TopLineAdapter(bannerData))
                .isAutoLoop(true)
                .setOrientation(Banner.VERTICAL)
                .setUserInputEnabled(false)
//                .setLoopTime(2000)
                .setPageTransformer(new ZoomOutPageTransformer())
                .setOnBannerListener((data, position) -> {
                    // 公告
                    navigation(R.id.announceListFragment);
                })
        ;

    }


    /**
     * 获取工作台数据
     */
//    private void getRole() {
//        String role = UserModel.get().getRole();// 获取角色
//        if (role == null) return;
//        //获取工作台数据
//        switch (role) {
//            case UserModel.Role.TYPE_FGS:
//                getHouseStatus();
//                getFoodType();
//                getStorageStatus();
////                WorkbenchModel.get().getFgsList(workbenchCallback);
//                break;
//            case UserModel.Role.TYPE_KLD:
//                getHouseStatus();
//                getFoodType();
//                getStorageStatus();
////                WorkbenchModel.get().getKzList(workbenchCallback);
//                break;
//            case UserModel.Role.TYPE_BGY:
//                mBinding.layoutCompany.setVisibility(View.GONE);
//                mBinding.layoutCount.setVisibility(View.GONE);
//                mBinding.layoutHouseStatus.setVisibility(View.GONE);
//                mBinding.layoutFoodType.setVisibility(View.GONE);
//                mBinding.layoutStorageStatus.setVisibility(View.GONE);
//                mWorkbenchAdapter.addData(WorkbenchModel.get().mHomeList);
//                break;
//        }
//    }
    private void getHouseStatus() {
        if (mPieChartHouseStatus == null)
            mPieChartHouseStatus = new MyPieChart(mBinding.pieHouseStatus);
        // 仓房状态
        List<PieEntry> pieEntries = new ArrayList<>();
        pieEntries.add(new PieEntry(200f, "入库"));
        pieEntries.add(new PieEntry(290f, "出库"));
        pieEntries.add(new PieEntry(240f, "通风"));
        pieEntries.add(new PieEntry(160f, "熏蒸"));
        mPieChartHouseStatus.showChart(pieEntries, "仓房状态");
    }

    private void getFoodType() {
        if (mBarChartFoodType == null)
            mBarChartFoodType = new MyBarChart(mBinding.barFoodType);

        // 粮食品种
        ArrayList<BarEntry> barEntries = new ArrayList<>();
        barEntries.add(new BarEntry(0, 140));
        barEntries.add(new BarEntry(1, 120));
        barEntries.add(new BarEntry(2, 80));
        barEntries.add(new BarEntry(3, 50));
        List<String> workTypeLabels = Arrays.asList("玉米", "小麦", "大豆", "稻谷");
        mBarChartFoodType.showChart(workTypeLabels, barEntries);
    }

    private void getStorageStatus() {
        if (mPieChartStorageStatus == null)
            mPieChartStorageStatus = new MyPieChart(mBinding.pieStorageStatus);
        // 储存状态
        List<PieEntry> pieEntries = new ArrayList<>();
        pieEntries.add(new PieEntry(200f, "空仓"));
        pieEntries.add(new PieEntry(290f, "满仓"));
        pieEntries.add(new PieEntry(240f, "半仓"));
        mPieChartStorageStatus.showChart(pieEntries, "储存状态");
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTodoEvent(TodoEvent event) {
        // 待办事项数量
//        ProcessModel.get().getTodoSize(new DataCallback<Integer>() {
//            @Override
//            public void onSuccess(Integer data) {
//                if (!isActive())return;
//
//                if ((int) data == 0) {
//                    mBinding.tvTodoCount.setVisibility(View.GONE);
//                    return;
//                }
//                mBinding.tvTodoCount.setVisibility(View.VISIBLE);
//                mBinding.tvTodoCount.setText(data + "");
//            }
//        });

        // 通知数量
        NoticeModel.get().getNoticeSize(new DataCallback<Integer>() {
            @Override
            public void onSuccess(Integer data) {
                if (!isActive())return;

                if ((int) data == 0) {
                    mBinding.tvNoticeCount.setVisibility(View.GONE);
                    return;
                }
                mBinding.tvNoticeCount.setVisibility(View.VISIBLE);
                mBinding.tvNoticeCount.setText(data + "");
            }
        });
    }

}
