package com.rehome.sgbaxj.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.activity.sbxdj.MainSbxdjglActivity;
import com.rehome.sgbaxj.adapter.GridViewAdapter;
import com.rehome.sgbaxj.base.BaseFragment;
import com.rehome.sgbaxj.bean.BasicDataBean;
import com.rehome.sgbaxj.bean.GridViewBean;
import com.rehome.sgbaxj.contans.Contans;
import com.rehome.sgbaxj.service.PushService;
import com.rehome.sgbaxj.utils.AutoToolbar;
import com.rehome.sgbaxj.utils.SPUtils;

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

/**
 * Created by ruihong on 2018/4/2.
 */

public class MainFragment extends BaseFragment {

    private static MainFragment instance = null;
    AutoToolbar toolbar;
    GridView gv;

    private List<GridViewBean> beanList = new ArrayList<>();
    private GridViewAdapter adapter;
    private boolean isTask = false;//是否有任务
    private String[] str = {"点检",};
    private int[] imageId = {R.mipmap.icon8};
    private int[] colors = {R.drawable.radius_a1};

    private List<Integer> item;
    private List<String> dialogDatas;
    private List<BasicDataBean.DataBean> zys;

    public static MainFragment getInstance() {
        if (instance == null) {
            instance = new MainFragment();
        }
        return instance;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initView() {
        gv = view.findViewById(R.id.gv);
        toolbar = view.findViewById(R.id.toolbar);
    }

    public void initData() {


        zys = new ArrayList<>();
        dialogDatas = new ArrayList<>();
        item = new ArrayList<>();
        item.addAll(PermissionsResult());
        checkPush();

        beanList.clear();
        beanList.addAll(getGridViewData(isTask));

        gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        adapter = new GridViewAdapter(getActivity(), beanList, item, true);
        gv.setAdapter(adapter);

        gv.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent;
            switch (position) {
                case 0:
                    intent = new Intent(getActivity(), MainSbxdjglActivity.class);
                    startActivity(intent);
                    break;
                case 1:
            }
        });

    }



    private List<Integer> PermissionsResult() {

        List<Integer> item = new ArrayList<>();


        try {
            String result = (String) SPUtils.get(context, Contans.PERMISSIONSRESULT, "");

            if (!result.isEmpty()) {
                String[] results = result.split(";");
                item.add(0);
                item.add(1);
                item.add(2);
                item.add(3);
                item.add(4);

            }
        } catch (Exception e) {
        }
        return item;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    /**
     * @param isTask 是否有行为安全观察任务
     * @return
     */
    private List<GridViewBean> getGridViewData(boolean isTask) {

        List<GridViewBean> datas = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            GridViewBean bean = new GridViewBean();
            bean.setTitle(str[i]);
            bean.setBackgroup(colors[i]);
            bean.setImageid(imageId[i]);
            bean.setShow(false);
            datas.add(bean);
        }

        return datas;
    }

    //检查系统是否有任务推送
    private void checkPush() {

        int[] whats = new int[]{1, 0, 0, 0, 0};
        for (int i = 0; i < item.size(); i++) {
            if (item.get(i) == 0) {
                //点检
                whats[0] = 1;
            }
        }

        Intent intent = new Intent(getActivity(), PushService.class);
        Bundle bundle = new Bundle();
        bundle.putIntArray("what", whats);
        intent.putExtras(bundle);
        getActivity().startService(intent);

    }

    /**
     * 广播接收器
     */
    public class MsgReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            isTask = intent.getBooleanExtra("isTask", false);
            beanList.clear();
            beanList.addAll(getGridViewData(isTask));
            adapter.notifyDataSetChanged();
        }
    }
}
