package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.DialogTitle;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lcp.arecyclerview.widget.ARecycleView;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.OfflineWork.OfflineWorkTool;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineWorkJobBean;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineWorkJobBean.OfflineWorkJobType;
import com.tofly.ghrq_route.module.OfflineWork.domain.OfflineWorkJobCountBean;
import com.tofly.ghrq_route.module.gps.GpsTool;
import com.tofly.ghrq_route.common.ui.adapter.OfflineWorkJobsInfoAdapter;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/2/13.
 */

public class OfflineWorkJobsInfoDailog extends AlertDialog {
    @BindView(android.R.id.icon)
    ImageView icon;
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.text_hint1)
    TextView textHint1;
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;

    private Context mContext;

    private OfflineWorkTool offlineWorkTool;

    private OfflineWorkJobsInfoAdapter jobsInfoAdapter;

    public OfflineWorkJobsInfoDailog(Context context){
        super(context, R.style.AlertDialog);
        mContext = context;
        offlineWorkTool = OfflineWorkTool.getInstance();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_user_bind_car);
        ButterKnife.bind(this);

        initView();
        initData();
    }


    private void initView() {
        alertTitle.setText(R.string.offline_job_info_title);
        textHint1.setVisibility(View.GONE);
        button1.setText(android.R.string.ok);
        button2.setVisibility(View.GONE);
        recyclerView.setEnabled(false);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.height = (int) (d.heightPixels * 0.5); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    private void initData(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        List job_list = new ArrayList<OfflineWorkJobCountBean>();
        jobsInfoAdapter = new OfflineWorkJobsInfoAdapter(mContext, job_list);
        recyclerView.setAdapter(jobsInfoAdapter);
        String noData = mContext.getResources().getString(R.string.offline_job_empty_data);
        recyclerView.setEmptyViewText(noData);
        getOfflineJobList();
    }

    public void reloadData(){
        jobsInfoAdapter.clear();
        getOfflineJobList();
    }

    private void getOfflineJobList(){
        int count = seperateOfflineWorkJobCount();
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.height = (int) (d.heightPixels * 0.8); // 高度设置为屏幕的0.6
        if (count < 4){
            lp.height = (int) (d.heightPixels * 0.5); // 高度设置为屏幕的0.6
        }
        dialogWindow.setAttributes(lp);
    }

    private int seperateOfflineWorkJobCount(){
        recyclerView.setRefreshing(true);

        int sign_history_count = 0;
        int upload_photo_count = 0;
        int plan_info_count = 0;
        int danger_add_count = 0;
        int plan_danger_count = 0;
        int plan_device_count = 0;
        int build_add_count = 0;
        int build_report_count = 0;
        int build_log_count = 0;
        int build_notice_count = 0;

        List<OfflineWorkJobBean> jobBeans = offlineWorkTool.getOfflineWorkJobs();
        if (jobBeans != null) {
            for (OfflineWorkJobBean jobBean : jobBeans) {
                int job_type = jobBean.getJob_type();
                switch (job_type) {
                    case OfflineWorkJobType.JOB_SIGN_UP_OFF_ADD:
                        sign_history_count++;
                        break;
                    case OfflineWorkJobType.JOB_UPLOAD_PHOTO_ADD:
                        upload_photo_count++;
                        break;
                    case OfflineWorkJobType.JOB_PLAN_INFO_CHANGED:
                        plan_info_count++;
                        break;
                    case OfflineWorkJobType.JOB_DANGER_DANGER_ADD:
                        danger_add_count++;
                        break;
                    case OfflineWorkJobType.JOB_DANGER_PLAN_ROUTE_ADD:
                    case OfflineWorkJobType.JOB_DANGER_PLAN_CLEAR_ADD:
                        plan_danger_count++;
                        break;
                    case OfflineWorkJobType.JOB_COMMON_DEVICE_ROUTE_ADD:
                    case OfflineWorkJobType.JOB_ADJUST_DEVICE_ROUTE_ADD:
                        plan_device_count++;
                        break;
                    case OfflineWorkJobType.JOB_BUILD_MANAGE_ADD:
                        build_add_count++;
                        break;
                    case OfflineWorkJobType.JOB_BUILD_REPORT_ADD:
                        build_report_count++;
                        break;
                    case OfflineWorkJobType.JOB_BUILD_LOG_ADD:
                        build_log_count++;
                        break;
                    case OfflineWorkJobType.JOB_BUILD_NOTICE_PHOTO_ADD:
                    case OfflineWorkJobType.JOB_BUILD_NOTICE_AUDIO_ADD:
                        build_notice_count++;
                        break;
                }
            }
        }

        List<OfflineWorkJobCountBean> jobCounts = new ArrayList<>();
        OfflineWorkJobCountBean jobCountBean;
        if (sign_history_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_SIGN_UP_OFF_ADD, sign_history_count);
            jobCounts.add(jobCountBean);
        }
        if (upload_photo_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_UPLOAD_PHOTO_ADD, upload_photo_count);
            jobCounts.add(jobCountBean);
        }
        if (plan_info_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_PLAN_INFO_CHANGED, plan_info_count);
            jobCounts.add(jobCountBean);
        }
        if (danger_add_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_DANGER_DANGER_ADD, danger_add_count);
            jobCounts.add(jobCountBean);
        }
        if (plan_danger_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_DANGER_PLAN_ROUTE_ADD, plan_danger_count);
            jobCounts.add(jobCountBean);
        }
        if (plan_device_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_COMMON_DEVICE_ROUTE_ADD, plan_device_count);
            jobCounts.add(jobCountBean);
        }
        if (build_add_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_BUILD_MANAGE_ADD, build_add_count);
            jobCounts.add(jobCountBean);
        }
        if (build_report_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_BUILD_REPORT_ADD, build_report_count);
            jobCounts.add(jobCountBean);
        }
        if (build_log_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_BUILD_LOG_ADD, build_log_count);
            jobCounts.add(jobCountBean);
        }
        if (build_notice_count > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_BUILD_NOTICE_PHOTO_ADD, build_notice_count);
            jobCounts.add(jobCountBean);
        }

        GpsTool gpsTool = GpsTool.getInstance();
        long unUploadPointCount = gpsTool.getUnUploadPointsCount();
        if (unUploadPointCount > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_PLAN_POINTS_OVER_ADD, unUploadPointCount);
            jobCounts.add(jobCountBean);
        }
        long unUploadGpsCount = offlineWorkTool.getUnUploadGpsCount();
        if (unUploadGpsCount > 0){
            jobCountBean = new OfflineWorkJobCountBean(OfflineWorkJobType.JOB_SAVED_GPS_UPLOAD, unUploadGpsCount);
            jobCounts.add(jobCountBean);
        }

        jobsInfoAdapter.setData(jobCounts);
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.setRefreshing(false);
            }
        }, 1000);


        return jobCounts.size();
    }

    @OnClick({android.R.id.button1})
    public void onClick(View view) {
        switch (view.getId()) {
            /*Button OK*/
            case android.R.id.button1:
                this.dismiss();
                break;
        }
    }
}
