package com.rehome.sgbaxj.activity.regular;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.Log;
import android.view.WindowManager;
import android.widget.GridView;
import com.rehome.sgbaxj.DBModel.Dqgz;
import com.rehome.sgbaxj.DBModel.DqgzList;
import com.rehome.sgbaxj.DBModel.ProfessionList;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.adapter.GridViewAdapter;
import com.rehome.sgbaxj.base.BaseActivity;
import com.rehome.sgbaxj.bean.DqgzRequestInfo;
import com.rehome.sgbaxj.bean.GridViewBean;
import com.rehome.sgbaxj.bean.XsRequestInfo;
import com.rehome.sgbaxj.contans.Contans;
import com.rehome.sgbaxj.utils.GsonUtils;
import com.rehome.sgbaxj.utils.HttpListener;
import com.rehome.sgbaxj.utils.NohttpUtils;
import com.rehome.sgbaxj.utils.SPUtils;
import com.rehome.sgbaxj.utils.StatusBarUtil;
import com.rehome.sgbaxj.DBModel.Profession;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * 巡视抄表管理
 */
public class RegularActivity extends BaseActivity {

    GridView gv;

    //    private String[] str = {"巡检任务", "巡检工作","燃料周期详情"};
//    private int[] imageId = {R.mipmap.icon_xs1, R.mipmap.icon6,R.mipmap.icon8};
//    private int[] colors = {R.drawable.radius_b1, R.drawable.radius_a4,R.drawable.radius_c1};
    private String[] str = {"定期任务", "定期工作"};
    private int[] imageId = {R.mipmap.icon_xs1, R.mipmap.icon6};
    private int[] colors = {R.drawable.radius_b1, R.drawable.radius_a4};
    private GridViewAdapter adapter;
    private String type = null;
    private  String username;
    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Contans.ACTION_REFRESHDOWNLOADPLAN.equals(action)) {
                //刷新页面
                requestDatas();
            }
        }
    };

    @Override
    public int getContentViewID() {
        return R.layout.activity_xscbgl;
    }

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

        Intent intentType = getIntent();
        type = intentType.getStringExtra("type");
        username = (String) SPUtils.get(context, Contans.USERID, "");
        if(type.equals("YX")){
            setTitle("运行定期");
            setTitle("运行定期");
        }else if(type.equals("RL")){
            setTitle("燃料定期");
            setTitle("燃料定期");
        }

        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        StatusBarUtil.transparencyBar(RegularActivity.this);
        mToolbar.setBackgroundColor(Color.parseColor("#00000000"));
        setBack();

        adapter = new GridViewAdapter(this, getGridViewData(), new ArrayList<Integer>(), true);
        gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        gv.setAdapter(adapter);
        gv.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent = null;
            switch (i) {
                case 0:
                    intent = new Intent(RegularActivity.this, DqgzUpAndDownActivity.class);
                    intent.putExtra("type",type);
                    break;
                case 1:
                    intent = new Intent(RegularActivity.this, DqgzActivity.class);
                    intent.putExtra("type",type);
                    break;
            }
            if (intent != null) {
                startActivity(intent);
            }
        });
        IntentFilter filter = new IntentFilter();
        filter.addAction(Contans.ACTION_REFRESHDOWNLOADPLAN);
        registerReceiver(myReceiver, filter);

        requestDatas();
        if(type.equals("YX")){
            getProfessionData();
        }
    }

    private List<GridViewBean> getGridViewData() {

        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 requestDatas() {    //获取定期任务列表数据

        Log.i("app",Contans.IP + Contans.DQGZ);
        Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.DQGZ, RequestMethod.POST);;
        requestxs.setDefineRequestBodyForJson(createJson());

        NohttpUtils.getInstance().add(this, 0, requestxs, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

                Log.i("app",response.get());
                DqgzList list = GsonUtils.GsonToBean(response.get(), DqgzList.class);
                Log.i("app",list.getMsg());
                Log.i("app",list.getState());
                Log.i("app",String.valueOf(list.getData().size()));

                if (list != null) {

                    if (list.getState().equals("1")) {

                        List<Dqgz> dqgzs = list.getData();//服务器数据

                        DataSupport.deleteAll(Dqgz.class, "download = 0 and username = ?",username);

                        for (Dqgz dqgz : dqgzs) {

                            List<Dqgz> dbDqgz = DataSupport.where("rwid = ? and download = ?  and username = ?", dqgz.getRWID(), "1",username).find(Dqgz.class);

                            if (dbDqgz.size() == 0) {
                                dqgz.setUsername(username);
                                dqgz.save();
                            }
                        }
                        //刷新计划下载界面
                        Intent intent = new Intent(Contans.ACTION_REFRESHPLANDOWNLOAD);
                        //发送广播,刷新任务下载页面
                        sendBroadcast(intent);
                    } else {
                        DataSupport.deleteAll(Dqgz.class, "download = 0 and username = ?",username);
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }

    //获取运行专业数据
    private void getProfessionData() {

        Log.i("app",Contans.IP + Contans.XSCB);
        final Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.XSCB, RequestMethod.POST);

        requestxs.setDefineRequestBodyForJson(createJsonZY());

        NohttpUtils.getInstance().add(this, 0, requestxs, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

                Log.i("app",response.get());
                ProfessionList list = GsonUtils.GsonToBean(response.get(), ProfessionList.class);
                Log.i("app",list.getMsg());
                Log.i("app",list.getState());
                Log.i("app",String.valueOf(list.getData().size()));

                if (list != null) {
                    //获取数据成功
                    if (list.getState().equals("1")) {
                        //服务器数据
                        List<Profession> professions = list.getData();
                        DataSupport.deleteAll(Profession.class);
                        for (Profession profession : professions) {
                            profession.save();
                        }
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }

    private String createJsonBA() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("BA_XJGL_RWLB_GET");
        String json = GsonUtils.GsonString(info);
        Log.i("app",json);
        return json;
    }
    private String createJson() {
        DqgzRequestInfo info = new DqgzRequestInfo();
        info.setAction("DQGZ_RWLB_GET");
        info.setRWZT(0);
        String bz = (String) SPUtils.get(context, Contans.BZBH, "");
//        if(bz!=null&&(!bz.equals(""))){
//            info.setBZ(bz);
//        }
        if(type!=null&&type.equals("YX")){
            info.setMK("YX");
        }
        if(type!=null&&type.equals("RL")){
            info.setMK("RL");
        }
        String json = GsonUtils.GsonString(info);
        Log.i("app",json);
        return json;
    }

    private String createJsonZY() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("YX_XJGL_ZY_GET");
        String jsonZY = GsonUtils.GsonString(info);
        return jsonZY;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }
}
