package com.bjzc.bhzhgd.activity.sbxj;


import static com.bjzc.bhzhgd.utils.GsonUtils.GsonToBean;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bjzc.bhzhgd.DBModel.Profession;
import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.adapter.CommonAdapter;
import com.bjzc.bhzhgd.adapter.ViewHolder;
import com.bjzc.bhzhgd.base.BaseActivity3;
import com.bjzc.bhzhgd.bean.MyUploadCheckedBean;
import com.bjzc.bhzhgd.bean.StatisticsBean;
import com.bjzc.bhzhgd.bean.XsRequestInfo;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.utils.GsonUtils;
import com.bjzc.bhzhgd.utils.HttpListener;
import com.bjzc.bhzhgd.utils.NoProgresshttpUtils;
import com.bjzc.bhzhgd.utils.NohttpUtils;
import com.bjzc.bhzhgd.utils.RSAUtils;
import com.bjzc.bhzhgd.utils.SPUtils;
import com.bjzc.bhzhgd.weight.ListDialog;
import com.google.gson.Gson;
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.Arrays;
import java.util.List;


public class XJStatisticsActivity extends BaseActivity3 {

    ListView lv;
    TextView tvNodata;
    TextView tv_zc;
    TextView checked_count;
    TextView tv_total;
    TextView tv_hasCheck;
    TextView tv_ljds;
    TextView tv_hasCheck_rate;
    LinearLayout tv_my_upload;


    private View headView;
    private TextView tv_djrw;
    private CommonAdapter<StatisticsBean.DataBean> adapter;
    private List<MyUploadCheckedBean.DataBean> dataBeans = new ArrayList<>();
    private List<String> listPro = new ArrayList<>();
    private List<Profession> professionList = new ArrayList<>();
    ListDialog listDialog;

