package com.hw.nativeapp.ui.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hw.nativeapp.MApplication;
import com.hw.nativeapp.R;
import com.hw.nativeapp.httpnet.ErrorConsumer;
import com.hw.nativeapp.httpnet.ResponseConsumer;
import com.hw.nativeapp.httpnet.entity.handleBean;
import com.hw.nativeapp.utils.ActivityUtils;
import com.hw.nativeapp.utils.DateUtils;
import com.hw.nativeapp.utils.RxUtil;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class SelectInfoActivity extends BaseActivity  {

    public static final Integer COUNTDOWN_TIME = 120000;
    private CountDownTimer timer;
    private TitleBar titleBar;

    // 演出时间
    @BindView(R.id.is_selectDate)
    LinearLayout is_selectDate;
    // 演出场次
    @BindView(R.id.is_selectTimes)
    LinearLayout is_selectTimes;
    // 票务信息
    @BindView(R.id.is_goods)
    LinearLayout is_goods;
    // 类型选择
    @BindView(R.id.is_selectRegion)
    LinearLayout is_selectRegion;


    private String performId = "";
    private String auditoriumId = "";
    private String date = "";
    private String goodsId = "";
    private String salePeice = "";
    private String performTimeId = "";
    private String seatTypeId = "";

    private JSONObject auditorium = new JSONObject();

    @Override
    void initView() {
        titleBar = findViewById(R.id.tb_main_bar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {

            @Override
            public void onLeftClick(View v) {
                ActivityUtils.activityFinish(SelectInfoActivity.this, true );
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
        getPageTransmitParams();
        startCountDownTimer();
        getPerform();
    }
    //获取页面传递参数
    private void getPageTransmitParams(){
        Intent transmitIntent = getIntent();
        performId = transmitIntent.getStringExtra("performId");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_info);
    }

    @OnClick({R.id.select_btn})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.select_btn:
                Bundle bundle = new Bundle();
                bundle.putString("performId", performId);
                bundle.putString("auditoriumId", auditoriumId);
                bundle.putString("performTimeId", performTimeId);
                bundle.putString("seatTypeId", seatTypeId);
                bundle.putString("goodsId", goodsId);
                bundle.putString("salePeice", salePeice);
                Log.e("salePeice","---------------salePeice--------------"+salePeice);
                ActivityUtils.launchActivity(this, InfoEnterActivity.class, bundle);
                break;
        }
    }

    // 获取演艺厅信息
    private void getPerform(){
        Map< String,String> options = new HashMap<>();
        options.put("performId", performId);
        MApplication.getApiService().performSell( options, System.currentTimeMillis() / 1000)
                .compose(RxUtil.applyObservableAsync())
                .subscribe(new ResponseConsumer<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject data) {
                        JSONArray theatreList = data.getJSONArray("theatreList");
                        JSONArray goodsList = data.getJSONArray("goodsList");
                        JSONObject obj = theatreList.getJSONObject(0);
                        JSONArray auditoriumList = obj.getJSONArray("auditoriumList");
                        auditorium = auditoriumList.getJSONObject(0);
                        auditoriumId = auditorium.getString("id");
                        getSelectDate();
                        addGoods(goodsList);
                    }

                    @Override
                    public void onFailed(int code, String msg) {

                    }
                }, new ErrorConsumer());
    }
    // 获取演出日期
    private void getSelectDate(){
        Map< String,String> options = new HashMap<>();
        options.put("performId", performId);
        options.put("auditoriumId", auditorium.getString("id"));
        MApplication.getApiService().selectDate( options, System.currentTimeMillis() / 1000)
                .compose(RxUtil.applyObservableAsync())
                .subscribe(new ResponseConsumer<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject data) throws ParseException {
                        JSONArray list = data.getJSONArray("list");
                        date = list.getJSONObject(0).getString("performDate");
                        getSelectTimes();
                        addDate(list);
                    }

                    @Override
                    public void onFailed(int code, String msg) {

                    }
                }, new ErrorConsumer());
    }

    // 获取演出场次
    private void getSelectTimes(){
        Map< String,String> options = new HashMap<>();
        options.put("performId", performId);
        options.put("auditoriumId", auditorium.getString("id"));
        options.put("date", date);
        MApplication.getApiService().selectTimes( options, System.currentTimeMillis() / 1000)
                .compose(RxUtil.applyObservableAsync())
                .subscribe(new ResponseConsumer<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject data) {
                        JSONArray list = data.getJSONArray("list");
                        performTimeId = list.getJSONObject(0).getString("id");
                        is_selectTimes.removeAllViews();
                        addTimes(list);
                        getSelectRegion();
                    }

                    @Override
                    public void onFailed(int code, String msg) {

                    }
                }, new ErrorConsumer());
    }

    // 获取类型选择
    private void getSelectRegion(){
        JSONObject options = new  JSONObject();
        options.put("performId", performId);
        options.put("auditoriumId", auditorium.getString("id"));
        options.put("performTimeId", performTimeId);
        options.put("goodsId", goodsId);
        MApplication.getApiService().selectRegion( options, System.currentTimeMillis() / 1000)
                .compose(RxUtil.applyObservableAsync())
                .subscribe(new ResponseConsumer<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject data) {
                        JSONArray list = data.getJSONArray("regionPriceList");
                        is_selectRegion.removeAllViews();
                        addRegion(list);
                    }

                    @Override
                    public void onFailed(int code, String msg) {

                    }
                }, new ErrorConsumer());
    }

    private void jumpToLogin(){
        stopCountDownTimer();
        ActivityUtils.activityFinish(SelectInfoActivity.this, true);
    }
    private void stopCountDownTimer(){
        if (timer != null){
            timer.cancel();
        }
    }
    private void startCountDownTimer(){
        timer = new CountDownTimer(COUNTDOWN_TIME, 1000) {
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                titleBar.setRightTitle(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                jumpToLogin();
            }
        };
        timer.start();
    }

    // 添加演出时间
    private void addDate(JSONArray list) throws ParseException {
        for (int i = 0; i < list.size(); i++) {
            LinearLayout linearLayout = new LinearLayout(this);
            // 设置LinearLayout的布局参数
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//            layoutParams.width = 380;
            layoutParams.setMargins(0, 10, 50, 30);
            linearLayout.setPadding(50, 30, 50, 30);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setLayoutParams(layoutParams);

            // 创建天TextView对象
            TextView textView = new TextView(this);
            textView.setGravity(Gravity.CENTER);
            textView.setText(DateUtils.getWeek(list.getJSONObject(i).getString("performDate")));
            // 创建时TextView对象
            TextView textView1 = new TextView(this);
            textView1.setPadding(0,20,0,0);
            textView1.setGravity(Gravity.CENTER);
            textView1.setText(list.getJSONObject(i).getString("performDate"));

            if(i == 0){
                linearLayout.setBackgroundResource(R.drawable.bg_box_02);
                textView1.setTextColor(getResources().getColor(R.color.login_bk));
                textView.setTextColor(getResources().getColor(R.color.login_bk));
            } else {
                linearLayout.setBackgroundResource(R.drawable.bg_box_01);
                textView1.setTextColor(getResources().getColor(R.color.black));
                textView.setTextColor(getResources().getColor(R.color.black));
            }
            linearLayout.setTag(list.getJSONObject(i).getString("performDate"));
            date = list.getJSONObject(0).getString("performDate");
            linearLayout.addView(textView);
            linearLayout.addView(textView1);
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int j = 0; j < is_selectDate.getChildCount(); j++){
                        LinearLayout linearLayout = (LinearLayout) is_selectDate.getChildAt(j);
                        linearLayout.setBackgroundResource(R.drawable.bg_box_01);
                        TextView textView = (TextView) linearLayout.getChildAt(1);
                        textView.setTextColor(getResources().getColor(R.color.black));
                        TextView textView1 = (TextView) linearLayout.getChildAt(0);
                        textView1.setTextColor(getResources().getColor(R.color.black));
                    }
                    linearLayout.setBackgroundResource(R.drawable.bg_box_02);
                    textView1.setTextColor(getResources().getColor(R.color.login_bk));
                    textView.setTextColor(getResources().getColor(R.color.login_bk));
                    date = linearLayout.getTag().toString();
                    getSelectTimes();
                }
            });
            is_selectDate.addView(linearLayout);
        }
    }

    // 添加演出场次
    private void addTimes(JSONArray list) {
        for (int i = 0; i < list.size(); i++) {
            LinearLayout linearLayout = new LinearLayout(this);
            // 设置LinearLayout的布局参数
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//            layoutParams.width = 420;
            layoutParams.setMargins(0, 40, 50, 30);
            linearLayout.setPadding(50, 30, 50, 30);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setLayoutParams(layoutParams);

            // 创建天TextView对象
            TextView textView = new TextView(this);
            textView.setGravity(Gravity.CENTER);
            textView.setText(list.getJSONObject(i).getString("performTimeStart") + " - " + list.getJSONObject(i).getString("performTimeEnd"));
            performTimeId = list.getJSONObject(0).getString("id");
            linearLayout.setTag(list.getJSONObject(i).getString("id"));
            if(i == 0){
                linearLayout.setBackgroundResource(R.drawable.bg_box_02);
                textView.setTextColor(getResources().getColor(R.color.login_bk));
            } else {
                linearLayout.setBackgroundResource(R.drawable.bg_box_01);
                textView.setTextColor(getResources().getColor(R.color.black));
            }

            linearLayout.addView(textView);
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int j = 0; j < is_selectTimes.getChildCount(); j++){
                        LinearLayout linearLayout = (LinearLayout) is_selectTimes.getChildAt(j);
                        linearLayout.setBackgroundResource(R.drawable.bg_box_01);
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextColor(getResources().getColor(R.color.black));
                    }
                    linearLayout.setBackgroundResource(R.drawable.bg_box_02);
                    textView.setTextColor(getResources().getColor(R.color.login_bk));
                    performTimeId = linearLayout.getTag().toString();
                    getSelectRegion();
                }
            });

            is_selectTimes.addView(linearLayout);
        }
    }

    // 添加票务信息
    private void addGoods(JSONArray list) {
        for (int i = 0; i < list.size(); i++) {
            LinearLayout linearLayout = new LinearLayout(this);
            // 设置LinearLayout的布局参数
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//            layoutParams.width = 420;
            layoutParams.setMargins(0, 40, 50, 30);
            linearLayout.setPadding(50, 30, 50, 30);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setLayoutParams(layoutParams);

            // 创建天TextView对象
            TextView textView = new TextView(this);
            textView.setGravity(Gravity.CENTER);
            textView.setText(list.getJSONObject(i).getString("goodsName"));

            if(i == 0){
                linearLayout.setBackgroundResource(R.drawable.bg_box_02);
                textView.setTextColor(getResources().getColor(R.color.login_bk));
            } else {
                linearLayout.setBackgroundResource(R.drawable.bg_box_01);
                textView.setTextColor(getResources().getColor(R.color.black));
            }
            goodsId = list.getJSONObject(0).getString("id");
            linearLayout.setTag(list.getJSONObject(i).getString("id"));
            linearLayout.addView(textView);
            int finalI = i;
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int j = 0; j < is_goods.getChildCount(); j++){
                        LinearLayout linearLayout = (LinearLayout) is_goods.getChildAt(j);
                        linearLayout.setBackgroundResource(R.drawable.bg_box_01);
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextColor(getResources().getColor(R.color.black));
                    }
                    linearLayout.setBackgroundResource(R.drawable.bg_box_02);
                    goodsId = linearLayout.getTag().toString();
                    textView.setTextColor(getResources().getColor(R.color.login_bk));
                }
            });

            is_goods.addView(linearLayout);
        }
    }

    // 添加类型选择
    private void addRegion(JSONArray list) {
        for (int i = 0; i < list.size(); i++) {
            LinearLayout linearLayout = new LinearLayout(this);
            // 设置LinearLayout的布局参数
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//            layoutParams.width = 420;
            layoutParams.setMargins(0, 40, 50, 30);
            linearLayout.setPadding(50, 30, 50, 30);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setLayoutParams(layoutParams);

            // 创建天TextView对象
            TextView textView = new TextView(this);
            textView.setGravity(Gravity.CENTER);
            textView.setText(list.getJSONObject(i).getString("seatTypeName") + ": " + list.getJSONObject(i).getString("salePrice") + "元");
            if(i == 0){
                linearLayout.setBackgroundResource(R.drawable.bg_box_02);
                textView.setTextColor(getResources().getColor(R.color.login_bk));
            } else {
                linearLayout.setBackgroundResource(R.drawable.bg_box_01);
                textView.setTextColor(getResources().getColor(R.color.black));
            }
            salePeice = list.getJSONObject(0).getString("salePrice");
            seatTypeId = list.getJSONObject(0).getString("seatTypeId");
            linearLayout.addView(textView);
            int finalI = i;
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int j = 0; j < is_selectRegion.getChildCount(); j++){
                        LinearLayout linearLayout = (LinearLayout) is_selectRegion.getChildAt(j);
                        linearLayout.setBackgroundResource(R.drawable.bg_box_01);
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextColor(getResources().getColor(R.color.black));
                    }
                    linearLayout.setBackgroundResource(R.drawable.bg_box_02);
                    textView.setTextColor(getResources().getColor(R.color.login_bk));
                    salePeice = list.getJSONObject(finalI).getString("salePrice");
                    seatTypeId = list.getJSONObject(finalI).getString("seatTypeId");
                }
            });

            is_selectRegion.addView(linearLayout);
        }
    }
}