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

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.esri.arcgisruntime.geometry.Point;
import com.gaoyuan.weixinrecord.utils.PermissionHelper;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.planPointRepair.controller.PointRepairController;
import com.tofly.ghrq_route.module.planPointRepair.domain.PlanPointRepair;
import com.tofly.ghrq_route.module.sqlite.DBDaoHelper;
import com.tofly.ghrq_route.module.trouble.domain.TroublePictureBean;
import com.tofly.ghrq_route.module.uploadFile.controller.UploadFileController;
import com.tofly.ghrq_route.common.util.Location.LocationUtil_xu;
import com.tofly.ghrq_route.common.util.image.PhotoHelper;

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

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/7/25.
 */

public class UiPlanPointRepair extends BaseUi {
    @BindView(R.id.edit_repair_main_pressure_on)
    EditText editRepairPressureOn;
    @BindView(R.id.edit_repair_sub_pressure_off)
    EditText editRepairSubPressureOff;
    @BindView(R.id.edit_repair_main_pressure_off)
    EditText editRepairPressureOff;
    @BindView(R.id.edit_repair_main_pressure_cut)
    EditText editRepairPressureCut;
    @BindView(R.id.edit_report_note)
    EditText editReportNote;
    @BindView(R.id.text_add_photo)
    TextView textAddPhoto;
    @BindView(R.id.text_type_popup)
    TextView textTypePopup;
    @BindView(R.id.btn_add_photo)
    Button btnAddPhoto;
    @BindView(R.id.layout_btn_add)
    LinearLayout layoutBtnAdd;
    @BindView(R.id.photo_gallery)
    LinearLayout layoutGallery;
    @BindView(R.id.layout_addpic_area)
    LinearLayout layoutAddPicArea;

    @BindArray(R.array.repair_photo_type)
    public String[] repairTypes;

    private static final String TAG = UiPlanPointRepair.class.getName();

    private int mPhotoType = 0;

    private List<TroublePictureBean> mPicturesUpload = new ArrayList<TroublePictureBean>();

    private int mUploadIndex = 0;

    private DBDaoHelper dbDaoHelper;

    private PhotoHelper photoHelper;

    private PermissionHelper permissionHelper;

    private PointRepairController repairController;

    private UploadFileController uploadFileController;

    private PlanPointRepair mPointRepair;

    private PlanPointRepair mRepariHistoryData;

    private OfflinePathBean mPlanPoint;

    private PlanBean mPlan;

    private Point mLocation;

    private boolean mbIsNewRepair = false;

    private boolean mbIsLocal = false;

    private boolean mbIsEditable = false;

    private boolean mbIsRemovable = false;

    private boolean mbNeedRemoveCache = true;

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

        initView();
        initData();
        initListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_danger_report, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_next:
                if (checkValid()){
                    gotoUiPointRepair();
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {

    }

    private void initData() {
        dbDaoHelper = DBDaoHelper.getInstance();
        permissionHelper = new PermissionHelper(this);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null){
            mPointRepair = (PlanPointRepair) bundle.getSerializable("point_repair");
            mPlanPoint = (OfflinePathBean) bundle.getSerializable("plan_point");
            mPlan = (PlanBean) bundle.getSerializable("plan");
            LocationUtil_xu.CoordinateClass coordinate =
                    (LocationUtil_xu.CoordinateClass) bundle.getSerializable("location");
            mLocation = new Point(coordinate.Longtitude, coordinate.Latitude);
            mRepariHistoryData = (PlanPointRepair)bundle.getSerializable("repair_history");
            mbIsNewRepair = bundle.getBoolean("is_new", false);
            mbIsLocal = bundle.getBoolean("is_local", false);
        }
    }

    private void initListener() {

    }

    private boolean checkValid() {
        String onPressure = editRepairPressureOn.getText().toString();
        String subOffPresure = editRepairSubPressureOff.getText().toString();
        String offPresure = editRepairPressureOff.getText().toString();
        String cutPressure = editRepairPressureCut.getText().toString();
        String notes = editReportNote.getText().toString();

        if (onPressure == null || onPressure.isEmpty()) {
            toast("请填写主路运行压力数据");
            return false;
        }

        if (subOffPresure == null || subOffPresure.isEmpty()) {
            toast("请填写副路关闭压力数据");
            return false;
        }

        if (offPresure == null || offPresure.isEmpty()) {
            toast("请填写主路关闭压力数据");
            return false;
        }

        if (cutPressure == null || cutPressure.isEmpty()) {
            toast("请填写主路切断压力数据");
            return false;
        }

         mPointRepair.setNotes(notes);
        return true;
    }

    private void gotoUiPointRepair() {
        if (mPointRepair != null && mPlanPoint != null){
            Bundle bundle = new Bundle();
            bundle.putSerializable("point_repair", mPointRepair);
            bundle.putSerializable("plan_point", mPlanPoint);
            bundle.putSerializable("plan", mPlan);
            bundle.putBoolean("is_new", true);
            bundle.putBoolean("is_local", false);
            LocationUtil_xu.CoordinateClass coordinate =
                    new LocationUtil_xu.CoordinateClass(mLocation.getX(), mLocation.getY());
            bundle.putSerializable("location", coordinate);
            Intent intent = new Intent();
            intent.setClass(this, UiAdjustDeviceRepair.class);
            intent.putExtras(bundle);
            startActivity(intent);
            this.finish();
        }
    }

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