package jp.odakyu.toiletsignage.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Typeface;

import java.util.ArrayList;

import jp.odakyu.toiletsignage.R;
import jp.odakyu.toiletsignage.info.ToiletInfo;
import jp.odakyu.toiletsignage.json.ResultToiletJson;
import jp.odakyu.toiletsignage.json.ToiletDefaultJson;

import static java.lang.Boolean.TRUE;

/**
 * Created by agentec on 2017/11/30.
 */

public class SignageActivity extends ParentActivity {
    private final String TAG = "SignageActivity";
    private ImageView sexualityIconImageView;
    private ImageView statusIconImageView;
    private ImageView leftStatusIconImageView;
    private ImageView rightStatusIconImageView;
    private TextView vacantCountTextView;
    private TextView totalCountTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (toiletInfo.getToiletDefaultJsonArray() != null && toiletInfo.getToiletDefaultJsonArray().size() == 3) {
            setContentView(R.layout.activity_signage_3);
        } else {
            setContentView(R.layout.activity_signage_1);
        }

        findViewById(R.id.statusIconImageView);

        settingInitDisplay();
        settingFontFamily();
    }

    @Override
    protected void onResume() {
        Log.i(TAG, "onResume");
        super.onResume();
    }
    @Override
    protected void onPause() {
        Log.i(TAG, "onPause");
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        Log.i(TAG, "onStop");
        super.onStop();
    }

    @Override
    protected void onStart() {
        Log.i(TAG, "onStart");
        super.onStart();
    }

    private void settingInitDisplay() {
        sexualityIconImageView = (ImageView) findViewById(R.id.sexualityIconImageView);
        sexualityIconImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // クリック時の処理
                moveMainView();
            }
        });
        statusIconImageView = (ImageView) findViewById(R.id.statusIconImageView);
        vacantCountTextView = (TextView) findViewById(R.id.vacantCountTextView);
        totalCountTextView = (TextView) findViewById(R.id.totalCountTextView);

        if (toiletInfo.getToiletDefaultJsonArray() != null && toiletInfo.getToiletDefaultJsonArray().size() == 3) {
            leftStatusIconImageView = (ImageView) findViewById(R.id.leftStatusIconImageView);
            rightStatusIconImageView = (ImageView) findViewById(R.id.rightStatusIconImageView);

            //お近くのトイレ情報設定
            TextView leftGateNameJPTextView = (TextView) findViewById(R.id.leftGateNameJPTextView);
            TextView leftGateNameENTextView = (TextView) findViewById(R.id.leftGateNameENTextView);
            TextView rightGateNameJPTextView = (TextView) findViewById(R.id.rightGateNameJPTextView);
            TextView rightGateNameENTextView = (TextView) findViewById(R.id.rightGateNameENTextView);

            for (ToiletDefaultJson toiletDefaultJson:
            toiletInfo.getToiletDefaultJsonArray()) {
                if (toiletDefaultJson.getPriority() == ToiletInfo.VALUE_PRIORITY_SUB_LEFT) {
                    leftGateNameJPTextView.setText(toiletDefaultJson.getToiletNameJP());
                    leftGateNameENTextView.setText(toiletDefaultJson.getToiletNameEN());
                } else if (toiletDefaultJson.getPriority() == ToiletInfo.VALUE_PRIORITY_SUB_RIGHT) {
                    rightGateNameJPTextView.setText(toiletDefaultJson.getToiletNameJP());
                    rightGateNameENTextView.setText(toiletDefaultJson.getToiletNameEN());
                }
            }
        }

        int toiletSex = toiletInfo.getToiletSex();

        if (toiletInfo.getToiletDefaultJsonArray() != null && toiletInfo.getToiletDefaultJsonArray().size() == 3) {
            if (toiletSex == ToiletInfo.VALUE_SEX_MAN) {
                sexualityIconImageView.setImageResource(R.drawable.icon_man_3);
            } else {
                sexualityIconImageView.setImageResource(R.drawable.icon_woman_3);
            }
        } else {
            if (toiletSex == ToiletInfo.VALUE_SEX_MAN) {
                sexualityIconImageView.setImageResource(R.drawable.icon_man_1);
            } else {
                sexualityIconImageView.setImageResource(R.drawable.icon_woman_1);
            }
        }
        settingDisplay();
    }

    private void settingFontFamily() {
        TextView mainTitleJPTextView = (TextView) findViewById(R.id.mainTitleJPTextView);
        TextView mainTitleENTextView = (TextView) findViewById(R.id.mainTitleENTextView);
        TextView vacantTitleJPTextView = (TextView) findViewById(R.id.vacantTitleJPTextView);
        TextView vacantTitleENTextView = (TextView) findViewById(R.id.vacantTitleENTextView);
        TextView vacantCountTextView = (TextView) findViewById(R.id.vacantCountTextView);
        TextView totalTitleJPTextView = (TextView) findViewById(R.id.totalTitleJPTextView);
        TextView totalTitleENTextView = (TextView) findViewById(R.id.totalTitleENTextView);
        TextView totalCountTextView = (TextView) findViewById(R.id.totalCountTextView);

        mainTitleJPTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
        mainTitleENTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryo.ttf"));
        vacantTitleJPTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
        vacantTitleENTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
        vacantCountTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
        totalTitleJPTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
        totalTitleENTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
        totalCountTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));

        if (toiletInfo.getToiletDefaultJsonArray() != null && toiletInfo.getToiletDefaultJsonArray().size() == 3) {
            TextView subTitleJPTextView = (TextView) findViewById(R.id.subTitleJPTextView);
            TextView subTitleENTextView = (TextView) findViewById(R.id.subTitleENTextView);
            TextView leftGateNameJPTextView = (TextView) findViewById(R.id.leftGateNameJPTextView);
            TextView leftGateNameENTextView = (TextView) findViewById(R.id.leftGateNameENTextView);
            TextView rightGateNameJPTextView = (TextView) findViewById(R.id.rightGateNameJPTextView);
            TextView rightGateNameENTextView = (TextView) findViewById(R.id.rightGateNameENTextView);
            subTitleJPTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
            subTitleENTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryo.ttf"));
            leftGateNameJPTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
            leftGateNameENTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryo.ttf"));
            rightGateNameJPTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryob.ttf"));
            rightGateNameENTextView.setTypeface(Typeface.createFromAsset(getAssets(), "meiryo.ttf"));
        }
    }

    @Override
    public void reloadDisplayView() {
        Log.i(TAG, "reloadDisplayView");
        //バックグラウンドスレッドからメーイン
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (toiletInfo.getConnectErrorCount() > ToiletInfo.MAX_ERROR_COUNT) {
                    //サーバ側から取得したトイレ情報削除
                    toiletInfo.clearResultToiletJsonMap();
                    moveMainView();
                } else {
                    settingDisplay();
                }
            }
        });
    }

    private void moveMainView() {
        Intent intent = new Intent(SignageActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void settingDisplay() {
        ArrayList<ToiletDefaultJson> toiletDefaultJsonArray = toiletInfo.getToiletDefaultJsonArray();

        if (toiletDefaultJsonArray == null || toiletDefaultJsonArray.size() == 0) {
            //MainActivity 移動
            moveMainView();
        }

        for (ToiletDefaultJson toiletDefaultJson:
                toiletDefaultJsonArray) {
            ArrayList<ResultToiletJson> resultToiletJson = toiletInfo.getResultToiletJsonMap(toiletDefaultJson.getToiletId());
            int vacantCount = toiletInfo.getVacantCount(toiletDefaultJson.getToiletId());
            int totalCount = toiletInfo.getTotalCount(toiletDefaultJson.getToiletId());
            if (toiletDefaultJson.getPriority() == toiletInfo.VALUE_PRIORITY_MAIN) { //開設トイレ


                if (vacantCount == -1) {
                    //取得失敗
                    continue;
                }
                vacantCountTextView.setText(String.valueOf(vacantCount));
                totalCountTextView.setText(String.valueOf(totalCount));
                if (toiletInfo.getToiletDefaultJsonArray() != null && toiletInfo.getToiletDefaultJsonArray().size() == 3) {
                    if (vacantCount == 0) {
                        statusIconImageView.setImageResource(R.drawable.icon_full_3);
                    } else {
                        statusIconImageView.setImageResource(R.drawable.icon_empty_3);
                    }
                } else {
                    if (vacantCount == 0) {
                        statusIconImageView.setImageResource(R.drawable.icon_full_1);
                    } else {
                        statusIconImageView.setImageResource(R.drawable.icon_empty_1);

                    }
                }
            } else if (toiletDefaultJson.getPriority() == toiletInfo.VALUE_PRIORITY_SUB_LEFT) { //お近くトイレ（左側）
                if (vacantCount == 0) {
                    leftStatusIconImageView.setImageResource(R.drawable.icon_full_sub);
                } else {
                    leftStatusIconImageView.setImageResource(R.drawable.icon_empty_sub);
                }
            } else if (toiletDefaultJson.getPriority() == toiletInfo.VALUE_PRIORITY_SUB_RIGHT) { //お近くトイレ（左側）
                if (vacantCount == 0) {
                    rightStatusIconImageView.setImageResource(R.drawable.icon_full_sub);
                } else {
                    rightStatusIconImageView.setImageResource(R.drawable.icon_empty_sub);
                }
            }
        }
    }
}