    //private ArrayList<XSJJHDataBean> xsjjhDataBeanArrayList = new ArrayList<>();//点检记录列表
    private String type = null;
    private String username;
    private String professionId = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_checked;
    }

    @Override
    public void initView() {
        lv = findViewById(R.id.lv);
        tvNodata = findViewById(R.id.tv_nodata);
        tv_zc = findViewById(R.id.tv_zc);
        checked_count = findViewById(R.id.checked_count);
        tv_total = findViewById(R.id.tv_total);
        tv_hasCheck = findViewById(R.id.tv_hasCheck);
        tv_ljds = findViewById(R.id.tv_ljds);
        tv_hasCheck_rate = findViewById(R.id.tv_hasCheck_rate);
        tv_my_upload = findViewById(R.id.tv_my_upload);


        Intent intent = getIntent();
        type = intent.getStringExtra("type");

        String UserIDTemp = (String) SPUtils.get(context, Contans.USERID, "");
        username = RSAUtils.decryptBASE64StrLocal(UserIDTemp);

        lv.setEmptyView(tvNodata);
        headView = View.inflate(this, R.layout.checked_status_xsgz_item, null);
        headView.findViewById(R.id.head).setVisibility(View.VISIBLE);
        lv.addHeaderView(headView, null, false);
        tv_djrw = headView.findViewById(R.id.tv_djrw);
        if(type!=null&&type.equals("YX")){
            String zyid = (String) SPUtils.get(context, Contans.ZY, "");
            if(zyid!=null&&(!zyid.equals(""))){
                List<String> zyids = this.stringToList(zyid);
//                Log.i("app","---------");
//                Log.i("app",new Gson().toJson(zyids));
//                Log.i("app",zyids.get(0));
                if(zyids.size()>0){
                    this.professionId=zyids.get(0);
                }
            }

            initToolbar("巡检统计", "选择专业", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(type.equals("YX")){
                        listDialog.show();;
                    }
                }
            });
            professionList = DataSupport.findAll(Profession.class);
            if(professionList!=null&&professionList.size()>0){
                for(Profession profession : professionList){
                    listPro.add(profession.getZymc());
                }
            }
            if(listPro!=null&&listPro.size()>0){
                listDialog = new ListDialog(context, listPro, new ListDialog.ListDialogListener() {
                    @Override
                    public void selectText(String text, int position) {
                        if(professionList!=null&&professionList.size()>0){
                            Profession profession = professionList.get(position);
                            //Log.i("app",profession.getZyid());
                            getStatisticsData(profession.getZyid());
                        }
                    }
                });
                listDialog.setTvTitle("请选择专业");
            }
        }else{
            initToolbar("巡检统计", "", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }

        tv_my_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(XJStatisticsActivity.this, XJStatisticsMyUploadActivity.class);
                intent.putExtra("type",type);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        getMyUploadData();
        getStatisticsData(null);
        //setListData();
    }

    private List<String> stringToList(String strs) {
        String[] str = strs.split(",");
        return Arrays.asList(str);
    }

    //获取本人上传的巡检数据
    private void getMyUploadData() {
        //Log.i("app",Contans.IP + Contans.GET_MY_UPLOAD_CHECKED);
        Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.GET_MY_UPLOAD_CHECKED, RequestMethod.POST);
        requestxs.setDefineRequestBodyForJson(createJson());

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

            @Override
            public void onSucceed(int what, Response<String> response) {
                String result = response.get();
                showLog("----------------");
                showLog(result);
                MyUploadCheckedBean bean = GsonToBean(result, MyUploadCheckedBean.class);
                if(bean!=null&&bean.getState()==1){
                    List<MyUploadCheckedBean.DataBean> data = bean.getData();
//                    Log.i("app",bean.getMsg());
//                    Log.i("app",String.valueOf(bean.getState()));
//                    Log.i("app",String.valueOf(bean.getData().size()));
//                    Log.i("app",new Gson().toJson(bean.getData()));
                    XJStatisticsActivity.this.runOnUiThread(new Runnable() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void run() {
                            if(type.equals("BA")){
                                tv_zc.setText("保安");
                            }
                            if(data!=null&&data.size()>0){
                                checked_count.setText(String.valueOf(data.size()));
                                String zc = data.get(0).getZc();
                                if(!TextUtils.isEmpty(zc)){
                                    tv_zc.setText(zc+"值");
                                }
                            }
                        }
                    });
                }
            }

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

            }
        });
    }

    //获取本人上传的巡检数据
    private void getStatisticsData(String zyid) {
        if(type!=null&&type.equals("YX")){
            if(zyid!=null){
                this.professionId=zyid;
            }
        }

        Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.GET_STATISTICS_DATA, RequestMethod.POST);
        requestxs.setDefineRequestBodyForJson(createJsonStatistics());
        requestxs.setReadTimeout(30000);

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

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

                String result = response.get();
                showLog("----------------");
                showLog(result);

                StatisticsBean bean = GsonToBean(result, StatisticsBean.class);
                if(bean!=null&&bean.getState()==1){
                    showLog(String.valueOf(bean.getData().size()));
                    showLog(new Gson().toJson(bean.getData()));
                    List<StatisticsBean.DataBean> data = bean.getData();
                    XJStatisticsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(data!=null&&data.size()>0){

                                int countZds = 0;
                                int countYjds = 0;
                                int countLjds = 0;
                                for (StatisticsBean.DataBean item: data) {
                                    String zds = item.getZds();
                                    String yjds = item.getYjds();
                                    String ljds = item.getLjds();
                                    countZds = countZds + Integer.parseInt(zds);
                                    countYjds = countYjds + Integer.parseInt(yjds);
                                    countLjds = countLjds + Integer.parseInt(ljds);
                                }
                                tv_total.setText(String.valueOf(countZds));
                                tv_hasCheck.setText(String.valueOf(countYjds));
                                tv_ljds.setText(String.valueOf(countLjds));
                                java.text.DecimalFormat df=new java.text.DecimalFormat("##.#%");//传入格式模板
                                String rate=df.format((float)(countYjds)/(float)countZds);
                                tv_hasCheck_rate.setText(rate);

                                setListData(data);
                            }
                        }
                    });
                }
            }

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

            }
        });
    }

    private void setListData(List<StatisticsBean.DataBean> data) {
        adapter = new CommonAdapter<StatisticsBean.DataBean>(context, R.layout.checked_status_xsgz_item, data) {

            @Override
            protected void convert(ViewHolder viewHolder, StatisticsBean.DataBean item, int position) {
                viewHolder.setText(R.id.tv_xh, item.getZds());
                viewHolder.setText(R.id.tv_qymc, item.getQymc());
                viewHolder.setText(R.id.tv_djrw, item.getYjds());
                //viewHolder.setTextColor(R.id.tv_djrw, Color.parseColor("#00CD00"));//已经检查完成绿色
            }
        };
        lv.setAdapter(adapter);
    }

    private String createJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("YX_XJGL_MY_UPLOAD");
        info.setUsername(username);
        info.setType(type);
        String json = GsonUtils.GsonString(info);
        showLog(json);
        return json;
    }
    private String createJsonStatistics() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("XJ_STATISTICS_CHECKED");
        if(type!=null&&type.equals("YX")){
            info.setZy(this.professionId);
        }else{
            info.setZy(type);
        }
        String json = GsonUtils.GsonString(info);
        showLog(json);
        return json;
    }
}